Worth noting there are polyfills[1] for this, if you need to support older environments.
In practice, I'll usually use something like...
function MyAppError(msg, code, details) {
return Object.assign(
new Error(msg),
{code},
details
)
}
Or helper functions that do the same for assigning attributes to a baseline error... I have gone as far as to pop the top entry off the stack as well, to make it look closer to native.
1. https://www.npmjs.com/package/error.iserror
Worth noting there are polyfills[1] for this, if you need to support older environments. In practice, I'll usually use something like... function MyAppError(msg, code, details) { return Object.assign( new Error(msg), {code}, details ) } Or helper functions that do the same for assigning attributes to a baseline error... I have gone as far as to pop the top entry off the stack as well, to make it look closer to native. 1. https://www.npmjs.com/package/error.iserror