Skip to content

Commit 3f374b4

Browse files
authored
Implement SnapError (#1833)
This implements the `SnapError` class, and wrapping/unwrapping of errors thrown from a Snap. `SnapError`s will not cause the Snap to crash, and can be used to respond to JSON-RPC requests for example. Closes #1824.
1 parent 58b1ad9 commit 3f374b4

File tree

68 files changed

+1677
-455
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1677
-455
lines changed

packages/examples/packages/bip32/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "O0Ncs/BuMYDj6nUxHCvIn1rO5BkQi5o7bFSs9Yw3lH8=",
10+
"shasum": "QbIeP2qWOCUL23DriGKPaAJBCtWiFOAsYhJJOkmTPHk=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/bip32/src/index.test.ts

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ describe('onRpcRequest', () => {
1212
});
1313

1414
expect(response).toRespondWithError({
15-
code: -32603,
16-
message: 'Internal JSON-RPC error.',
15+
code: -32601,
16+
message: 'The method does not exist / is not available.',
17+
stack: expect.any(String),
1718
data: {
18-
cause: {
19-
message: 'The method does not exist / is not available.',
20-
stack: expect.any(String),
21-
},
19+
method: 'foo',
20+
cause: null,
2221
},
2322
});
2423

@@ -75,15 +74,10 @@ describe('onRpcRequest', () => {
7574
});
7675

7776
expect(response).toRespondWithError({
78-
code: -32603,
79-
message: 'Internal JSON-RPC error.',
80-
data: {
81-
cause: {
82-
message:
83-
'The requested path is not permitted. Allowed paths must be specified in the snap manifest.',
84-
stack: expect.any(String),
85-
},
86-
},
77+
code: 4100,
78+
message:
79+
'The requested path is not permitted. Allowed paths must be specified in the snap manifest.',
80+
stack: expect.any(String),
8781
});
8882

8983
await close();
@@ -101,15 +95,10 @@ describe('onRpcRequest', () => {
10195
});
10296

10397
expect(response).toRespondWithError({
104-
code: -32603,
105-
message: 'Internal JSON-RPC error.',
106-
data: {
107-
cause: {
108-
message:
109-
'The requested path is not permitted. Allowed paths must be specified in the snap manifest.',
110-
stack: expect.any(String),
111-
},
112-
},
98+
code: 4100,
99+
message:
100+
'The requested path is not permitted. Allowed paths must be specified in the snap manifest.',
101+
stack: expect.any(String),
113102
});
114103

115104
await close();
@@ -202,14 +191,9 @@ describe('onRpcRequest', () => {
202191
await ui.cancel();
203192

204193
expect(await response).toRespondWithError({
205-
code: -32603,
206-
message: 'Internal JSON-RPC error.',
207-
data: {
208-
cause: {
209-
message: 'User rejected the request.',
210-
stack: expect.any(String),
211-
},
212-
},
194+
code: 4001,
195+
message: 'User rejected the request.',
196+
stack: expect.any(String),
213197
});
214198

215199
await close();

packages/examples/packages/bip44/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "MgSpx86WLoY6ebo+Ta3qcCO9yiuIdp5mY8FlooDZJZg=",
10+
"shasum": "jWcfzY4mlKuy+U0U5xNfKyrG0idGPeOaTNPl4Ua6uGw=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/bip44/src/index.test.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ describe('onRpcRequest', () => {
1212
});
1313

1414
expect(response).toRespondWithError({
15-
code: -32603,
16-
message: 'Internal JSON-RPC error.',
15+
code: -32601,
16+
message: 'The method does not exist / is not available.',
17+
stack: expect.any(String),
1718
data: {
18-
cause: {
19-
message: 'The method does not exist / is not available.',
20-
stack: expect.any(String),
21-
},
19+
method: 'foo',
20+
cause: null,
2221
},
2322
});
2423

@@ -69,15 +68,10 @@ describe('onRpcRequest', () => {
6968
});
7069

7170
expect(response).toRespondWithError({
72-
code: -32603,
73-
message: 'Internal JSON-RPC error.',
74-
data: {
75-
cause: {
76-
message:
77-
'The requested coin type is not permitted. Allowed coin types must be specified in the snap manifest.',
78-
stack: expect.any(String),
79-
},
80-
},
71+
code: 4100,
72+
message:
73+
'The requested coin type is not permitted. Allowed coin types must be specified in the snap manifest.',
74+
stack: expect.any(String),
8175
});
8276

8377
await close();
@@ -166,14 +160,9 @@ describe('onRpcRequest', () => {
166160
await ui.cancel();
167161

168162
expect(await response).toRespondWithError({
169-
code: -32603,
170-
message: 'Internal JSON-RPC error.',
171-
data: {
172-
cause: {
173-
message: 'User rejected the request.',
174-
stack: expect.any(String),
175-
},
176-
},
163+
code: 4001,
164+
message: 'User rejected the request.',
165+
stack: expect.any(String),
177166
});
178167

179168
await close();

packages/examples/packages/browserify-plugin/src/index.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ describe('onRpcRequest', () => {
1010
});
1111

1212
expect(response).toRespondWithError({
13-
code: -32603,
14-
message: 'Internal JSON-RPC error.',
13+
code: -32601,
14+
message: 'The method does not exist / is not available.',
15+
stack: expect.any(String),
1516
data: {
16-
cause: {
17-
message: 'The method does not exist / is not available.',
18-
stack: expect.any(String),
19-
},
17+
method: 'foo',
18+
cause: null,
2019
},
2120
});
2221

packages/examples/packages/browserify/src/index.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ describe('onRpcRequest', () => {
1010
});
1111

1212
expect(response).toRespondWithError({
13-
code: -32603,
14-
message: 'Internal JSON-RPC error.',
13+
code: -32601,
14+
message: 'The method does not exist / is not available.',
15+
stack: expect.any(String),
1516
data: {
16-
cause: {
17-
message: 'The method does not exist / is not available.',
18-
stack: expect.any(String),
19-
},
17+
method: 'foo',
18+
cause: null,
2019
},
2120
});
2221

packages/examples/packages/dialogs/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "49Ki82EdQq7D43dfQc6FSZ9BOs0gDniYJC6n5KxvoVo=",
10+
"shasum": "5088QO3qlZ8bJEDRjmeQaQX58F0WOX/us8kjdCuVNBc=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/dialogs/src/index.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ describe('onRpcRequest', () => {
1212
});
1313

1414
expect(response).toRespondWithError({
15-
code: -32603,
16-
message: 'Internal JSON-RPC error.',
15+
code: -32601,
16+
message: 'The method does not exist / is not available.',
17+
stack: expect.any(String),
1718
data: {
18-
cause: {
19-
message: 'The method does not exist / is not available.',
20-
stack: expect.any(String),
21-
},
19+
method: 'foo',
20+
cause: null,
2221
},
2322
});
2423

packages/examples/packages/ethereum-provider/src/index.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ describe('onRpcRequest', () => {
1010
});
1111

1212
expect(response).toRespondWithError({
13-
code: -32603,
14-
message: 'Internal JSON-RPC error.',
13+
code: -32601,
14+
message: 'The method does not exist / is not available.',
15+
stack: expect.any(String),
1516
data: {
16-
cause: {
17-
message: 'The method does not exist / is not available.',
18-
stack: expect.any(String),
19-
},
17+
method: 'foo',
18+
cause: null,
2019
},
2120
});
2221

packages/examples/packages/ethers-js/src/index.test.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ describe('onRpcRequest', () => {
1212
});
1313

1414
expect(response).toRespondWithError({
15-
code: -32603,
16-
message: 'Internal JSON-RPC error.',
15+
code: -32601,
16+
message: 'The method does not exist / is not available.',
17+
stack: expect.any(String),
1718
data: {
18-
cause: {
19-
message: 'The method does not exist / is not available.',
20-
stack: expect.any(String),
21-
},
19+
method: 'foo',
20+
cause: null,
2221
},
2322
});
2423

@@ -85,14 +84,9 @@ describe('onRpcRequest', () => {
8584
await ui.cancel();
8685

8786
expect(await response).toRespondWithError({
88-
code: -32603,
89-
message: 'Internal JSON-RPC error.',
90-
data: {
91-
cause: {
92-
message: 'User rejected the request.',
93-
stack: expect.any(String),
94-
},
95-
},
87+
code: 4001,
88+
message: 'User rejected the request.',
89+
stack: expect.any(String),
9690
});
9791

9892
await close();

0 commit comments

Comments
 (0)