Skip to content

Commit

Permalink
chore: added a function to better construct the message
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Nov 7, 2024
1 parent 7fb0b5a commit 0ddd329
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/RPCServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ import * as events from './events';

const cleanupReason = Symbol('CleanupReason');

function composeMessage(error: unknown): string {
if (
typeof error === 'boolean' ||
typeof error === 'number' ||
typeof error === 'string' ||
typeof error === 'bigint' ||
typeof error === 'symbol'
) {
return `Non-error literal ${String(error)} was thrown`;
}
if (
typeof error === 'object' &&
error != null &&
'message' in error &&
error.message != null
) {
return String(error.message);
}
if (typeof error === 'object' && error?.constructor?.name != null) {
return `Non-error object ${error.constructor.name} was thrown`;
}
return `Non-error value ${error} was thrown`;
}

/**
* You must provide a error handler `addEventListener('error')`.
* Otherwise errors will just be ignored.
Expand Down Expand Up @@ -327,7 +351,7 @@ class RPCServer {
try {
const rpcError: JSONRPCResponseError = {
code: errors.JSONRPCResponseErrorCode.RPCRemote,
message: e.message ?? e.constructor.name,
message: composeMessage(e),
data: this.fromError(e),
};
const rpcErrorMessage: JSONRPCResponseFailed = {
Expand Down Expand Up @@ -599,7 +623,7 @@ class RPCServer {
try {
const rpcError: JSONRPCResponseError = {
code: errors.JSONRPCResponseErrorCode.RPCRemote,
message: e.message ?? e.constructor.name,
message: composeMessage(e),
data: this.fromError(e),
};
const rpcErrorMessage: JSONRPCResponseFailed = {
Expand Down

0 comments on commit 0ddd329

Please sign in to comment.