standard-http-error
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -0,1 +1,43 @@ | ||
## 1.2.0 (Oct 4, 2015) | ||
- Restores status code names and constants to pre Node v4 times (Node v0.12) for | ||
backwards compatibility. Will release StandardHttpError.js v2 briefly with | ||
Node v4's names and constants. | ||
- Adds new status codes and constants from Node v4: | ||
Code | Name | ||
------|----- | ||
`208` | `ALREADY_REPORTED` | ||
`226` | `IM_USED` | ||
`421` | `MISDIRECTED_REQUEST` | ||
`508` | `LOOP_DETECTED` | ||
- Makes `StandardHttpError` inherit from `StandardError` so you could more | ||
easily differentiate between bugs (usually thrown as `TypeError`, | ||
`SyntaxError` et al.) from user-facing errors that you've intentionally | ||
thrown. If all of your custom errors inherit from `StandardError` | ||
([StandardError.js](https://github.com/moll/js-standard-error)), this will | ||
come in handy. | ||
```javascript | ||
var StandardError = require("standard-error") | ||
var HttpError = require("standard-http-error") | ||
function magic(n) { | ||
if (isNaN(n)) throw new TypeError("Bug! Should never be NaN!") | ||
if (n <= 0) throw new HttpError(422, "Think positively!") | ||
// ... | ||
} | ||
try { magic(42) } | ||
catch (ex) { | ||
if (ex instanceof StandardError) console.error("Uh-oh: " + ex.message) | ||
else throw ex | ||
} | ||
``` | ||
Make sure your dependencies have been deduped (see `npm dedupe`) to ensure | ||
a single `StandardError` instance (needed for `instanceof` to work) across | ||
your whole app. | ||
## 1.1.1 (Jun 4, 2015) | ||
@@ -2,0 +44,0 @@ - Sets HttpError's name as a property on its prototype for when the code gets |
@@ -1,1 +0,3 @@ | ||
module.exports = require("http").STATUS_CODES | ||
var CODES = require("http").STATUS_CODES | ||
exports = module.exports = require("./codes.json") | ||
for (var code in CODES) code in exports || (exports[code] = CODES[code]) |
@@ -13,2 +13,4 @@ { | ||
"207": "Multi-Status", | ||
"208": "Already Reported", | ||
"226": "IM Used", | ||
"300": "Multiple Choices", | ||
@@ -41,2 +43,3 @@ "301": "Moved Permanently", | ||
"418": "I'm a teapot", | ||
"421": "Misdirected Request", | ||
"422": "Unprocessable Entity", | ||
@@ -58,2 +61,3 @@ "423": "Locked", | ||
"507": "Insufficient Storage", | ||
"508": "Loop Detected", | ||
"509": "Bandwidth Limit Exceeded", | ||
@@ -60,0 +64,0 @@ "510": "Not Extended", |
@@ -14,3 +14,3 @@ exports = module.exports = HttpError | ||
HttpError.prototype = Object.create(Error.prototype, { | ||
HttpError.prototype = Object.create(StandardError.prototype, { | ||
constructor: {value: HttpError, configurable: true, writable: true} | ||
@@ -17,0 +17,0 @@ }) |
{ | ||
"name": "standard-http-error", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Standard HTTP error class. Proper serialization, no bloat. Extensible.", | ||
@@ -45,4 +45,5 @@ "keywords": [ | ||
"oolong": ">= 1.7.0 < 2", | ||
"http-codes": ">= 1.0.0 < 2" | ||
"http-codes": ">= 1.0.0 < 2", | ||
"semver": ">= 5 < 6" | ||
} | ||
} |
StandardHttpError.js | ||
==================== | ||
[![NPM version][npm-badge]](https://www.npmjs.com/package/standard-http-error) | ||
[![Build status][travis-badge]](https://travis-ci.org/moll/js-standard-http-error) | ||
**StandardHttpError.js** is a very simple but useful **error class** for | ||
JavaScript and Node.js that represents HTTP errors. You can then detect it with | ||
`instanceof` in error handling middleware and act accordingly. | ||
`instanceof` in error handling middleware and act accordingly. Also works in the | ||
browser through [Browserify](http://browserify.org). | ||
@@ -14,2 +16,3 @@ You can use StandardHttpError.js with any error code you like, standardized or | ||
[npm-badge]: https://img.shields.io/npm/v/standard-http-error.svg | ||
[travis-badge]: https://travis-ci.org/moll/js-standard-http-error.png?branch=master | ||
@@ -53,4 +56,3 @@ | ||
StandardHttpError.js also supports passing a constant name instead of the error | ||
code. Constant names are taken from Node.js's `Http` module's `STATUS_CODES` | ||
object so they'll always be up to date. | ||
code. | ||
@@ -159,5 +161,6 @@ ```javascript | ||
When running on Node.js, the names are generated during run-time from | ||
`Http.STATUS_CODES`, so they'll always be up to date. Here's the list for | ||
reference as of Node v0.12: | ||
When running on Node.js, cached status codes and their names get merged with new | ||
codes from `Http.STATUS_CODES`. Existing status codes will not be changed | ||
without bumping StandardHttpError.js's major version number. That ensures | ||
consistent constants in Node and in the browser. | ||
@@ -177,2 +180,4 @@ Code | Name | ||
`207` | `MULTI_STATUS` | ||
`208` | `ALREADY_REPORTED` | ||
`226` | `IM_USED` | ||
`300` | `MULTIPLE_CHOICES` | ||
@@ -205,2 +210,3 @@ `301` | `MOVED_PERMANENTLY` | ||
`418` | `IM_A_TEAPOT` | ||
`421` | `MISDIRECTED_REQUEST` | ||
`422` | `UNPROCESSABLE_ENTITY` | ||
@@ -222,2 +228,3 @@ `423` | `LOCKED` | ||
`507` | `INSUFFICIENT_STORAGE` | ||
`508` | `LOOP_DETECTED` | ||
`509` | `BANDWIDTH_LIMIT_EXCEEDED` | ||
@@ -224,0 +231,0 @@ `510` | `NOT_EXTENDED` |
@@ -1,9 +0,12 @@ | ||
var $ = require("oolong") | ||
var O = require("oolong") | ||
var isVersion = require("semver").satisfies.bind(null, process.version) | ||
var describeNodeV012 = isVersion(">= 0.12 < 0.13") ? describe : xdescribe | ||
var CODES_JSON = require("../codes.json") | ||
var STATUS_CODES = require("http").STATUS_CODES | ||
describe("codes.json", function() { | ||
describeNodeV012("codes.json", function() { | ||
// Test for superset rather than equivalence because future Node versions | ||
// might _add_ status codes. I presume they won't remove any though. | ||
$.each(STATUS_CODES, function(name, code) { | ||
// might _add_ status codes. During the switch from Node 0.12 to Node v4 some | ||
// names _were_ changed. | ||
O.each(STATUS_CODES, function(name, code) { | ||
it("must have " + code + " equal " + name, function() { | ||
@@ -10,0 +13,0 @@ CODES_JSON[code].must.equal(name) |
@@ -1,4 +0,6 @@ | ||
var $ = require("oolong") | ||
var STATUS_NAMES = require("http-codes") | ||
var O = require("oolong") | ||
var HttpError = require("..") | ||
var isVersion = require("semver").satisfies.bind(null, process.version) | ||
var describeNodeV012 = isVersion(">= 0.12 < 0.13") ? describe : xdescribe | ||
var describeNodeV4 = isVersion(">= 4 < 5") ? describe : xdescribe | ||
@@ -157,12 +159,52 @@ function RemoteError(code, msg) { HttpError.apply(this, arguments) } | ||
describe("HTTP status codes", function() { | ||
// Fail safes: | ||
STATUS_NAMES.must.have.property("NOT_FOUND", 404) | ||
STATUS_NAMES.must.have.property("INTERNAL_SERVER_ERROR", 500) | ||
it("must have NOT_FOUND equal 404", function() { | ||
HttpError.must.have.property("NOT_FOUND", 404) | ||
}) | ||
$.each(STATUS_NAMES, function(code, constant) { | ||
it("must have " + constant + " equal " + code, function() { | ||
HttpError[constant].must.equal(code) | ||
it("must have INTERNAL_SERVER_ERROR equal 500", function() { | ||
HttpError.must.have.property("INTERNAL_SERVER_ERROR", 500) | ||
}) | ||
describeNodeV012("when on Node v0.12", function() { | ||
var STATUS_NAMES = require("http-codes") | ||
// Fail safes: | ||
STATUS_NAMES.must.have.property("NOT_FOUND", 404) | ||
STATUS_NAMES.must.have.property("INTERNAL_SERVER_ERROR", 500) | ||
O.each(STATUS_NAMES, function(code, name) { | ||
it("must have " + name + " equal " + code, function() { | ||
HttpError[name].must.equal(code) | ||
}) | ||
}) | ||
}) | ||
describeNodeV4("when on Node v4", function() { | ||
O.each({ | ||
MOVED_TEMPORARILY: 302, // => FOUND | ||
REQUEST_TIME_OUT: 408, // => REQUEST_TIMEOUT | ||
REQUEST_ENTITY_TOO_LARGE: 413, // => PAYLOAD_TOO_LARGE | ||
REQUEST_URI_TOO_LARGE: 414, // => URI_TOO_LONG | ||
REQUESTED_RANGE_NOT_SATISFIABLE: 416, // => RANGE_NOT_SATISFIABLE | ||
GATEWAY_TIME_OUT: 504 // => GATEWAY_TIMEOUT | ||
}, function(code, name) { | ||
it("must have " + name + " equal " + code, function() { | ||
HttpError[name].must.equal(code) | ||
}) | ||
}) | ||
O.each({ | ||
302: "Moved Temporarily", | ||
408: "Request Time-out", | ||
413: "Request Entity Too Large", | ||
414: "Request-URI Too Large", | ||
416: "Requested Range Not Satisfiable", | ||
504: "Gateway Time-out" | ||
}, function(name, code) { | ||
it("must have " + name + " equal " + code, function() { | ||
new HttpError(Number(code)).message.must.equal(name) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23395
297
255
5