From 2bb759f96c4537dc5a3b0ee60ae44d506e005b69 Mon Sep 17 00:00:00 2001 From: Brad Jones Date: Wed, 16 Mar 2022 19:38:46 -0600 Subject: [PATCH] Bubble up errors by passing them through. --- src/transports/EventEmitterTransport.ts | 4 +++- src/transports/HTTPTransport.ts | 4 +++- src/transports/TransportRequestManager.ts | 4 +++- src/transports/WebSocketTransport.ts | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/transports/EventEmitterTransport.ts b/src/transports/EventEmitterTransport.ts index 9583581..e7679db 100644 --- a/src/transports/EventEmitterTransport.ts +++ b/src/transports/EventEmitterTransport.ts @@ -31,7 +31,9 @@ class EventEmitterTransport extends Transport { this.transportRequestManager.settlePendingRequest(notifications); return prom; } catch (e) { - const responseErr = new JSONRPCError(e.message, ERR_UNKNOWN, e); + const responseErr = e instanceof JSONRPCError + ? e + : new JSONRPCError(e.message, ERR_UNKNOWN, e); this.transportRequestManager.settlePendingRequest(notifications, responseErr); return Promise.reject(responseErr); } diff --git a/src/transports/HTTPTransport.ts b/src/transports/HTTPTransport.ts index 3532bd3..606273e 100644 --- a/src/transports/HTTPTransport.ts +++ b/src/transports/HTTPTransport.ts @@ -49,7 +49,9 @@ class HTTPTransport extends Transport { return Promise.reject(responseErr); } } catch (e) { - const responseErr = new JSONRPCError(e.message, ERR_UNKNOWN, e); + const responseErr = e instanceof JSONRPCError + ? e + : new JSONRPCError(e.message, ERR_UNKNOWN, e); this.transportRequestManager.settlePendingRequest(notifications, responseErr); this.transportRequestManager.settlePendingRequest(getBatchRequests(data), responseErr); return Promise.reject(responseErr); diff --git a/src/transports/TransportRequestManager.ts b/src/transports/TransportRequestManager.ts index 03d679f..fc4c03a 100644 --- a/src/transports/TransportRequestManager.ts +++ b/src/transports/TransportRequestManager.ts @@ -67,7 +67,9 @@ export class TransportRequestManager { } return this.resolveRes(data, emitError); } catch (e) { - const err = new JSONRPCError("Bad response format", ERR_UNKNOWN, payload); + const err = e instanceof JSONRPCError + ? e + : new JSONRPCError("Bad response format", ERR_UNKNOWN, payload); if (emitError) { this.transportEventChannel.emit("error", err); } diff --git a/src/transports/WebSocketTransport.ts b/src/transports/WebSocketTransport.ts index 9c38d79..6f8b34c 100644 --- a/src/transports/WebSocketTransport.ts +++ b/src/transports/WebSocketTransport.ts @@ -31,7 +31,9 @@ class WebSocketTransport extends Transport { const notifications = getNotifications(data); this.connection.send(JSON.stringify(this.parseData(data)), (err?: Error) => { if (err) { - const jsonError = new JSONRPCError(err.message, ERR_UNKNOWN, err); + const jsonError = err instanceof JSONRPCError + ? err + : new JSONRPCError(e.message, ERR_UNKNOWN, err); this.transportRequestManager.settlePendingRequest(notifications, jsonError); this.transportRequestManager.settlePendingRequest(getBatchRequests(data), jsonError); prom = Promise.reject(jsonError);