mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-22 09:23:21 +01:00
Add error module for safely handling errors (#17511)
Co-authored-by: Danica Shen <zhaodanica@gmail.com>
This commit is contained in:
parent
85a52d49db
commit
c1f9c778c9
@ -3521,7 +3521,8 @@
|
||||
"define": true,
|
||||
"document.cookie": true,
|
||||
"localStorage": true,
|
||||
"log": "write"
|
||||
"log": "write",
|
||||
"navigator": true
|
||||
}
|
||||
},
|
||||
"luxon": {
|
||||
|
@ -3845,7 +3845,8 @@
|
||||
"define": true,
|
||||
"document.cookie": true,
|
||||
"localStorage": true,
|
||||
"log": "write"
|
||||
"log": "write",
|
||||
"navigator": true
|
||||
}
|
||||
},
|
||||
"luxon": {
|
||||
|
@ -3521,7 +3521,8 @@
|
||||
"define": true,
|
||||
"document.cookie": true,
|
||||
"localStorage": true,
|
||||
"log": "write"
|
||||
"log": "write",
|
||||
"navigator": true
|
||||
}
|
||||
},
|
||||
"luxon": {
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
32
shared/modules/error.test.ts
Normal file
32
shared/modules/error.test.ts
Normal 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
24
shared/modules/error.ts
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user