Skip to content

Commit

Permalink
construct fallback error
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyapotti committed Nov 22, 2024
1 parent 8c7f1a9 commit 46df215
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions src/jrpc/jrpcEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
JRPCEngineEndCallback,
JRPCEngineNextCallback,
JRPCEngineReturnHandler,
JRPCError,
JRPCMiddleware,
JRPCRequest,
JRPCResponse,
Expand All @@ -22,6 +23,22 @@ export type JrpcEngineEvents = {
notification: (...args: unknown[]) => void;
};

function constructFallbackError(error: Error): JRPCError {
const {
message = "",
code = -32603,
stack = "Stack trace is not available.",
data = "",
} = error as { message?: string; code?: number; stack?: string; data?: string };
const codeNumber = parseInt(code?.toString() || "-32603");
return {
message: message || error?.toString() || getMessageFromCode(codeNumber),
code: codeNumber,
stack,
data: data || message || error?.toString(),
};
}

/**
* A JSON-RPC request and response processor.
* Give it a stack of middleware, pass it requests, and get back responses.
Expand Down Expand Up @@ -87,12 +104,7 @@ export class JRPCEngine extends SafeEventEmitter<JrpcEngineEvents> {

res.error = serializeError(error, {
shouldIncludeStack: true,
fallbackError: {
message: error?.message || error?.toString() || getMessageFromCode(error?.code || -32603),
code: error?.code || -32603,
stack: error?.stack || "Stack trace is not available.",
data: error?.data || error?.message || error?.toString(),
},
fallbackError: constructFallbackError(error),
});
}
// True indicates that the request should end
Expand Down Expand Up @@ -333,12 +345,7 @@ export class JRPCEngine extends SafeEventEmitter<JrpcEngineEvents> {
log.error(error);
res.error = serializeError(error, {
shouldIncludeStack: true,
fallbackError: {
message: error?.message || error?.toString() || getMessageFromCode((error as { code?: number })?.code || -32603),
code: (error as { code?: number })?.code || -32603,
stack: error?.stack || "Stack trace is not available.",
data: (error as { data?: string })?.data || error?.message || error?.toString(),
},
fallbackError: constructFallbackError(error),
});
}
}
Expand Down Expand Up @@ -432,12 +439,7 @@ export function providerFromEngine(engine: JRPCEngine): SafeEventEmitterProvider
if (typeof res.error === "object" && Object.keys(res.error).includes("stack") === false) res.error.stack = "Stack trace is not available.";
log.error(res.error);
const err = serializeError(res.error, {
fallbackError: {
message: res.error?.message || res.error?.toString() || getMessageFromCode(res.error?.code || -32603),
code: res.error?.code || -32603,
stack: res.error?.stack || "Stack trace is not available.",
data: res.error?.data || res.error?.message || res.error?.toString(),
},
fallbackError: constructFallbackError(res.error),
shouldIncludeStack: true,
});

Expand Down

0 comments on commit 46df215

Please sign in to comment.