From 7d6cff2203824aa189eee4602702002e8bdd8ab8 Mon Sep 17 00:00:00 2001 From: Melisa Anabella Rossi Date: Tue, 5 Dec 2023 10:42:23 -0300 Subject: [PATCH] fix: valid undefined error (#545) * fix: valid undefined error * remove unnecessary condition --- src/providers/WebSocketProvider.ts | 4 ++-- src/providers/common.ts | 3 ++- src/utils/jsonrpc.ts | 3 ++- test/jsonrpc.isValidResponse.spec.ts | 29 ++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/providers/WebSocketProvider.ts b/src/providers/WebSocketProvider.ts index 47f775cd..461d4c18 100644 --- a/src/providers/WebSocketProvider.ts +++ b/src/providers/WebSocketProvider.ts @@ -180,8 +180,8 @@ export class WebSocketProvider { this.responseCallbacks.delete(id) - if ('error' in message) { - defer.reject(Object.assign(new Error(message.error.message || message.error), message.error)) + if (message.error !== undefined && message.error !== null) { + defer.reject(Object.assign(new Error((message.error as any).message || message.error), message.error)) } else if ('result' in message) { defer.resolve(message) } diff --git a/src/providers/common.ts b/src/providers/common.ts index 072e6288..c542900e 100644 --- a/src/providers/common.ts +++ b/src/providers/common.ts @@ -10,7 +10,7 @@ export type RPCMessage = { export type RPCError = { jsonrpc: '2.0' id: number - error: any + error: string | number | boolean | symbol | object } export type RPCResponse = @@ -19,6 +19,7 @@ export type RPCResponse = jsonrpc: '2.0' id: number result: any + error?: undefined | null } export function toRPC(message: RPCMessage): RPCMessage { diff --git a/src/utils/jsonrpc.ts b/src/utils/jsonrpc.ts index 1d348430..acd5f6b8 100644 --- a/src/utils/jsonrpc.ts +++ b/src/utils/jsonrpc.ts @@ -55,9 +55,10 @@ export function isValidResponse(response: RPCResponse | RPCResponse[]) { function validateSingleMessage(message: RPCResponse) { return ( !!message && - !('error' in message) && + (message.error === undefined || message.error === null) && message.jsonrpc === '2.0' && typeof message.id === 'number' && + 'result' in message && message.result !== undefined ) // only undefined is not valid json object // the null is not a valid response for rpc nodes diff --git a/test/jsonrpc.isValidResponse.spec.ts b/test/jsonrpc.isValidResponse.spec.ts index 7793a865..575bf293 100644 --- a/test/jsonrpc.isValidResponse.spec.ts +++ b/test/jsonrpc.isValidResponse.spec.ts @@ -130,5 +130,34 @@ describe('jsonrpc', function () { // then expect(valid).toEqual(true) }) + + it('should validate jsonrpc response with result and undefined error', () => { + let response: RPCResponse = { + jsonrpc: '2.0', + id: 1, + result: "3", + error: undefined + } + expect(Jsonrpc.isValidResponse(response)).toEqual(true) + }) + + it('should validate jsonrpc response with result and null error', () => { + let response: RPCResponse = { + jsonrpc: '2.0', + id: 1, + result: "3", + error: null + } + expect(Jsonrpc.isValidResponse(response)).toEqual(true) + }) + + it('should validate jsonrpc response with error', () => { + let response: RPCResponse = { + jsonrpc: '2.0', + id: 1, + error: "23" + } + expect(Jsonrpc.isValidResponse(response)).toEqual(false) + }) }) })