Skip to content

Commit

Permalink
fix: error handling on error response without id
Browse files Browse the repository at this point in the history
  • Loading branch information
zcstarr committed Sep 19, 2019
1 parent 4469a9c commit cdecef1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/transports/TransportRequestManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
20 changes: 12 additions & 8 deletions src/transports/TransportRequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit cdecef1

Please sign in to comment.