The value to normalize.
A normalized Error instance.
This function converts any thrown value into a proper Error object.
It preserves the original message when possible and attaches the
original value for debugging purposes.
Behavior:
Error, it is returned as-is{ message: string }), it is convertedThe original value is attached to the returned error as cause.
If there is a stack trace in the provided value, it will be preserved.
| Input | Output message |
|---|---|
new Error('msg') |
'msg' |
{ message: 'msg' } |
'msg' |
'error' |
'error' |
123 |
'123' |
null |
'null' |
undefined |
'undefined' |
try {
throw { message: 'Something went wrong' };
} catch (err) {
const error = normalizeError(err);
console.log(error.message); // 'Something went wrong'
console.log(error.cause); // { message: 'Something went wrong' }
}
Normalizes an unknown value into an
Errorinstance.