Last active
March 27, 2018 15:48
-
-
Save dlueth/f4a49c7caa974de4e3a29f1e9cae7bec to your computer and use it in GitHub Desktop.
NodeJS: Custom errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
class CustomError extends Error { | |
constructor(...args) { | |
super(...args); | |
Object.defineProperty(this, 'name', { value: this.constructor.name }); | |
// This is for Node only... | |
Error.captureStackTrace(this, this.constructor); | |
// ... whereas use this for Browser instead | |
/* | |
if(Error.captureStackTrace) { | |
Error.captureStackTrace(this, CustomError); | |
} | |
*/ | |
} | |
} | |
class ExtendedError extends CustomError { | |
constructor(...args) { | |
super(...args); | |
} | |
} | |
try { | |
throw new ExtendedError('gnarf') | |
} catch(err) { | |
console.log(err.name); | |
console.log(err); | |
} | |
module.exports = CustomError; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment