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

feat: improve error code type #489

Merged
merged 1 commit into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/long-brooms-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Improved RpcError code types.
10 changes: 8 additions & 2 deletions src/types/eip1193.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ export type EIP1193Provider = Requests & Events
//////////////////////////////////////////////////
// Errors

// rome-ignore format: no formatting
export type RpcErrorCode =
// https://eips.ethereum.org/EIPS/eip-1193#provider-errors
| 4_001 | 4_100 | 4_200 | 4_900 | 4_901
// https://eips.ethereum.org/EIPS/eip-1474#error-codes
| -32700 | -32600 | -32601 | -32602 | -32603 | -32000 | -32001 | -32002 | -32003 | -32004 | -32005 | -32006
export class RpcError extends Error {
code: number
code: RpcErrorCode | (number & {})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds loose autocomplete. Could lock down completely though.

details: string

constructor(code: number, message: string) {
constructor(code: RpcErrorCode | (number & {}), message: string) {
super(message)
this.code = code
this.details = message
Expand Down