Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing a cause to predefined error functions #83

Merged
merged 6 commits into from
Apr 17, 2023
Merged

Conversation

Mrtenz
Copy link
Member

@Mrtenz Mrtenz commented Apr 17, 2023

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:

const error = rpcErrors.invalidParams({
  data: {
    cause: new Error('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 owner April 17, 2023 14:04
src/utils.ts Show resolved Hide resolved
src/utils.ts Show resolved Hide resolved
src/errors.test.ts Outdated Show resolved Hide resolved
src/errors.test.ts Outdated Show resolved Hide resolved
@Mrtenz Mrtenz merged commit e6a9162 into main Apr 17, 2023
@Mrtenz Mrtenz deleted the mrtenz/error-cause branch April 17, 2023 16:05
Sign up for free to 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