Created
December 27, 2016 20:30
-
-
Save ilaif/984ca6b95f3e8fd06a407a9b1b30b048 to your computer and use it in GitHub Desktop.
Generic BaseError
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
'use strict'; | |
const logger = require('../libs/logger.lib'); | |
const ErrorLevel = require('../enums/ErrorLevel'); | |
const ErrorLevelInverse = require('../enums/ErrorLevelInverse'); | |
const BaseError = exports.BaseError = class BaseError extends Error { | |
constructor(opts) { | |
let message = opts.message || 'Unknown Error'; | |
super(message); | |
Error.captureStackTrace(this, this.constructor); | |
this.name = this.constructor.name; | |
this.level = opts.level || ErrorLevel.ERROR; | |
this.msg = message; | |
this.code = opts.code || 1; | |
this.statusCode = opts.statusCode || 500; | |
this.info = opts.info || {}; | |
this.originalError = opts.originalError; | |
logger[ErrorLevelInverse[this.level]](this.display()); | |
} | |
display() { | |
return { | |
name: this.name, | |
level: this.level, | |
msg: this.message, | |
code: this.code, | |
statusCode: this.statusCode, | |
info: this.info | |
}; | |
} | |
}; | |
exports.GeneralError = class GeneralError extends BaseError { | |
constructor(opts) { | |
super(_.merge({ | |
code: 2, | |
message: 'An unhandled exception occurred, please contact support' | |
}, opts)); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment