Created
January 30, 2021 11:49
-
-
Save jinalshah999/722be6378e8c157ce80a163ab149440d to your computer and use it in GitHub Desktop.
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
const util = require('util'); | |
const EXCEPTION_MESSAGES = require('./exceptions.json'); | |
class Exception { | |
constructor(errorName, params) { | |
Error.captureStackTrace(this, Exception); | |
this.errorName = errorName; | |
this.params = params; | |
this.code = EXCEPTION_MESSAGES[errorName]['code']; | |
this.httpCode = EXCEPTION_MESSAGES[errorName]['httpCode']; | |
this.errorMessage = EXCEPTION_MESSAGES[errorName]['message']; | |
if (params !== undefined && params != null && typeof params !== 'string') { | |
for (const key in params) { | |
if (params[key]) { | |
const regExp = new RegExp('{' + key + '}', 'g'); | |
this.errorMessage = this.errorMessage.replace(regExp, params[key]); | |
} | |
} | |
} else if (params !== undefined) { | |
this.errorMessage = params; | |
} | |
this.stack_trace = this.stack; | |
switch (EXCEPTION_MESSAGES[errorName]['log']) { | |
case 'log': | |
console.log(this); | |
break; | |
case 'error': | |
console.error(this); | |
break; | |
} | |
} | |
getError() { | |
return { | |
'Code': this.code, | |
'Name': this.errorName, | |
'Message': this.errorMessage | |
}; | |
} | |
} | |
util.inherits(Exception, Error) | |
module.exports = Exception; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment