I have a function that might return some object or might return a custom error object. I am unable to detect error object type.
I have tried constructor.match(/Error/i) or tried to work on Object.keys of prototype but nothing worked. Following is the code?
function x() {
try {
...
} catch {e} {
let err = new Error('caught error')
return err
}
return someObject
}
//the following gives: TypeError: err.constructor.match is not a function
if (x().constructor.match(/Error/i)) {
//log error
}
//do something
Any ideas how to detect the output error type?
You can check if returned object is an instanceof
Error as follows
let y = x();
if(y instanceof Error) {
// returned object is error
} else {
//returned object is not error
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With