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

Fix snaps-controllers and snaps-execution-environments tests #1835

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"branches": 89.28,
"branches": 89.39,
"functions": 95.6,
"lines": 96.97,
"statements": 96.64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import type {
JsonRpcRequest,
PendingJsonRpcResponse,
} from '@metamask/utils';
import { Duration, isJsonRpcNotification, isObject } from '@metamask/utils';
import {
Duration,
hasProperty,
isJsonRpcNotification,
isObject,
} from '@metamask/utils';
import { createStreamMiddleware } from 'json-rpc-middleware-stream';
import { nanoid } from 'nanoid';
import { pipeline } from 'stream';
Expand Down Expand Up @@ -47,6 +52,10 @@ export type Job<WorkerType> = {
worker: WorkerType;
};

export class ExecutionEnvironmentError extends Error {
cause?: Json;
}

export abstract class AbstractExecutionService<WorkerType>
implements ExecutionService
{
Expand Down Expand Up @@ -373,12 +382,23 @@ export abstract class AbstractExecutionService<WorkerType>
}

log('Parent: Sending Command', message);
const response: PendingJsonRpcResponse<Json> =
// eslint-disable-next-line @typescript-eslint/await-thenable
await job.rpcEngine.handle(message);
const response: PendingJsonRpcResponse<Json> = await job.rpcEngine.handle(
message,
);

if (response.error) {
throw new Error(response.error.message);
const error = new ExecutionEnvironmentError(response.error.message);
if (
isObject(response.error.data) &&
hasProperty(response.error.data, 'cause') &&
response.error.data.cause !== null
) {
error.cause = response.error.data.cause;
}

throw error;
}

return response.result;
}

Expand Down
1 change: 1 addition & 0 deletions packages/snaps-controllers/src/services/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('browser entrypoint', () => {
'OffscreenExecutionService',
'WebWorkerExecutionService',
'ProxyPostMessageStream',
'ExecutionEnvironmentError',
];

it('entrypoint has expected exports', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { SnapId } from '@metamask/snaps-utils';
import { HandlerType } from '@metamask/snaps-utils';

import { createService, MOCK_BLOCK_NUMBER } from '../../test-utils';
import { ExecutionEnvironmentError } from '../AbstractExecutionService';
import type { SnapErrorJson } from '../ExecutionService';
import { NodeProcessExecutionService } from './NodeProcessExecutionService';

Expand Down Expand Up @@ -47,7 +48,7 @@ describe('NodeProcessExecutionService', () => {
});

it('can handle errors in request handler', async () => {
expect.assertions(1);
expect.assertions(2);
const { service } = createService(NodeProcessExecutionService);
const snapId = 'TestSnap';
await service.executeSnap({
Expand All @@ -58,8 +59,8 @@ describe('NodeProcessExecutionService', () => {
endowments: [],
});

await expect(
service.handleRpcRequest(snapId, {
const result = await service
.handleRpcRequest(snapId, {
origin: 'fooOrigin',
handler: ON_RPC_REQUEST,
request: {
Expand All @@ -68,8 +69,18 @@ describe('NodeProcessExecutionService', () => {
params: {},
id: 1,
},
}),
).rejects.toThrow('foobar');
})
.catch((error) => error);

expect(result).toBeInstanceOf(ExecutionEnvironmentError);

// eslint-disable-next-line jest/prefer-strict-equal
expect((result as ExecutionEnvironmentError).cause).toEqual({
FrederikBolding marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Unwrap errors, and change this to the actual error message.
message: 'foobar',
stack: expect.any(String),
});

await service.terminateAllSnaps();
});

Expand Down Expand Up @@ -126,9 +137,10 @@ describe('NodeProcessExecutionService', () => {
code: -32603,
data: {
snapId: 'TestSnap',
stack: expect.any(String),
FrederikBolding marked this conversation as resolved.
Show resolved Hide resolved
stack: expect.stringContaining('Error: random error inside'),
},
message: 'random error inside',
// TODO: Unwrap errors, and change this to the actual error message.
message: 'Execution Environment Error',
});

await service.terminateAllSnaps();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { SnapId } from '@metamask/snaps-utils';
import { HandlerType } from '@metamask/snaps-utils';

import { createService, MOCK_BLOCK_NUMBER } from '../../test-utils';
import { ExecutionEnvironmentError } from '../AbstractExecutionService';
import type { SnapErrorJson } from '../ExecutionService';
import { NodeThreadExecutionService } from './NodeThreadExecutionService';

Expand Down Expand Up @@ -47,7 +48,7 @@ describe('NodeThreadExecutionService', () => {
});

it('can handle errors in request handler', async () => {
expect.assertions(1);
expect.assertions(2);
const { service } = createService(NodeThreadExecutionService);
const snapId = 'TestSnap';
await service.executeSnap({
Expand All @@ -58,8 +59,8 @@ describe('NodeThreadExecutionService', () => {
endowments: [],
});

await expect(
service.handleRpcRequest(snapId, {
const result = await service
.handleRpcRequest(snapId, {
origin: 'fooOrigin',
handler: ON_RPC_REQUEST,
request: {
Expand All @@ -68,8 +69,18 @@ describe('NodeThreadExecutionService', () => {
params: {},
id: 1,
},
}),
).rejects.toThrow('foobar');
})
.catch((error) => error);

expect(result).toBeInstanceOf(ExecutionEnvironmentError);

// eslint-disable-next-line jest/prefer-strict-equal
expect((result as ExecutionEnvironmentError).cause).toEqual({
// TODO: Unwrap errors, and change this to the actual error message.
message: 'foobar',
FrederikBolding marked this conversation as resolved.
Show resolved Hide resolved
stack: expect.any(String),
});

await service.terminateAllSnaps();
});

Expand Down Expand Up @@ -126,9 +137,10 @@ describe('NodeThreadExecutionService', () => {
code: -32603,
data: {
snapId: 'TestSnap',
stack: expect.any(String),
stack: expect.stringContaining('Error: random error inside'),
FrederikBolding marked this conversation as resolved.
Show resolved Hide resolved
},
message: 'random error inside',
// TODO: Unwrap errors, and change this to the actual error message.
message: 'Execution Environment Error',
});

await service.terminateAllSnaps();
Expand Down
6 changes: 3 additions & 3 deletions packages/snaps-execution-environments/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 80.41,
"branches": 79.72,
"functions": 91.91,
"lines": 91.41,
"statements": 91.12
"lines": 91.08,
"statements": 90.8
}
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ describe('BaseSnapExecutor', () => {
jsonrpc: '2.0',
error: {
code: -32603,
message: expect.stringContaining('undefined'),
// TODO: Unwrap errors, and change this to the actual error message.
message: 'Execution Environment Error',
data: expect.any(Object),
},
id: 2,
Expand Down Expand Up @@ -1022,7 +1023,8 @@ describe('BaseSnapExecutor', () => {
stack: error.stack,
snapId: MOCK_SNAP_ID,
},
message: error.message,
// TODO: Unwrap errors, and change this to the actual error message.
message: 'Execution Environment Error',
},
},
});
Expand Down Expand Up @@ -1082,7 +1084,8 @@ describe('BaseSnapExecutor', () => {
stack: error.stack,
snapId: MOCK_SNAP_ID,
},
message: error.message,
// TODO: Unwrap errors, and change this to the actual error message.
message: 'Execution Environment Error',
},
},
});
Expand Down Expand Up @@ -1388,7 +1391,8 @@ describe('BaseSnapExecutor', () => {
error: {
code: -32603,
data: expect.anything(),
message: expect.stringContaining('undefined'),
// TODO: Unwrap errors, and change this to the actual error message.
message: 'Execution Environment Error',
},
});

Expand Down Expand Up @@ -1744,7 +1748,8 @@ describe('BaseSnapExecutor', () => {
error: {
code: -32603,
data: expect.any(Object),
message: 'JSON-RPC responses must be JSON serializable objects.',
// TODO: Unwrap errors, and change this to the actual error message.
message: 'Execution Environment Error',
},
});
});
Expand Down