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

Add documentation on Snaps known errors. #1117

Merged
merged 7 commits into from Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions snaps/how-to/communicate-errors.md
@@ -0,0 +1,47 @@
---
description: Communicate errors from your Snap without crashing it
sidebar_position: 9
---

# Communicate errors

The Snaps SDK exposes a set of known errors that can be thrown from your Snap code without crashing the Snap.

## Import and throw errors

To throw these known errors, you have to import them from the `@metamask/snaps-sdk` package, then throw them where needed. Here's an example:

```typescript
import type { OnRpcRequestHandler } from '@metamask/snaps-sdk';
import { MethodNotFoundError } from '@metamask/snaps-sdk';

export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
switch (request.method) {
case 'hello':
return 'Hello World!';
default:
// Throw a known error to avoid crashing the Snap
throw new MethodNotFoundError();
}
};
```

### Pass data along with the error

The error class constructors exported by `@metamask/snaps-sdk` have the following signature:

```typescript
class SnapJsonRpcError extends SnapError {
new (message?: string, data?: Record<string, Json>)
}
```

Both parameters are optional. If you don't pass `message`, then a pre-determined message is used. If you don't pass `data`, then an empty object is passed.

`data` can be any JSON-serializable object.

## Detect known errors in Dapps

Known errors are thrown back to the caller as JSON-RPC errors. They have a numeric `code`, a `message` string, and a `data` object.

The [Snaps known errors reference](../reference/known-errors.md) lists all the known errors with their codes and intended usage.
150 changes: 150 additions & 0 deletions snaps/reference/known-errors.md
@@ -0,0 +1,150 @@
---
description: See the Snaps known errors reference
sidebar_position: 7
---

# Snaps known errors

## `InternalError`

### Description
This can be thrown by a Snap to indicate that an internal error occurred,
without crashing the Snap.

### Error code
The code returned with this error is `-32603`.

## `InvalidInputError`

### Description
This can be thrown by a Snap to indicate that the input to a method is
invalid, without crashing the Snap.

### Error code
The code returned with this error is `-32000`.

## `InvalidParamsError`

### Description
This can be thrown by a Snap to indicate that the parameters to a method are
invalid, without crashing the Snap.

### Error code
The code returned with this error is `-32602`.

## `InvalidRequestError`

### Description
This can be thrown by a Snap to indicate that the request is invalid, without
crashing the Snap.

### Error code
The code returned with this error is `-32600`.

## `LimitExceededError`

### Description
This can be thrown by a Snap to indicate that a limit has been exceeded,
without crashing the Snap.

### Error code
The code returned with this error is `-32005`.

## `MethodNotFoundError`

### Description
This can be thrown by a Snap to indicate that a method does not exist,
without crashing the Snap.

### Error code
The code returned with this error is `-32601`.

## `MethodNotSupportedError`

### Description
This can be thrown by a Snap to indicate that a method is not supported,
without crashing the Snap.

### Error code
The code returned with this error is `-32004`.

## `ParseError`

### Description
This can be thrown by a Snap to indicate that a request is not valid JSON,
without crashing the Snap.

### Error code
The code returned with this error is `-32700`.

## `ResourceNotFoundError`

### Description
This can be thrown by a Snap to indicate that a resource does not exist,
without crashing the Snap.

### Error code
The code returned with this error is `-32001`.

## `ResourceUnavailableError`

### Description
This can be thrown by a Snap to indicate that a resource is unavailable,
without crashing the Snap.

### Error code
The code returned with this error is `-32002`.

## `TransactionRejected`

### Description
This can be thrown by a Snap to indicate that a transaction was rejected,
without crashing the Snap.

### Error code
The code returned with this error is `-32003`.

## `ChainDisconnectedError`

### Description
This can be thrown by a Snap to indicate that the provider is disconnected
from the requested chain, without crashing the Snap.

### Error code
The code returned with this error is `4901`.

## `DisconnectedError`

### Description
This can be thrown by a Snap to indicate that the provider is disconnected,
without crashing the Snap.

### Error code
The code returned with this error is `4900`.

## `UnauthorizedError`

### Description
This can be thrown by a Snap to indicate that the requested method / account
is not authorized by the user, without crashing the Snap.

### Error code
The code returned with this error is `4100`.

## `UnsupportedMethodError`

### Description
This can be thrown by a Snap to indicate that the requested method is not
supported by the provider, without crashing the Snap.

### Error code
The code returned with this error is `4200`.

## `UserRejectedRequestError`

### Description
This can be thrown by a Snap to indicate that the user rejected the request,
without crashing the Snap.

### Error code
The code returned with this error is `4001`.