js-rpc2 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-rpc2",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "js web websocket http rpc",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/lib.js CHANGED
@@ -574,7 +574,7 @@ export async function rpcRunServerDecodeBuffer(extension, writer, buffer) {
574
574
  box = {
575
575
  id: dataId,
576
576
  type: RPC_TYPE_ERROR,
577
- data: buildRpcItemData([`Error:${error.message}\n${error.stack}`]),
577
+ data: buildRpcItemData([error.message, error.stack]),
578
578
  }
579
579
  }
580
580
  await writer.write(buildRpcData(box))
@@ -593,7 +593,11 @@ export function createRPCProxy(apiInvoke) {
593
593
  }
594
594
  proxy = new Proxy(Function, {
595
595
  async apply(_target, _thisArg, argArray) {
596
- return await apiInvoke(String(p), argArray)
596
+ try {
597
+ return await apiInvoke(String(p), argArray)
598
+ } catch (error) {
599
+ throw new Error(error.message, { cause: error })
600
+ }
597
601
  }
598
602
  })
599
603
  map.set(p, proxy)
@@ -644,6 +648,20 @@ export function createRpcServerHelper(param) {
644
648
  * }} RPC_HELPER_CLIENT
645
649
  */
646
650
 
651
+ class RPCError extends Error {
652
+ /**
653
+ * @param {string} message
654
+ * @param {string} stack
655
+ * @param {ErrorOptions} [option]
656
+ */
657
+ constructor(message, stack, option) {
658
+ super(message, option)
659
+ if (stack) {
660
+ this.stack = stack
661
+ }
662
+ }
663
+ }
664
+
647
665
  /**
648
666
  * @param {{ rpcKey: string; }} param
649
667
  */
@@ -666,7 +684,7 @@ export function createRpcClientHelper(param) {
666
684
  if (callbackFunctionMap.has(data.id)) {
667
685
  let o = callbackFunctionMap.get(data.id)
668
686
  if (data.type == RPC_TYPE_ERROR) {
669
- o.promise.reject(new Error(items.at(0).data))
687
+ o.promise.reject(new RPCError(items.at(0).data, items.at(1).data))
670
688
  }
671
689
  if (data.type == RPC_TYPE_RETURN) {
672
690
  callbackFunctionMap.delete(data.id)
package/src/lib.test.js CHANGED
@@ -282,4 +282,48 @@ export async function runWithAbortController(func) {
282
282
  await func(ac)
283
283
  await sleep(1000)
284
284
  } finally { ac.abort() }
285
- }
285
+ }
286
+
287
+
288
+ test('error-stack', async () => {
289
+ // node --test-name-pattern="^error-stack$" src/lib.test.js
290
+
291
+ // server
292
+ const app = new Koa()
293
+ const router = new Router()
294
+ app.use(router.routes())
295
+ app.use(router.allowedMethods())
296
+ let server = createServer(app.callback()).listen(9000)
297
+
298
+ class RpcApi {
299
+ async hello() {
300
+ new URL('/')
301
+ }
302
+ }
303
+
304
+ const extension = new RpcApi()
305
+
306
+ createRpcServerKoaRouter({
307
+ path: '/rpc/abc',
308
+ router: router,
309
+ extension: extension,
310
+ })
311
+
312
+
313
+ // client
314
+
315
+ /** @type{RpcApi} */
316
+ const rpc = createRpcClientHttp({
317
+ // url: `/rpc/abc`, // in same site html page
318
+ url: `http://127.0.0.1:9000/rpc/abc`, // others
319
+ })
320
+
321
+ try {
322
+ let ret = await rpc.hello()
323
+ console.info(ret)
324
+ } catch (error) {
325
+ console.error(error)
326
+ }
327
+ server.close()
328
+
329
+ })