Skip to content

Instantly share code, notes, and snippets.

@erikologic
Created August 12, 2017 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikologic/f499c4697a5f3e650f37def3ec8acc22 to your computer and use it in GitHub Desktop.
Save erikologic/f499c4697a5f3e650f37def3ec8acc22 to your computer and use it in GitHub Desktop.
CustomError in NodeJS
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
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
}
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);
}
@tamiratabe
Copy link

hi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment