1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

Add error module for safely handling errors (#17511)

Co-authored-by: Danica Shen <zhaodanica@gmail.com>
This commit is contained in:
Brad Decker 2023-02-02 13:30:57 -06:00 committed by GitHub
parent 85a52d49db
commit c1f9c778c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 82 additions and 3 deletions

View File

@ -3521,7 +3521,8 @@
"define": true,
"document.cookie": true,
"localStorage": true,
"log": "write"
"log": "write",
"navigator": true
}
},
"luxon": {

View File

@ -3845,7 +3845,8 @@
"define": true,
"document.cookie": true,
"localStorage": true,
"log": "write"
"log": "write",
"navigator": true
}
},
"luxon": {

View File

@ -3521,7 +3521,8 @@
"define": true,
"document.cookie": true,
"localStorage": true,
"log": "write"
"log": "write",
"navigator": true
}
},
"luxon": {

View File

@ -49,6 +49,16 @@
"crypto": true
}
},
"eth-lattice-keyring>gridplus-sdk": {
"packages": {
"eth-lattice-keyring>gridplus-sdk>uuid": true
}
},
"eth-lattice-keyring>gridplus-sdk>uuid": {
"globals": {
"crypto": true
}
},
"ethereumjs-wallet>randombytes": {
"globals": {
"crypto.getRandomValues": true

View File

@ -346,6 +346,11 @@
"yargs>string-width": true
}
},
"chokidar": {
"packages": {
"chokidar>fsevents": true
}
},
"gulp-watch>chokidar": {
"builtin": {
"events.EventEmitter": true,
@ -526,6 +531,11 @@
},
"native": true
},
"gulp>glob-watcher>chokidar": {
"packages": {
"gulp>glob-watcher>chokidar>fsevents": true
}
},
"gulp>glob-watcher>chokidar>fsevents": {
"builtin": {
"events.EventEmitter": true,

View File

@ -0,0 +1,32 @@
import log from 'loglevel';
import { isErrorWithMessage, logErrorWithMessage } from './error';
jest.mock('loglevel');
afterEach(() => {
jest.resetAllMocks();
});
describe('error module', () => {
describe('isErrorWithMessage', () => {
it('returns true when passed an instance of an Error', () => {
expect(isErrorWithMessage(new Error('test'))).toBe(true);
});
it('returns false when passed a string', () => {
expect(isErrorWithMessage('test')).toBe(false);
});
});
describe('logErrorWithMessage', () => {
it('calls loglevel.error with the error.message when passed an instance of Error', () => {
logErrorWithMessage(new Error('test'));
expect(log.error).toBeCalledWith('test');
});
it('calls loglevel.error with the parameter passed in when parameter is not an instance of Error', () => {
logErrorWithMessage({ test: 'test' });
expect(log.error).toBeCalledWith({ test: 'test' });
});
});
});

24
shared/modules/error.ts Normal file
View File

@ -0,0 +1,24 @@
import log from 'loglevel';
/**
* Type guard for determining whether the given value is an error object with a
* `message` property, such as an instance of Error.
*
* TODO: Remove once this becomes available at @metamask/utils
*
* @param error - The object to check.
* @returns True or false, depending on the result.
*/
export function isErrorWithMessage(
error: unknown,
): error is { message: string } {
return typeof error === 'object' && error !== null && 'message' in error;
}
export function logErrorWithMessage(error: unknown) {
if (isErrorWithMessage(error)) {
log.error(error.message);
} else {
log.error(error);
}
}