Skip to content

Commit

Permalink
Improve error handling (#1841)
Browse files Browse the repository at this point in the history
This improves error handling by:

- Using `@metamask/rpc-errors` instead of `eth-rpc-errors` throughout
the entire stack.
- Introducing a new `SnapError`, which will not cause the Snap to crash
when thrown.
- Unwrapping errors where possible.
- Also bumps a ton of MetaMask dependencies, see
#1818

---------

Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com>
Co-authored-by: legobt <6wbvkn0j@anonaddy.me>
  • Loading branch information
3 people authored Oct 20, 2023
1 parent 495bdd4 commit f2e652b
Show file tree
Hide file tree
Showing 140 changed files with 2,247 additions and 998 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git a/dist/polyfills.js b/dist/polyfills.js
index 4f4f95302a6da4643b822262921f668abc8c3db1..78202915fa5c952222aa1751442f7a9410d73dc4 100644
--- a/dist/polyfills.js
+++ b/dist/polyfills.js
@@ -10,7 +10,7 @@ function builtinsPolyfills() {
libs.set('util', require.resolve('rollup-plugin-node-polyfills/polyfills/util'));
libs.set('sys', libs.get('util'));
libs.set('events', require.resolve('rollup-plugin-node-polyfills/polyfills/events'));
- libs.set('stream', require.resolve('rollup-plugin-node-polyfills/polyfills/stream'));
+ libs.set('stream', require.resolve('stream-browserify'));
libs.set('path', require.resolve('rollup-plugin-node-polyfills/polyfills/path'));
libs.set('querystring', require.resolve('rollup-plugin-node-polyfills/polyfills/qs'));
libs.set('punycode', require.resolve('rollup-plugin-node-polyfills/polyfills/punycode'));
diff --git a/esm/polyfills.js b/esm/polyfills.js
index 28b0ea77e1f7bc5766737caaa8cb5c5db25a279e..ae71645a1d24c46fc7987135aff0332e89017ec1 100644
--- a/esm/polyfills.js
+++ b/esm/polyfills.js
@@ -7,7 +7,7 @@ export function builtinsPolyfills() {
libs.set('util', require.resolve('rollup-plugin-node-polyfills/polyfills/util'));
libs.set('sys', libs.get('util'));
libs.set('events', require.resolve('rollup-plugin-node-polyfills/polyfills/events'));
- libs.set('stream', require.resolve('rollup-plugin-node-polyfills/polyfills/stream'));
+ libs.set('stream', require.resolve('stream-browserify'));
libs.set('path', require.resolve('rollup-plugin-node-polyfills/polyfills/path'));
libs.set('querystring', require.resolve('rollup-plugin-node-polyfills/polyfills/qs'));
libs.set('punycode', require.resolve('rollup-plugin-node-polyfills/polyfills/punycode'));
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
},
"resolutions": {
"@babel/core": "patch:@babel/core@npm%3A7.23.2#./.yarn/patches/@babel-core-npm-7.23.2-b93f586907.patch",
"@esbuild-plugins/node-modules-polyfill@^0.2.2": "patch:@esbuild-plugins/node-modules-polyfill@npm%3A0.2.2#./.yarn/patches/@esbuild-plugins-node-modules-polyfill-npm-0.2.2-f612681798.patch",
"@lavamoat/lavapack@^5.4.1": "patch:@lavamoat/lavapack@npm%3A5.4.1#./.yarn/patches/@lavamoat-lavapack-npm-5.4.1-2548b3e225.patch",
"@types/glob@*": "patch:@types/glob@npm%3A7.1.4#./.yarn/patches/@types-glob-npm-7.1.4-d45247eaa2.patch",
"@types/glob@^7.1.1": "patch:@types/glob@npm%3A7.1.4#./.yarn/patches/@types-glob-npm-7.1.4-d45247eaa2.patch",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/bip32/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"dependencies": {
"@metamask/key-tree": "^9.0.0",
"@metamask/rpc-errors": "^5.1.1",
"@metamask/rpc-errors": "^6.1.0",
"@metamask/snaps-types": "workspace:^",
"@metamask/snaps-ui": "workspace:^",
"@metamask/utils": "^8.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/bip32/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "k9a+qc7tJNdko1Kb1m9GsjwODLwJipQaW4OZt9KHFsY=",
"shasum": "QbIeP2qWOCUL23DriGKPaAJBCtWiFOAsYhJJOkmTPHk=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
48 changes: 16 additions & 32 deletions packages/examples/packages/bip32/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ describe('onRpcRequest', () => {
});

expect(response).toRespondWithError({
code: -32603,
message: 'Internal JSON-RPC error.',
code: -32601,
message: 'The method does not exist / is not available.',
stack: expect.any(String),
data: {
cause: {
message: 'The method does not exist / is not available.',
stack: expect.any(String),
},
method: 'foo',
cause: null,
},
});

Expand Down Expand Up @@ -75,15 +74,10 @@ describe('onRpcRequest', () => {
});

expect(response).toRespondWithError({
code: -32603,
message: 'Internal JSON-RPC error.',
data: {
cause: {
message:
'The requested path is not permitted. Allowed paths must be specified in the snap manifest.',
stack: expect.any(String),
},
},
code: 4100,
message:
'The requested path is not permitted. Allowed paths must be specified in the snap manifest.',
stack: expect.any(String),
});

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

expect(response).toRespondWithError({
code: -32603,
message: 'Internal JSON-RPC error.',
data: {
cause: {
message:
'The requested path is not permitted. Allowed paths must be specified in the snap manifest.',
stack: expect.any(String),
},
},
code: 4100,
message:
'The requested path is not permitted. Allowed paths must be specified in the snap manifest.',
stack: expect.any(String),
});

await close();
Expand Down Expand Up @@ -202,14 +191,9 @@ describe('onRpcRequest', () => {
await ui.cancel();

expect(await response).toRespondWithError({
code: -32603,
message: 'Internal JSON-RPC error.',
data: {
cause: {
message: 'User rejected the request.',
stack: expect.any(String),
},
},
code: 4001,
message: 'User rejected the request.',
stack: expect.any(String),
});

await close();
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/bip44/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"dependencies": {
"@metamask/key-tree": "^9.0.0",
"@metamask/rpc-errors": "^5.1.1",
"@metamask/rpc-errors": "^6.1.0",
"@metamask/snaps-types": "workspace:^",
"@metamask/snaps-ui": "workspace:^",
"@metamask/utils": "^8.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/bip44/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "x/St7jjXmPv5SXyAWe44u8aFB8TRfKx4B7oRBrWNv4k=",
"shasum": "jWcfzY4mlKuy+U0U5xNfKyrG0idGPeOaTNPl4Ua6uGw=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
35 changes: 12 additions & 23 deletions packages/examples/packages/bip44/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ describe('onRpcRequest', () => {
});

expect(response).toRespondWithError({
code: -32603,
message: 'Internal JSON-RPC error.',
code: -32601,
message: 'The method does not exist / is not available.',
stack: expect.any(String),
data: {
cause: {
message: 'The method does not exist / is not available.',
stack: expect.any(String),
},
method: 'foo',
cause: null,
},
});

Expand Down Expand Up @@ -69,15 +68,10 @@ describe('onRpcRequest', () => {
});

expect(response).toRespondWithError({
code: -32603,
message: 'Internal JSON-RPC error.',
data: {
cause: {
message:
'The requested coin type is not permitted. Allowed coin types must be specified in the snap manifest.',
stack: expect.any(String),
},
},
code: 4100,
message:
'The requested coin type is not permitted. Allowed coin types must be specified in the snap manifest.',
stack: expect.any(String),
});

await close();
Expand Down Expand Up @@ -166,14 +160,9 @@ describe('onRpcRequest', () => {
await ui.cancel();

expect(await response).toRespondWithError({
code: -32603,
message: 'Internal JSON-RPC error.',
data: {
cause: {
message: 'User rejected the request.',
stack: expect.any(String),
},
},
code: 4001,
message: 'User rejected the request.',
stack: expect.any(String),
});

await close();
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/browserify-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^5.1.1",
"@metamask/rpc-errors": "^6.1.0",
"@metamask/snaps-types": "workspace:^"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "uAC3J0T7DOshJ+th+YB9RxUNTx/5I1XLgcJqHkTB6Ek=",
"shasum": "UmRhDLrx9n3NZ/qrj0O8kdRLXgb3rLKd4hMeSWaK61o=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
11 changes: 5 additions & 6 deletions packages/examples/packages/browserify-plugin/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ describe('onRpcRequest', () => {
});

expect(response).toRespondWithError({
code: -32603,
message: 'Internal JSON-RPC error.',
code: -32601,
message: 'The method does not exist / is not available.',
stack: expect.any(String),
data: {
cause: {
message: 'The method does not exist / is not available.',
stack: expect.any(String),
},
method: 'foo',
cause: null,
},
});

Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/browserify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^5.1.1",
"@metamask/rpc-errors": "^6.1.0",
"@metamask/snaps-types": "workspace:^"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/browserify/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "vdovVqAuPpZUhBv7sp9jeWcbswewQ+b1/DmLtqMzCo4=",
"shasum": "ARCsvmYbTNhSuYkKrCGu7Js9UKIy1x0+tMconKGJ5/U=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
11 changes: 5 additions & 6 deletions packages/examples/packages/browserify/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ describe('onRpcRequest', () => {
});

expect(response).toRespondWithError({
code: -32603,
message: 'Internal JSON-RPC error.',
code: -32601,
message: 'The method does not exist / is not available.',
stack: expect.any(String),
data: {
cause: {
message: 'The method does not exist / is not available.',
stack: expect.any(String),
},
method: 'foo',
cause: null,
},
});

Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/cronjobs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^5.1.1",
"@metamask/rpc-errors": "^6.1.0",
"@metamask/snaps-types": "workspace:^",
"@metamask/snaps-ui": "workspace:^"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/cronjobs/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "IdCbn1U58HdyfhTTSg2i6cYEmxe10PFyaq9b/h2vJF4=",
"shasum": "cak4KyQA/ZFVTwJjOAmWw3df6z4XJnD+KTd1yZVZQdo=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/dialogs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^5.1.1",
"@metamask/rpc-errors": "^6.1.0",
"@metamask/snaps-types": "workspace:^",
"@metamask/snaps-ui": "workspace:^"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/dialogs/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "ESPW+UbPbHYlyKxEc4UHB/GviiwDyzAkf+HdJsHieG0=",
"shasum": "5088QO3qlZ8bJEDRjmeQaQX58F0WOX/us8kjdCuVNBc=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
11 changes: 5 additions & 6 deletions packages/examples/packages/dialogs/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ describe('onRpcRequest', () => {
});

expect(response).toRespondWithError({
code: -32603,
message: 'Internal JSON-RPC error.',
code: -32601,
message: 'The method does not exist / is not available.',
stack: expect.any(String),
data: {
cause: {
message: 'The method does not exist / is not available.',
stack: expect.any(String),
},
method: 'foo',
cause: null,
},
});

Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/ethereum-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^5.1.1",
"@metamask/rpc-errors": "^6.1.0",
"@metamask/snaps-types": "workspace:^",
"@metamask/utils": "^8.1.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "Oyxv5Zjx6cNafk2unlOO0QvIaMhrvFGldQAarIYhEvc=",
"shasum": "SxE+1qPN5yMQft8XQ2fzE8zVytVNwBOUbysNM0Xzro8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
11 changes: 5 additions & 6 deletions packages/examples/packages/ethereum-provider/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ describe('onRpcRequest', () => {
});

expect(response).toRespondWithError({
code: -32603,
message: 'Internal JSON-RPC error.',
code: -32601,
message: 'The method does not exist / is not available.',
stack: expect.any(String),
data: {
cause: {
message: 'The method does not exist / is not available.',
stack: expect.any(String),
},
method: 'foo',
cause: null,
},
});

Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/ethers-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"lint:dependencies": "depcheck"
},
"dependencies": {
"@metamask/rpc-errors": "^5.1.1",
"@metamask/rpc-errors": "^6.1.0",
"@metamask/snaps-types": "workspace:^",
"@metamask/snaps-ui": "workspace:^",
"ethers": "^6.3.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/ethers-js/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "ZL0wwUmwZ8edoJXU/IS2nQdEEguzTHDB7+BGZOZFh+c=",
"shasum": "dgCNzP9t4aozrGzU4T42NENKgKTBa+H6GOynvc0NVd0=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Loading

0 comments on commit f2e652b

Please sign in to comment.