Skip to content

Commit 1325997

Browse files
committed
Add error handling for MetaMetrics hook
1 parent a28d1d7 commit 1325997

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

packages/snaps-controllers/src/snaps/SnapController.test.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9522,6 +9522,52 @@ describe('SnapController', () => {
95229522
expect(mockTrackEvent).not.toHaveBeenCalled();
95239523
snapController.destroy();
95249524
});
9525+
9526+
it('should properly handle error when MetaMetrics hook throws an error', async () => {
9527+
const log = jest.spyOn(console, 'error').mockImplementation();
9528+
const error = new Error('MetaMetrics hook error');
9529+
const mockTrackEvent = jest.fn().mockImplementation(() => {
9530+
throw error;
9531+
});
9532+
const rootMessenger = getControllerMessenger();
9533+
const executionEnvironmentStub = new ExecutionEnvironmentStub(
9534+
getNodeEESMessenger(rootMessenger),
9535+
) as unknown as NodeThreadExecutionService;
9536+
9537+
const [snapController] = getSnapControllerWithEES(
9538+
getSnapControllerWithEESOptions({
9539+
rootMessenger,
9540+
trackEvent: mockTrackEvent,
9541+
state: {
9542+
snaps: getPersistedSnapsState(),
9543+
},
9544+
}),
9545+
executionEnvironmentStub,
9546+
);
9547+
9548+
const snap = snapController.getExpect(MOCK_SNAP_ID);
9549+
await snapController.startSnap(snap.id);
9550+
9551+
await snapController.handleRequest({
9552+
snapId: snap.id,
9553+
origin: MOCK_ORIGIN,
9554+
handler: HandlerType.OnRpcRequest,
9555+
request: {
9556+
jsonrpc: '2.0',
9557+
method: 'test',
9558+
params: {},
9559+
id: 1,
9560+
},
9561+
});
9562+
9563+
expect(mockTrackEvent).toHaveBeenCalled();
9564+
expect(log).toHaveBeenCalledWith(
9565+
expect.stringContaining(
9566+
'Error when calling MetaMetrics hook for snap',
9567+
),
9568+
);
9569+
snapController.destroy();
9570+
});
95259571
});
95269572

95279573
it('handles a transaction insight request', async () => {

packages/snaps-controllers/src/snaps/SnapController.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3927,7 +3927,15 @@ export class SnapController extends BaseController<
39273927
const snap = this.get(snapId);
39283928

39293929
if (isTrackableHandler(handlerType) && !snap?.preinstalled) {
3930-
this.#trackSnapExport(snapId, handlerType, success, origin);
3930+
try {
3931+
this.#trackSnapExport(snapId, handlerType, success, origin);
3932+
} catch (error) {
3933+
logError(
3934+
`Error when calling MetaMetrics hook for snap "${snap?.id}": ${getErrorMessage(
3935+
error,
3936+
)}`,
3937+
);
3938+
}
39313939
}
39323940
}
39333941

0 commit comments

Comments
 (0)