Last active
December 18, 2015 08:59
-
-
Save discretepackets/5758490 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
var util = require('util'); | |
var _ = require('underscore'); | |
httpError('NotFound', 'Not Found', 404); | |
httpError('Server', 'There was an error', 500); | |
httpError('Unauthorized', 'You must log in to access this', 401); | |
httpError('Forbidden', 'You do not have permission to access this', 403); | |
httpError('BadRequest', 'There was an error with your input', 400); | |
function httpError(name, default_msg, status_code) { | |
var fn = function(err, opts) { | |
if (util.isError(err)) { | |
_.extend(this, err); | |
if (!this.message) this.message = default_msg; | |
} else if (_.isString(err)) { | |
this.message = err; | |
} else { | |
this.message = default_msg; | |
} | |
this.status_code = status_code; | |
if (!util.isError(err) && !_.isString(err)) opts = err; | |
if (opts) this.layout = opts.layout; | |
Error.captureStackTrace(this, arguments.callee); | |
} | |
util.inherits(fn, Error); | |
fn.prototype.name = name; | |
global[name + 'Error'] = fn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment