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

Added out of band error support #88

Merged
merged 4 commits into from
Sep 24, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import {
JsonRpcRequest,
PendingJsonRpcResponse,
} from 'json-rpc-engine';
import { EthereumRpcError } from 'eth-rpc-errors';
import { ExecutionEnvironmentService } from './ExecutionEnvironmentService';

export type SetupPluginProvider = (pluginName: string, stream: Duplex) => void;

type OnError = (pluginName: string, error: EthereumRpcError<unknown>) => void;
interface WorkerControllerArgs {
setupPluginProvider: SetupPluginProvider;
workerUrl: URL;
onError?: OnError;
}

interface WorkerStreams {
Expand Down Expand Up @@ -57,14 +60,21 @@ export class WebWorkerExecutionEnvironmentService

private workerToPluginMap: Map<string, string>;

constructor({ setupPluginProvider, workerUrl }: WorkerControllerArgs) {
private _onError?: OnError;

constructor({
setupPluginProvider,
workerUrl,
onError,
}: WorkerControllerArgs) {
this.workerUrl = workerUrl;
this.setupPluginProvider = setupPluginProvider;
this.store = new ObservableStore({ workers: {} });
this.workers = new Map();
this.pluginToWorkerMap = new Map();
this.workerToPluginMap = new Map();
this._pluginRpcHooks = new Map();
this._onError = onError;
}

private _setWorker(workerId: string, workerWrapper: WorkerWrapper): void {
Expand Down Expand Up @@ -233,6 +243,21 @@ export class WebWorkerExecutionEnvironmentService
const worker = new Worker(this.workerUrl, {
name: workerId,
});
// Handle out-of-band errors, i.e. errors thrown from the plugin outside of the req/res cycle.
const errorHandler = (ev: ErrorEvent) => {
if (this._onError) {
const err = new EthereumRpcError(
ev.error.code,
ev.error.message,
ev.error.data,
);
const pluginName = this.workerToPluginMap.get(workerId);
if (pluginName) {
this._onError(pluginName, err);
}
}
};
worker.addEventListener('error', errorHandler, { once: true });
const streams = this._initWorkerStreams(worker, workerId);
const rpcEngine = new JsonRpcEngine();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@metamask/snap-controllers": "^0.1.0",
"@metamask/snap-types": "^0.1.0",
"@metamask/snap-workers": "^0.1.0",
"eth-rpc-errors": "^4.0.3",
"json-rpc-engine": "^6.1.0",
"json-rpc-middleware-stream": "^3.0.0",
"nanoid": "^3.1.23",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('Iframe Controller', () => {
'https://metamask.github.io/iframe-execution-environment/',
),
});
iframeExecutionEnvironmentService.terminateAllPlugins();
expect(iframeExecutionEnvironmentService).toBeDefined();
await iframeExecutionEnvironmentService.terminateAllPlugins();
});

it('can create a plugin worker and start the plugin', async () => {
Expand All @@ -38,6 +38,7 @@ describe('Iframe Controller', () => {
});
expect(response).toStrictEqual('OK');
removeListener();
await iframeExecutionEnvironmentService.terminateAllPlugins();
});

it('can handle a crashed plugin', async () => {
Expand Down Expand Up @@ -66,6 +67,7 @@ describe('Iframe Controller', () => {
await expect(action()).rejects.toThrow(
/Error while running plugin 'TestPlugin'/u,
);
await iframeExecutionEnvironmentService.terminateAllPlugins();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added terminateAllPlugins here because when testing it wasn't cleaning up the plugin from the page

removeListener();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import {
PendingJsonRpcResponse,
} from 'json-rpc-engine';
import { ExecutionEnvironmentService } from '@metamask/snap-controllers';
import { EthereumRpcError } from 'eth-rpc-errors';

export type SetupPluginProvider = (pluginName: string, stream: Duplex) => void;

type OnError = (pluginName: string, error: EthereumRpcError<unknown>) => void;
interface IframeExecutionEnvironmentServiceArgs {
createWindowTimeout?: number;
setupPluginProvider: SetupPluginProvider;
iframeUrl: URL;
onError?: OnError;
}

interface JobStreams {
Expand Down Expand Up @@ -58,9 +61,12 @@ export class IframeExecutionEnvironmentService

private _createWindowTimeout: number;

private _onError?: OnError;

constructor({
setupPluginProvider,
iframeUrl,
onError,
createWindowTimeout = 60000,
}: IframeExecutionEnvironmentServiceArgs) {
this._createWindowTimeout = createWindowTimeout;
Expand All @@ -70,6 +76,7 @@ export class IframeExecutionEnvironmentService
this.pluginToJobMap = new Map();
this.jobToPluginMap = new Map();
this._pluginRpcHooks = new Map();
this._onError = onError;
}

private _setJob(jobId: string, jobWrapper: EnvMetadata): void {
Expand Down Expand Up @@ -262,6 +269,22 @@ export class IframeExecutionEnvironmentService
);

const commandStream = mux.createStream(PLUGIN_STREAM_NAMES.COMMAND);
// Handle out-of-band errors, i.e. errors thrown from the plugin outside of the req/res cycle.
const errorHandler = (data: any) => {
if (data.error && this._onError) {
const err = new EthereumRpcError(
data.error.code,
data.error.message,
data.error.data,
);
const pluginName = this.jobToPluginMap.get(jobId);
if (pluginName) {
this._onError(pluginName, err);
}
commandStream.off('data', errorHandler);
}
};
commandStream.on('data', errorHandler);
const rpcStream = mux.createStream(PLUGIN_STREAM_NAMES.JSON_RPC);

// Typecast: stream type mismatch
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5828,6 +5828,13 @@ eth-rpc-errors@^4.0.0, eth-rpc-errors@^4.0.2:
dependencies:
fast-safe-stringify "^2.0.6"

eth-rpc-errors@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/eth-rpc-errors/-/eth-rpc-errors-4.0.3.tgz#6ddb6190a4bf360afda82790bb7d9d5e724f423a"
integrity sha512-Z3ymjopaoft7JDoxZcEb3pwdGh7yiYMhOwm2doUt6ASXlMavpNlK6Cre0+IMl2VSGyEU9rkiperQhp5iRxn5Pg==
dependencies:
fast-safe-stringify "^2.0.6"

eth-sig-util@^1.4.0, eth-sig-util@^1.4.2:
version "1.4.2"
resolved "https://registry.yarnpkg.com/eth-sig-util/-/eth-sig-util-1.4.2.tgz#8d958202c7edbaae839707fba6f09ff327606210"
Expand Down