Created
August 12, 2017 02:41
-
-
Save erikologic/f499c4697a5f3e650f37def3ec8acc22 to your computer and use it in GitHub Desktop.
CustomError in NodeJS
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
MyCustomError | |
true | |
MyCustomError: message | |
at Object.<anonymous> (......./Errors.test.js:4:9) | |
at Module._compile (module.js:571:32) | |
at Object.Module._extensions..js (module.js:580:10) | |
at Module.load (module.js:488:32) | |
at tryModuleLoad (module.js:447:12) | |
at Function.Module._load (module.js:439:3) | |
at Module.runMain (module.js:605:10) | |
at run (bootstrap_node.js:427:7) | |
at startup (bootstrap_node.js:151:9) | |
at bootstrap_node.js:542:3 | |
MyCustomError | |
at Object.<anonymous> (......./Errors.test.js:13:9) | |
at Module._compile (module.js:571:32) | |
at Object.Module._extensions..js (module.js:580:10) | |
at Module.load (module.js:488:32) | |
at tryModuleLoad (module.js:447:12) | |
at Function.Module._load (module.js:439:3) | |
at Module.runMain (module.js:605:10) | |
at run (bootstrap_node.js:427:7) | |
at startup (bootstrap_node.js:151:9) | |
at bootstrap_node.js:542:3 |
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
module.exports = function CustomError(message) { | |
this.name = this.constructor.name | |
this.message = message | |
Error.captureStackTrace(this, this.constructor) | |
} | |
module.exports.prototype.inspect = function () { | |
return this.stack | |
} |
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
class MyCustomError extends require('./Errors') {} | |
try { | |
throw new MyCustomError ('message') | |
} catch (err) { | |
console.log(err.constructor.name); | |
console.log(err instanceof MyCustomError) | |
console.log(err); | |
} | |
try { | |
throw new MyCustomError () | |
} catch (err) { | |
console.log(err); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi