Created
August 27, 2018 14:16
-
-
Save yuriploc/e1bbb0fa898dcd97e2b025979cea6e0e to your computer and use it in GitHub Desktop.
Custom Error
This file contains 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
function CustomError(message, name='CustomError', code=500) { | |
const error = Error.call(this, message); | |
this.name = name; | |
this.code = code, | |
this.message = error.message; | |
this.stack = error.stack; | |
} | |
CustomError.prototype = Object.create(Error.prototype); | |
CustomError.prototype.constructor = CustomError; | |
const customError = new CustomError('some error message','cant-find-obj', 401); | |
console.log(customError.name + ' => cant-find-obj'); | |
console.log(customError.message + ' => some error message'); | |
console.log(customError.code + ' => 401'); | |
console.log((customError instanceof Error) + ' => true'); | |
console.log((customError instanceof CustomError) + ' => true'); | |
console.log('constructor: ' + customError.constructor); | |
console.log('toString: ' + customError.toString()); | |
console.log('stack: ' + customError.stack); |
This file contains 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
var util = require('util'); | |
function MyError(message, code) { | |
this.message = message; | |
this.code = code; | |
Error.captureStackTrace(this, MyError); | |
} | |
util.inherits(MyError, Error); | |
MyError.prototype.name = 'MyError'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment