mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-26 09:46:15 -04:00
25 lines
437 B
TypeScript
25 lines
437 B
TypeScript
![]() |
import _ from 'lodash';
|
||
|
|
||
|
export { getErrorMessageIfThrows };
|
||
|
|
||
|
function getErrorMessageIfThrows(cb: () => unknown) {
|
||
|
try {
|
||
|
cb();
|
||
|
return undefined;
|
||
|
} catch (err) {
|
||
|
if (_.isString(err)) {
|
||
|
return err;
|
||
|
}
|
||
|
|
||
|
if (_.isError(err)) {
|
||
|
return err.message;
|
||
|
}
|
||
|
|
||
|
if (_.isObject(err) && _.has(err, 'message')) {
|
||
|
return (err as { message: string }).message;
|
||
|
}
|
||
|
|
||
|
return 'An error as occurred.';
|
||
|
}
|
||
|
}
|