Skip to content

Commit

Permalink
fix: valid undefined error (#545)
Browse files Browse the repository at this point in the history
* fix: valid undefined error

* remove unnecessary condition
  • Loading branch information
Melisa Anabella Rossi authored Dec 5, 2023
1 parent fc02b6e commit 7d6cff2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/providers/WebSocketProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ export class WebSocketProvider<T extends IWebSocket> {

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)
}
Expand Down
3 changes: 2 additions & 1 deletion src/providers/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -19,6 +19,7 @@ export type RPCResponse =
jsonrpc: '2.0'
id: number
result: any
error?: undefined | null
}

export function toRPC(message: RPCMessage): RPCMessage {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions test/jsonrpc.isValidResponse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

0 comments on commit 7d6cff2

Please sign in to comment.