Skip to content

Commit

Permalink
Pass through errors that are not HTTP, but include response
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjones1 committed Mar 21, 2023
1 parent 8e14099 commit 2db24ff
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/transports/HTTPTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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);
Expand Down
16 changes: 9 additions & 7 deletions src/transports/TransportRequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2db24ff

Please sign in to comment.