Skip to content
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
4 changes: 4 additions & 0 deletions packages/multichain-account-service/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Filter out `KeyringController` locked errors from sentry reporting ([#8619](https://github.com/MetaMask/core/pull/8619))

### Changed

- Bump `@metamask/accounts-controller` from `^37.1.1` to `^37.2.0` ([#8363](https://github.com/MetaMask/core/pull/8363))
Expand Down
71 changes: 71 additions & 0 deletions packages/multichain-account-service/src/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
KeyringControllerError,
KeyringControllerErrorMessage,
} from '@metamask/keyring-controller';

import { reportError } from './errors';
import { logErrorAs } from './logger';
import { TimeoutError } from './providers/utils';

jest.mock('./logger', () => ({
logErrorAs: jest.fn(),
}));

describe('reportError', () => {
const message = 'Unable to create account';

beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation();
jest.spyOn(console, 'error').mockImplementation();
});

it.each([
{
name: 'timeout errors',
error: new TimeoutError('Timed out after: 500ms'),
},
{
name: 'keyring controller locked errors',
error: new KeyringControllerError(
KeyringControllerErrorMessage.ControllerLocked,
),
},
])('logs $name as warnings without capturing them', ({ error }) => {
const messenger = { captureException: jest.fn() };

reportError(messenger, message, error);

expect(logErrorAs).toHaveBeenCalledWith('warn', message, error);
expect(console.warn).toHaveBeenCalledWith(message, error);
expect(console.error).not.toHaveBeenCalled();
expect(messenger.captureException).not.toHaveBeenCalled();
});

it('logs unexpected errors and captures them with context', () => {
const error = new Error('Something went wrong');
const context = { accountId: 'account-id' };
const messenger = { captureException: jest.fn() };

reportError(messenger, message, error, context);

expect(logErrorAs).toHaveBeenCalledWith('error', message, error);
expect(console.error).toHaveBeenCalledWith(message, error);
expect(console.warn).not.toHaveBeenCalled();
expect(messenger.captureException).toHaveBeenCalledWith(
expect.objectContaining({
message,
cause: error,
context,
}),
);
});

it('does not throw if captureException is not provided', () => {
const error = new Error('Something went wrong');

expect(() => reportError({}, message, error)).not.toThrow();

expect(logErrorAs).toHaveBeenCalledWith('error', message, error);
expect(console.error).toHaveBeenCalledWith(message, error);
});
});
7 changes: 5 additions & 2 deletions packages/multichain-account-service/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { logErrorAs } from './logger';
import { isTimeoutError } from './providers/utils';
import {
isKeyringControllerLockedError,
isTimeoutError,
} from './providers/utils';
import { createSentryError } from './utils';

/**
Expand All @@ -20,7 +23,7 @@ export function reportError(
error: unknown,
context?: Record<string, unknown>,
): void {
if (isTimeoutError(error)) {
if (isTimeoutError(error) || isKeyringControllerLockedError(error)) {
Comment thread
hmalik88 marked this conversation as resolved.
logErrorAs('warn', message, error);
console.warn(message, error);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { TimeoutError, isTimeoutError, withRetry, withTimeout } from './utils';
import {
KeyringControllerError,
KeyringControllerErrorMessage,
} from '@metamask/keyring-controller';

import {
TimeoutError,
isKeyringControllerLockedError,
isTimeoutError,
withRetry,
withTimeout,
} from './utils';

describe('utils', () => {
it('retries RPC request up to 3 times if it fails and throws the last error', async () => {
Expand Down Expand Up @@ -60,4 +71,21 @@ describe('utils', () => {
expect(isTimeoutError('string')).toBe(false);
expect(isTimeoutError(null)).toBe(false);
});

it('isKeyringControllerLockedError returns true for KeyringControllerLockedError instances', () => {
expect(
isKeyringControllerLockedError(
new KeyringControllerError(
KeyringControllerErrorMessage.ControllerLocked,
),
),
).toBe(true);
});

it.each([new Error('some error'), 'string', null])(
'isKeyringControllerLockedError returns false for %p',
(error) => {
expect(isKeyringControllerLockedError(error)).toBe(false);
},
);
});
18 changes: 18 additions & 0 deletions packages/multichain-account-service/src/providers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {
KeyringControllerError,
KeyringControllerErrorMessage,
} from '@metamask/keyring-controller';

/** Timeout error. */
export class TimeoutError extends Error {
constructor(message: string) {
Expand All @@ -16,6 +21,19 @@ export function isTimeoutError(error: unknown): error is TimeoutError {
return error instanceof TimeoutError;
}

/**
* Check if an error is a `KeyringControllerLockedError`.
*
* @param error - The error to check.
* @returns `true` if the error is a `KeyringControllerLockedError`, otherwise `false`.
*/
export function isKeyringControllerLockedError(error: unknown): boolean {
Comment thread
hmalik88 marked this conversation as resolved.
return (
error instanceof KeyringControllerError &&
error.message === KeyringControllerErrorMessage.ControllerLocked
);
}

/**
* Execute a function with exponential backoff on transient failures.
*
Expand Down
Loading