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 all 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
4 changes: 4 additions & 0 deletions snaps-sidebar.js
Expand Up @@ -63,6 +63,10 @@ const sidebar = {
type: "doc",
id: "reference/permissions",
},
{
type: "doc",
id: "reference/known-errors",
},
{
type: "category",
label: "Snaps command line",
Expand Down
56 changes: 56 additions & 0 deletions snaps/how-to/communicate-errors.md
@@ -0,0 +1,56 @@
---
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.
See the [Snaps known errors reference](../reference/known-errors.md) for the full list of errors.

## Import and throw errors

To throw these known errors, first import them from the
[`@metamask/snaps-sdk`](https://github.com/MetaMask/snaps/tree/main/packages/snaps-sdk) package,
then throw them where needed.
For 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 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.
1 change: 0 additions & 1 deletion snaps/reference/entry-points.md
@@ -1,6 +1,5 @@
---
description: See the Snaps entry points reference.
sidebar_position: 2
---

# Snaps entry points
Expand Down
26 changes: 26 additions & 0 deletions snaps/reference/known-errors.md
@@ -0,0 +1,26 @@
---
description: See the Snaps known errors reference
---

# Snaps known errors

Snaps can [communicate the following errors](../how-to/communicate-errors.md) without crashing the Snap:

| Error | What the error indicates | Error code |
|----------------------------|-------------------------------------------------------------|:----------:|
| `ChainDisconnectedError` | The provider is disconnected from the requested chain. | `4901` |
| `DisconnectedError` | The provider is disconnected. | `4900` |
| `InternalError` | An internal error has occurred. | `-32603` |
| `InvalidInputError` | The input to a method is invalid. | `-32000` |
| `InvalidParamsError` | The parameters to a method are invalid. | `-32602` |
| `InvalidRequestError` | The request is invalid. | `-32600` |
| `LimitExceededError` | A limit has been exceeded. | `-32005` |
| `MethodNotFoundError` | A method does not exist. | `-32601` |
| `MethodNotSupportedError` | A method is not supported. | `-32004` |
| `ParseError` | A request is not valid JSON. | `-32700` |
| `ResourceNotFoundError` | A resource does not exist. | `-32001` |
| `ResourceUnavailableError` | A resource is unavailable. | `-32002` |
| `TransactionRejected` | A transaction has been rejected. | `-32003` |
| `UnauthorizedError` | The requested method/account is not authorized by the user. | `4100` |
| `UnsupportedMethodError` | The requested method is not supported by the provider. | `4200` |
| `UserRejectedRequestError` | The user has rejected the request. | `4001` |
1 change: 0 additions & 1 deletion snaps/reference/permissions.md
@@ -1,6 +1,5 @@
---
description: See the Snaps permissions reference.
sidebar_position: 4
---

import Tabs from '@theme/Tabs';
Expand Down
1 change: 0 additions & 1 deletion snaps/reference/snaps-api.md
@@ -1,6 +1,5 @@
---
description: See the Snaps API reference.
sidebar_position: 1
toc_max_heading_level: 2
---

Expand Down
8 changes: 8 additions & 0 deletions src/css/custom.css
Expand Up @@ -153,3 +153,11 @@ svg {
.getfeedback-hidden {
display: none !important;
}

.markdown {
> h1, h2, h3, h4, h5, h6 {
> code {
font-size: inherit;
}
}
}