Skip to content

Instantly share code, notes, and snippets.

@jinalshah999
Created January 30, 2021 11:49
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 jinalshah999/722be6378e8c157ce80a163ab149440d to your computer and use it in GitHub Desktop.
Save jinalshah999/722be6378e8c157ce80a163ab149440d to your computer and use it in GitHub Desktop.
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