diff --git a/src/transports/TransportRequestManager.test.ts b/src/transports/TransportRequestManager.test.ts index ff7590f..dce05e5 100644 --- a/src/transports/TransportRequestManager.test.ts +++ b/src/transports/TransportRequestManager.test.ts @@ -44,6 +44,14 @@ describe("Transport Request Manager", () => { expect(err.message).toContain("Could not resolve"); }); + it("should error on missing id but error response", () => { + const errPayload = reqData.generateMockErrorResponse(9, "haha"); + delete errPayload.id; + const payload = JSON.stringify(errPayload); + const err = transportReqMan.resolveResponse(payload, false) as Error; + expect(err.message).toContain("Error message"); + }); + it("should error on missing id to resolve and emit error", (done) => { transportReqMan.transportEventChannel.on("error", (e) => { expect(e.message).toContain("Could not resolve"); diff --git a/src/transports/TransportRequestManager.ts b/src/transports/TransportRequestManager.ts index 87a1b68..def9c67 100644 --- a/src/transports/TransportRequestManager.ts +++ b/src/transports/TransportRequestManager.ts @@ -111,19 +111,23 @@ export class TransportRequestManager { } private resolveRes(data: IJSONRPCNotificationResponse | IJSONRPCResponse, emitError: boolean): TransportResponse { - const { id } = data; - if (id === undefined || id === null) { - this.transportEventChannel.emit("notification", data as IJSONRPCNotificationResponse); - return; - } - const status = this.pendingRequest[id]; + const { id, error} = data; + + const status = this.pendingRequest[id as string]; if (status) { - delete this.pendingRequest[id]; + delete this.pendingRequest[id as string]; this.processResult(data, status); this.transportEventChannel.emit("response", data as IJSONRPCResponse); return; } - const err = new JSONRPCError(`Could not resolve ${id}`, ERR_MISSIING_ID); + if (id === undefined && error === undefined) { + this.transportEventChannel.emit("notification", data as IJSONRPCNotificationResponse); + return; + } + let err = new JSONRPCError(`Could not resolve ${id}`, ERR_MISSIING_ID); + if (error) { + err = convertJSONToRPCError(data); + } if (emitError) { this.transportEventChannel.emit("error", err); }