diff --git a/jest.config.js b/jest.config.js index dabcda1..c073e0f 100644 --- a/jest.config.js +++ b/jest.config.js @@ -45,10 +45,10 @@ module.exports = { // An object that configures minimum threshold enforcement for coverage results coverageThreshold: { global: { - branches: 91.89, - functions: 94.59, - lines: 92.42, - statements: 92.42, + branches: 92.59, + functions: 94.73, + lines: 92.64, + statements: 92.64, }, }, diff --git a/src/utils.test.ts b/src/utils.test.ts index 9aefd4a..62f13db 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -98,7 +98,7 @@ describe('serializeError', () => { const result = serializeError(invalidError7); expect(result).toStrictEqual({ code: rpcCodes.internal, - message: getMessageFromCode(rpcCodes.internal), + message: invalidError7.message, data: { cause: { code: invalidError7.code, @@ -209,7 +209,7 @@ describe('serializeError', () => { const result = serializeError(error); expect(result).toStrictEqual({ code: errorCodes.rpc.internal, - message: getMessageFromCode(errorCodes.rpc.internal), + message: error.message, data: { cause: { message: error.message, @@ -220,7 +220,7 @@ describe('serializeError', () => { expect(JSON.parse(JSON.stringify(result))).toStrictEqual({ code: errorCodes.rpc.internal, - message: getMessageFromCode(errorCodes.rpc.internal), + message: error.message, data: { cause: { message: error.message, diff --git a/src/utils.ts b/src/utils.ts index de3e061..6e6bb95 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -145,16 +145,37 @@ function buildError( return error; } + const originalMessage = getOriginalMessage(error); + // If the error does not match the JsonRpcError type, use the fallback error, but try to include the original error as `cause`. const cause = serializeCause(error); const fallbackWithCause = { ...fallbackError, + ...(originalMessage && { message: originalMessage }), data: { cause }, }; return fallbackWithCause; } +/** + * Attempts to extract the original `message` property from an error value of uncertain shape. + * + * @param error - The error in question. + * @returns The original message, if it exists and is a non-empty string. + */ +function getOriginalMessage(error: unknown): string | undefined { + if ( + isObject(error) && + hasProperty(error, 'message') && + typeof error.message === 'string' && + error.message.length > 0 + ) { + return error.message; + } + return undefined; +} + /** * Check if the given code is a valid JSON-RPC server error code. *