-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Align RPC errors with EIP-1193 / EIP-1474 spec
- Loading branch information
Showing
5 changed files
with
158 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,41 @@ | ||
// EIP-1474 error codes | ||
export enum RpcErrorCode { | ||
ParseError = -32700, | ||
InvalidRequest = -32600, | ||
MethodNotFound = -32601, | ||
InvalidParams = -32602, | ||
InternalError = -32603, | ||
} | ||
|
||
// EIP-1193 error codes | ||
export enum ProviderErrorCode { | ||
// EIP-1193 error codes | ||
UserRejectedRequest = 4001, | ||
Unauthorized = 4100, | ||
UnsupportedMethod = 4200, | ||
Disconnected = 4900, | ||
} | ||
|
||
// EI-1474 error messages used as default | ||
export const RpcErrorMessage: Record<RpcErrorCode, string> = { | ||
[-32700]: "Parse error", | ||
[-32600]: "Invalid request", | ||
[-32601]: "Method not found", | ||
[-32602]: "Invalid params", | ||
[-32603]: "Internal error", | ||
// EIP-1474 / JSON-RPC error codes | ||
ParseError = -32700, | ||
InvalidRequest = -32600, | ||
MethodNotFound = -32601, | ||
InvalidParams = -32602, | ||
InternalError = -32603, | ||
} | ||
|
||
// EIP-1193 error messages used as default | ||
export const ProviderErrorMessage: Record<ProviderErrorCode, string> = { | ||
// EIP-1193 error messages | ||
[4001]: "User rejected request", | ||
[4100]: "Unauthorized", | ||
[4200]: "Unsupported method", | ||
[4900]: "Disconnected", | ||
} | ||
|
||
export class RpcError extends Error { | ||
public code: RpcErrorCode | ||
public data?: unknown | ||
|
||
constructor({ | ||
code, | ||
message, | ||
data, | ||
cause, | ||
}: { | ||
code: RpcErrorCode | ||
message?: string | ||
data?: unknown | ||
cause?: any | ||
}) { | ||
if (!RpcErrorMessage[code]) { | ||
throw new Error(`Invalid RPC error code: ${code}`) | ||
} | ||
super(message || RpcErrorMessage[code]) | ||
this.code = code | ||
this.data = data | ||
if (cause) { | ||
this.stack = `${this.stack}\nCaused by: ${cause.stack}` | ||
} | ||
} | ||
// EIP-1474 / JSON-RPC error messages | ||
[-32700]: "Parse error", | ||
[-32600]: "Invalid request", | ||
[-32601]: "Method not found", | ||
[-32602]: "Invalid params", | ||
[-32603]: "Internal error", | ||
} | ||
|
||
export class ProviderError extends Error { | ||
public code: ProviderErrorCode | ||
public data?: any | ||
|
||
constructor(code: ProviderErrorCode, message?: string) { | ||
super(message || ProviderErrorMessage[code]) | ||
constructor({code, cause}: {code: ProviderErrorCode; cause?: any}) { | ||
super(ProviderErrorMessage[code]) | ||
this.code = code | ||
if (cause) { | ||
this.stack = `${this.stack}\nCaused by: ${cause?.stack || cause}` | ||
} | ||
this.data = {cause} | ||
} | ||
} |