Skip to content

Commit

Permalink
[errors] ensure that error.status is always a number when defined
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Jan 5, 2016
1 parent 964b7ae commit 0e3fe15
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 47 deletions.
95 changes: 48 additions & 47 deletions src/lib/errors.js
Expand Up @@ -92,53 +92,54 @@ errors.RequestTypeError = function RequestTypeError(feature) {
};
_.inherits(errors.RequestTypeError, ErrorAbstract);

var statusCodes = {
300: 'Multiple Choices',
301: 'Moved Permanently',
302: 'Found',
303: 'See Other',
304: 'Not Modified',
305: 'Use Proxy',
307: 'Temporary Redirect',
308: 'Permanent Redirect',
400: 'Bad Request',
401: 'Authentication Exception',
402: 'Payment Required',
403: ['Authorization Exception', 'Forbidden'],
404: 'Not Found',
405: 'Method Not Allowed',
406: 'Not Acceptable',
407: 'Proxy Authentication Required',
408: 'Request Timeout',
409: 'Conflict',
410: 'Gone',
411: 'Length Required',
412: 'Precondition Failed',
413: 'Request Entity Too Large',
414: 'Request URIToo Long',
415: 'Unsupported Media Type',
416: 'Requested Range Not Satisfiable',
417: 'Expectation Failed',
418: 'Im ATeapot',
421: 'Too Many Connections From This IP',
426: 'Upgrade Required',
429: 'Too Many Requests',
450: 'Blocked By Windows Parental Controls',
494: 'Request Header Too Large',
497: 'HTTPTo HTTPS',
499: 'Client Closed Request',

500: 'Internal Server Error',
501: 'Not Implemented',
502: 'Bad Gateway',
503: 'Service Unavailable',
504: 'Gateway Timeout',
505: 'HTTPVersion Not Supported',
506: 'Variant Also Negotiates',
510: 'Not Extended'
};

_.each(statusCodes, function createStatusCodeError(names, status) {
var statusCodes = [
[ 300, 'Multiple Choices' ],
[ 301, 'Moved Permanently' ],
[ 302, 'Found' ],
[ 303, 'See Other' ],
[ 304, 'Not Modified' ],
[ 305, 'Use Proxy' ],
[ 307, 'Temporary Redirect' ],
[ 308, 'Permanent Redirect' ],
[ 400, 'Bad Request' ],
[ 401, 'Authentication Exception' ],
[ 402, 'Payment Required' ],
[ 403, ['Authorization Exception', 'Forbidden'] ],
[ 404, 'Not Found' ],
[ 405, 'Method Not Allowed' ],
[ 406, 'Not Acceptable' ],
[ 407, 'Proxy Authentication Required' ],
[ 408, 'Request Timeout' ],
[ 409, 'Conflict' ],
[ 410, 'Gone' ],
[ 411, 'Length Required' ],
[ 412, 'Precondition Failed' ],
[ 413, 'Request Entity Too Large' ],
[ 414, 'Request URIToo Long' ],
[ 415, 'Unsupported Media Type' ],
[ 416, 'Requested Range Not Satisfiable' ],
[ 417, 'Expectation Failed' ],
[ 418, 'Im ATeapot' ],
[ 421, 'Too Many Connections From This IP' ],
[ 426, 'Upgrade Required' ],
[ 429, 'Too Many Requests' ],
[ 450, 'Blocked By Windows Parental Controls' ],
[ 494, 'Request Header Too Large' ],
[ 497, 'HTTPTo HTTPS' ],
[ 499, 'Client Closed Request' ],
[ 500, 'Internal Server Error' ],
[ 501, 'Not Implemented' ],
[ 502, 'Bad Gateway' ],
[ 503, 'Service Unavailable' ],
[ 504, 'Gateway Timeout' ],
[ 505, 'HTTPVersion Not Supported' ],
[ 506, 'Variant Also Negotiates' ],
[ 510, 'Not Extended' ]
];

_.each(statusCodes, function createStatusCodeError(tuple) {
var status = tuple[0];
var names = tuple[1];
var allNames = [].concat(names, status);
var primaryName = allNames[0];
var className = _.studlyCase(primaryName);
Expand Down
8 changes: 8 additions & 0 deletions test/unit/specs/errors.js
Expand Up @@ -24,3 +24,11 @@ describe('Error Abstract', function () {
expect(err.stack).to.be.a('string');
});
});

describe('StatusCodeError', function () {
it('exposes status code as a number', function () {
var err = new errors['404']();
expect(err.status).to.be(404);
expect(err.status).to.not.be('404');
});
});

0 comments on commit 0e3fe15

Please sign in to comment.