1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00
metamask-extension/shared/modules/error.ts
Brad Decker c1f9c778c9
Add error module for safely handling errors (#17511)
Co-authored-by: Danica Shen <zhaodanica@gmail.com>
2023-02-02 13:30:57 -06:00

25 lines
655 B
TypeScript

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);
}
}