From 2db24ffadc8a99faa5382c2dcab20983a251dc34 Mon Sep 17 00:00:00 2001 From: Brad Jones Date: Mon, 20 Mar 2023 22:59:06 -0600 Subject: [PATCH] Pass through errors that are not HTTP, but include response --- src/Error.ts | 2 ++ src/transports/HTTPTransport.ts | 5 +++-- src/transports/TransportRequestManager.ts | 16 +++++++++------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Error.ts b/src/Error.ts index d8d3ae2..9ce0ce1 100644 --- a/src/Error.ts +++ b/src/Error.ts @@ -6,6 +6,8 @@ export class JSONRPCError extends Error { public message: string; public code: number; public data?: unknown; + // For transports that run over Fetch. + public response?: Response; constructor(message: string, code: number, data?: any) { super(message); this.message = message; diff --git a/src/transports/HTTPTransport.ts b/src/transports/HTTPTransport.ts index 49ae92e..f264366 100644 --- a/src/transports/HTTPTransport.ts +++ b/src/transports/HTTPTransport.ts @@ -39,8 +39,9 @@ class HTTPTransport extends Transport { const notifications = getNotifications(data); const batch = getBatchRequests(data); const fetcher = this.injectedFetcher || fetch; + let result; try { - const result = await fetcher(this.uri, { + result = await fetcher(this.uri, { method: "POST", headers: this.headers, body: JSON.stringify(this.parseData(data)), @@ -54,7 +55,7 @@ class HTTPTransport extends Transport { const body = await result.text(); const responseErr = this.transportRequestManager.resolveResponse(body); if (responseErr) { - // requirements are that batch requuests are successfully resolved + // requirements are that batch requests are successfully resolved // this ensures that individual requests within the batch request are settled this.transportRequestManager.settlePendingRequest(batch, responseErr); return Promise.reject(responseErr); diff --git a/src/transports/TransportRequestManager.ts b/src/transports/TransportRequestManager.ts index fc4c03a..a19b890 100644 --- a/src/transports/TransportRequestManager.ts +++ b/src/transports/TransportRequestManager.ts @@ -124,17 +124,19 @@ export class TransportRequestManager { private resolveRes(data: IJSONRPCNotificationResponse | IJSONRPCResponse, emitError: boolean): TransportResponse { const { id, error } = data; - const status = this.pendingRequest[id as string]; - if (status) { - delete this.pendingRequest[id as string]; - this.processResult(data, status); - this.transportEventChannel.emit("response", data as IJSONRPCResponse); - return; - } if (id === undefined && error === undefined) { this.transportEventChannel.emit("notification", data as IJSONRPCNotificationResponse); return; } + const status = this.pendingRequest[id as string]; + if (status) { + delete this.pendingRequest[id as string]; + if (error === undefined) { + this.processResult(data, status); + this.transportEventChannel.emit("response", data as IJSONRPCResponse); + return; + } + } let err; if (error) { err = convertJSONToRPCError(data);