Skip to content

Allow passing a cause to predefined error functions - #83

Merged
Mrtenz merged 6 commits into
mainfrom
mrtenz/error-cause
Apr 17, 2023
Merged

Allow passing a cause to predefined error functions#83
Mrtenz merged 6 commits into
mainfrom
mrtenz/error-cause

Conversation

@Mrtenz

Copy link
Copy Markdown
Member

This allows passing an Error class object to one of the predefined error functions as defined in rpcErrors and providerErrors. Upon calling the serialise function of the JsonRpcError class, the cause error will be serialised using the serializeCause function.

For example:

consterror=rpcErrors.invalidParams({data: {cause: newError('foo bar'),},});

Callng error.serialize() results in:

{
"code": -32602,
"message": "Invalid method parameter(s).",
"data": {
"cause": {
"stack": "Error: foo bar [...]",
"message": "foo bar"
}
},
"stack": "Error: Invalid method parameter(s). [...]"
}

This allows passing an `Error` class object to one of the predefined error functions as defined in `rpcErrors` and `providerErrors`. Upon calling the `serialise` function of the `JsonRpcError` class, the cause error will be serialised using the `serializeCause` function.
The expect function ensures that the type of error.serialize().data is an object and contains specific serialized data while ensuring it does not contain an error instance. This test is necessary to avoid unexpected errors during exception handling.
@Mrtenz
Mrtenz requested a review from a team as a code ownerApril 17, 2023 14:04
Comment threadsrc/utils.ts
Comment threadsrc/utils.ts
Comment threadsrc/errors.test.ts Outdated
Comment threadsrc/errors.test.ts Outdated
@Mrtenz
Mrtenz merged commit e6a9162 into mainApr 17, 2023
@Mrtenz
Mrtenz deleted the mrtenz/error-cause branch April 17, 2023 16:05
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@Mrtenz@FrederikBolding