Skip to content

Instantly share code, notes, and snippets.

@yuriploc
Created August 27, 2018 14:16
Show Gist options
  • Save yuriploc/e1bbb0fa898dcd97e2b025979cea6e0e to your computer and use it in GitHub Desktop.
Save yuriploc/e1bbb0fa898dcd97e2b025979cea6e0e to your computer and use it in GitHub Desktop.
Custom Error
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);
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