Skip to content

Instantly share code, notes, and snippets.

@yashprit
Created October 12, 2014 09:35
Show Gist options
  • Save yashprit/8423f773552e7a8a8b8e to your computer and use it in GitHub Desktop.
Save yashprit/8423f773552e7a8a8b8e to your computer and use it in GitHub Desktop.
Error handling in NodeJS
//sub object, filename = badrequest.js
var Exception = require('./exception')
, inherits = require('util').inherits;
function BadRequest(message, uiMessage){
var code = 400;
Exception.call(this, code, message, uiMessage);
}
inherits(BadRequest, Exception);
BadRequest.prototype.name = "BadRequest";
module.exports = BadRequest;
//super object, filename exception.js
function HttpError(code, message){
this.message = message;
this.stack = new Error().stack;
this.uiMessage = uiMessage;
this.type = this.name;
this.code = code;
}
HttpError.prototype = Object.create(Error.prototype);
HttpError.prototype.printStackTrace = function printStackTrace(){
return this;
}
module.exports = HttpError;
var BadRequest = require('bad_request.js');
//useage, Application is constant object, contains ui related message
Promises.reject(new BadRequest(error.message, Application.ModuleName.usernameNotFound)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment