pax_global_header00006660000000000000000000000064131412422030014502gustar00rootroot0000000000000052 comment=7e534cb45fc06e8c3ad782cde89a7462851b27d1 http-errors-1.6.2/000077500000000000000000000000001314124220300140015ustar00rootroot00000000000000http-errors-1.6.2/.eslintignore000066400000000000000000000000261314124220300165020ustar00rootroot00000000000000coverage node_modules http-errors-1.6.2/.eslintrc000066400000000000000000000000341314124220300156220ustar00rootroot00000000000000{ "extends": "standard" } http-errors-1.6.2/.gitignore000066400000000000000000000001001314124220300157600ustar00rootroot00000000000000 .DS_Store* *.log *.gz node_modules coverage package-lock.json http-errors-1.6.2/.travis.yml000066400000000000000000000021061314124220300161110ustar00rootroot00000000000000language: node_js node_js: - "0.6" - "0.8" - "0.10" - "0.12" - "1.8" - "2.5" - "3.3" - "4.8" - "5.12" - "6.11" - "7.10" - "8.2" sudo: false cache: directories: - node_modules before_install: # Skip updating shrinkwrap / lock - "npm config set shrinkwrap false" # Setup Node.js version-specific dependencies - "test $TRAVIS_NODE_VERSION != '0.6' || npm rm --save-dev istanbul" - "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul" - "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 4 || npm rm --save-dev $(grep -E '\"eslint\\S*\"' package.json | cut -d'\"' -f2)" # Update Node.js modules - "test ! -d node_modules || npm prune" - "test ! -d node_modules || npm rebuild" script: # Run test script, depending on istanbul install - "test ! -z $(npm -ps ls istanbul) || npm test" - "test -z $(npm -ps ls istanbul) || npm run-script test-travis" - "test -z $(npm -ps ls eslint ) || npm run-script lint" after_script: - "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" http-errors-1.6.2/HISTORY.md000066400000000000000000000051331314124220300154660ustar00rootroot000000000000002017-08-04 / 1.6.2 ================== * deps: depd@1.1.1 - Remove unnecessary `Buffer` loading 2017-02-20 / 1.6.1 ================== * deps: setprototypeof@1.0.3 - Fix shim for old browsers 2017-02-14 / 1.6.0 ================== * Accept custom 4xx and 5xx status codes in factory * Add deprecation message to `"I'mateapot"` export * Deprecate passing status code as anything except first argument in factory * Deprecate using non-error status codes * Make `message` property enumerable for `HttpError`s 2016-11-16 / 1.5.1 ================== * deps: inherits@2.0.3 - Fix issue loading in browser * deps: setprototypeof@1.0.2 * deps: statuses@'>= 1.3.1 < 2' 2016-05-18 / 1.5.0 ================== * Support new code `421 Misdirected Request` * Use `setprototypeof` module to replace `__proto__` setting * deps: statuses@'>= 1.3.0 < 2' - Add `421 Misdirected Request` - perf: enable strict mode * perf: enable strict mode 2016-01-28 / 1.4.0 ================== * Add `HttpError` export, for `err instanceof createError.HttpError` * deps: inherits@2.0.1 * deps: statuses@'>= 1.2.1 < 2' - Fix message for status 451 - Remove incorrect nginx status code 2015-02-02 / 1.3.1 ================== * Fix regression where status can be overwritten in `createError` `props` 2015-02-01 / 1.3.0 ================== * Construct errors using defined constructors from `createError` * Fix error names that are not identifiers - `createError["I'mateapot"]` is now `createError.ImATeapot` * Set a meaningful `name` property on constructed errors 2014-12-09 / 1.2.8 ================== * Fix stack trace from exported function * Remove `arguments.callee` usage 2014-10-14 / 1.2.7 ================== * Remove duplicate line 2014-10-02 / 1.2.6 ================== * Fix `expose` to be `true` for `ClientError` constructor 2014-09-28 / 1.2.5 ================== * deps: statuses@1 2014-09-21 / 1.2.4 ================== * Fix dependency version to work with old `npm`s 2014-09-21 / 1.2.3 ================== * deps: statuses@~1.1.0 2014-09-21 / 1.2.2 ================== * Fix publish error 2014-09-21 / 1.2.1 ================== * Support Node.js 0.6 * Use `inherits` instead of `util` 2014-09-09 / 1.2.0 ================== * Fix the way inheriting functions * Support `expose` being provided in properties argument 2014-09-08 / 1.1.0 ================== * Default status to 500 * Support provided `error` to extend 2014-09-08 / 1.0.1 ================== * Fix accepting string message 2014-09-08 / 1.0.0 ================== * Initial release http-errors-1.6.2/LICENSE000066400000000000000000000022201314124220300150020ustar00rootroot00000000000000 The MIT License (MIT) Copyright (c) 2014 Jonathan Ong me@jongleberry.com Copyright (c) 2016 Douglas Christopher Wilson doug@somethingdoug.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. http-errors-1.6.2/README.md000066400000000000000000000113031314124220300152560ustar00rootroot00000000000000# http-errors [![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Node.js Version][node-version-image]][node-version-url] [![Build Status][travis-image]][travis-url] [![Test Coverage][coveralls-image]][coveralls-url] Create HTTP errors for Express, Koa, Connect, etc. with ease. ## Install This is a [Node.js](https://nodejs.org/en/) module available through the [npm registry](https://www.npmjs.com/). Installation is done using the [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): ```bash $ npm install http-errors ``` ## Example ```js var createError = require('http-errors') var express = require('express') var app = express() app.use(function (req, res, next) { if (!req.user) return next(createError(401, 'Please login to view this page.')) next() }) ``` ## API This is the current API, currently extracted from Koa and subject to change. All errors inherit from JavaScript `Error` and the exported `createError.HttpError`. ### Error Properties - `expose` - can be used to signal if `message` should be sent to the client, defaulting to `false` when `status` >= 500 - `headers` - can be an object of header names to values to be sent to the client, defaulting to `undefined`. When defined, the key names should all be lower-cased - `message` - the traditional error message, which should be kept short and all single line - `status` - the status code of the error, mirroring `statusCode` for general compatibility - `statusCode` - the status code of the error, defaulting to `500` ### createError([status], [message], [properties]) ```js var err = createError(404, 'This video does not exist!') ``` - `status: 500` - the status code as a number - `message` - the message of the error, defaulting to node's text for that status code. - `properties` - custom properties to attach to the object ### new createError\[code || name\](\[msg]\)) ```js var err = new createError.NotFound() ``` - `code` - the status code as a number - `name` - the name of the error as a "bumpy case", i.e. `NotFound` or `InternalServerError`. #### List of all constructors |Status Code|Constructor Name | |-----------|-----------------------------| |400 |BadRequest | |401 |Unauthorized | |402 |PaymentRequired | |403 |Forbidden | |404 |NotFound | |405 |MethodNotAllowed | |406 |NotAcceptable | |407 |ProxyAuthenticationRequired | |408 |RequestTimeout | |409 |Conflict | |410 |Gone | |411 |LengthRequired | |412 |PreconditionFailed | |413 |PayloadTooLarge | |414 |URITooLong | |415 |UnsupportedMediaType | |416 |RangeNotSatisfiable | |417 |ExpectationFailed | |418 |ImATeapot | |421 |MisdirectedRequest | |422 |UnprocessableEntity | |423 |Locked | |424 |FailedDependency | |425 |UnorderedCollection | |426 |UpgradeRequired | |428 |PreconditionRequired | |429 |TooManyRequests | |431 |RequestHeaderFieldsTooLarge | |451 |UnavailableForLegalReasons | |500 |InternalServerError | |501 |NotImplemented | |502 |BadGateway | |503 |ServiceUnavailable | |504 |GatewayTimeout | |505 |HTTPVersionNotSupported | |506 |VariantAlsoNegotiates | |507 |InsufficientStorage | |508 |LoopDetected | |509 |BandwidthLimitExceeded | |510 |NotExtended | |511 |NetworkAuthenticationRequired| ## License [MIT](LICENSE) [npm-image]: https://img.shields.io/npm/v/http-errors.svg [npm-url]: https://npmjs.org/package/http-errors [node-version-image]: https://img.shields.io/node/v/http-errors.svg [node-version-url]: https://nodejs.org/en/download/ [travis-image]: https://img.shields.io/travis/jshttp/http-errors.svg [travis-url]: https://travis-ci.org/jshttp/http-errors [coveralls-image]: https://img.shields.io/coveralls/jshttp/http-errors.svg [coveralls-url]: https://coveralls.io/r/jshttp/http-errors [downloads-image]: https://img.shields.io/npm/dm/http-errors.svg [downloads-url]: https://npmjs.org/package/http-errors http-errors-1.6.2/index.js000066400000000000000000000131761314124220300154560ustar00rootroot00000000000000/*! * http-errors * Copyright(c) 2014 Jonathan Ong * Copyright(c) 2016 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module dependencies. * @private */ var deprecate = require('depd')('http-errors') var setPrototypeOf = require('setprototypeof') var statuses = require('statuses') var inherits = require('inherits') /** * Module exports. * @public */ module.exports = createError module.exports.HttpError = createHttpErrorConstructor() // Populate exports for all constructors populateConstructorExports(module.exports, statuses.codes, module.exports.HttpError) /** * Get the code class of a status code. * @private */ function codeClass (status) { return Number(String(status).charAt(0) + '00') } /** * Create a new HTTP Error. * * @returns {Error} * @public */ function createError () { // so much arity going on ~_~ var err var msg var status = 500 var props = {} for (var i = 0; i < arguments.length; i++) { var arg = arguments[i] if (arg instanceof Error) { err = arg status = err.status || err.statusCode || status continue } switch (typeof arg) { case 'string': msg = arg break case 'number': status = arg if (i !== 0) { deprecate('non-first-argument status code; replace with createError(' + arg + ', ...)') } break case 'object': props = arg break } } if (typeof status === 'number' && (status < 400 || status >= 600)) { deprecate('non-error status code; use only 4xx or 5xx status codes') } if (typeof status !== 'number' || (!statuses[status] && (status < 400 || status >= 600))) { status = 500 } // constructor var HttpError = createError[status] || createError[codeClass(status)] if (!err) { // create error err = HttpError ? new HttpError(msg) : new Error(msg || statuses[status]) Error.captureStackTrace(err, createError) } if (!HttpError || !(err instanceof HttpError) || err.status !== status) { // add properties to generic error err.expose = status < 500 err.status = err.statusCode = status } for (var key in props) { if (key !== 'status' && key !== 'statusCode') { err[key] = props[key] } } return err } /** * Create HTTP error abstract base class. * @private */ function createHttpErrorConstructor () { function HttpError () { throw new TypeError('cannot construct abstract class') } inherits(HttpError, Error) return HttpError } /** * Create a constructor for a client error. * @private */ function createClientErrorConstructor (HttpError, name, code) { var className = name.match(/Error$/) ? name : name + 'Error' function ClientError (message) { // create the error object var msg = message != null ? message : statuses[code] var err = new Error(msg) // capture a stack trace to the construction point Error.captureStackTrace(err, ClientError) // adjust the [[Prototype]] setPrototypeOf(err, ClientError.prototype) // redefine the error message Object.defineProperty(err, 'message', { enumerable: true, configurable: true, value: msg, writable: true }) // redefine the error name Object.defineProperty(err, 'name', { enumerable: false, configurable: true, value: className, writable: true }) return err } inherits(ClientError, HttpError) ClientError.prototype.status = code ClientError.prototype.statusCode = code ClientError.prototype.expose = true return ClientError } /** * Create a constructor for a server error. * @private */ function createServerErrorConstructor (HttpError, name, code) { var className = name.match(/Error$/) ? name : name + 'Error' function ServerError (message) { // create the error object var msg = message != null ? message : statuses[code] var err = new Error(msg) // capture a stack trace to the construction point Error.captureStackTrace(err, ServerError) // adjust the [[Prototype]] setPrototypeOf(err, ServerError.prototype) // redefine the error message Object.defineProperty(err, 'message', { enumerable: true, configurable: true, value: msg, writable: true }) // redefine the error name Object.defineProperty(err, 'name', { enumerable: false, configurable: true, value: className, writable: true }) return err } inherits(ServerError, HttpError) ServerError.prototype.status = code ServerError.prototype.statusCode = code ServerError.prototype.expose = false return ServerError } /** * Populate the exports object with constructors for every error class. * @private */ function populateConstructorExports (exports, codes, HttpError) { codes.forEach(function forEachCode (code) { var CodeError var name = toIdentifier(statuses[code]) switch (codeClass(code)) { case 400: CodeError = createClientErrorConstructor(HttpError, name, code) break case 500: CodeError = createServerErrorConstructor(HttpError, name, code) break } if (CodeError) { // export the constructor exports[code] = CodeError exports[name] = CodeError } }) // backwards-compatibility exports["I'mateapot"] = deprecate.function(exports.ImATeapot, '"I\'mateapot"; use "ImATeapot" instead') } /** * Convert a string of words to a JavaScript identifier. * @private */ function toIdentifier (str) { return str.split(' ').map(function (token) { return token.slice(0, 1).toUpperCase() + token.slice(1) }).join('').replace(/[^ _0-9a-z]/gi, '') } http-errors-1.6.2/package.json000066400000000000000000000024001314124220300162630ustar00rootroot00000000000000{ "name": "http-errors", "description": "Create HTTP error objects", "version": "1.6.2", "author": "Jonathan Ong (http://jongleberry.com)", "contributors": [ "Alan Plum ", "Douglas Christopher Wilson " ], "license": "MIT", "repository": "jshttp/http-errors", "dependencies": { "depd": "1.1.1", "inherits": "2.0.3", "setprototypeof": "1.0.3", "statuses": ">= 1.3.1 < 2" }, "devDependencies": { "eslint": "3.19.0", "eslint-config-standard": "10.2.1", "eslint-plugin-import": "2.7.0", "eslint-plugin-markdown": "1.0.0-beta.6", "eslint-plugin-node": "5.1.1", "eslint-plugin-promise": "3.5.0", "eslint-plugin-standard": "3.0.1", "istanbul": "0.4.5", "mocha": "1.21.5" }, "engines": { "node": ">= 0.6" }, "scripts": { "lint": "eslint --plugin markdown --ext js,md .", "test": "mocha --reporter spec --bail", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot" }, "keywords": [ "http", "error" ], "files": [ "index.js", "HISTORY.md", "LICENSE", "README.md" ] } http-errors-1.6.2/test/000077500000000000000000000000001314124220300147605ustar00rootroot00000000000000http-errors-1.6.2/test/.eslintrc000066400000000000000000000000451314124220300166030ustar00rootroot00000000000000{ "env": { "mocha": true } } http-errors-1.6.2/test/test.js000066400000000000000000000176451314124220300163120ustar00rootroot00000000000000 process.env.NO_DEPRECATION = 'http-errors' var assert = require('assert') var util = require('util') var create = require('..') describe('HTTP Errors', function () { it('create(status)', function () { var err = create(404) assert.equal(err.name, 'NotFoundError') assert.equal(err.message, 'Not Found') assert.equal(err.status, 404) assert.equal(err.statusCode, 404) }) it('create(status) for 300', function () { var err = create(300) assert.equal(err.name, 'Error') assert.equal(err.message, 'Multiple Choices') assert.equal(err.status, 300) assert.equal(err.statusCode, 300) }) it('create(status) for 471', function () { var err = create(471) assert.equal(err.name, 'BadRequestError') assert.equal(err.message, 'Bad Request') assert.equal(err.status, 471) assert.equal(err.statusCode, 471) }) it('create(status) for 520', function () { var err = create(520) assert.equal(err.name, 'InternalServerError') assert.equal(err.message, 'Internal Server Error') assert.equal(err.status, 520) assert.equal(err.statusCode, 520) }) it('create(status, msg)', function () { var err = create(404, 'LOL') assert.equal(err.name, 'NotFoundError') assert.equal(err.message, 'LOL') assert.equal(err.status, 404) assert.equal(err.statusCode, 404) }) it('create(status, props)', function () { var err = create(404, { id: 1 }) assert.equal(err.name, 'NotFoundError') assert.equal(err.message, 'Not Found') assert.equal(err.status, 404) assert.equal(err.statusCode, 404) assert.equal(err.id, 1) }) it('create(status, props) with status prop', function () { var err = create(404, { id: 1, status: 500 }) assert.equal(err.name, 'NotFoundError') assert.equal(err.message, 'Not Found') assert.equal(err.status, 404) assert.equal(err.statusCode, 404) assert.equal(err.id, 1) }) it('create(status, props) with statusCode prop', function () { var err = create(404, { id: 1, statusCode: 500 }) assert.equal(err.name, 'NotFoundError') assert.equal(err.message, 'Not Found') assert.equal(err.status, 404) assert.equal(err.statusCode, 404) assert.equal(err.id, 1) }) it('create(props)', function () { var err = create({ id: 1 }) assert.equal(err.name, 'InternalServerError') assert.equal(err.message, 'Internal Server Error') assert.equal(err.status, 500) assert.equal(err.statusCode, 500) assert.equal(err.id, 1) }) it('create(msg, status)', function () { var err = create('LOL', 404) assert.equal(err.name, 'NotFoundError') assert.equal(err.message, 'LOL') assert.equal(err.status, 404) assert.equal(err.statusCode, 404) }) it('create(msg)', function () { var err = create('LOL') assert.equal(err.name, 'InternalServerError') assert.equal(err.message, 'LOL') assert.equal(err.status, 500) assert.equal(err.statusCode, 500) }) it('create(msg, props)', function () { var err = create('LOL', { id: 1 }) assert.equal(err.name, 'InternalServerError') assert.equal(err.message, 'LOL') assert.equal(err.status, 500) assert.equal(err.statusCode, 500) assert.equal(err.id, 1) }) it('create(err)', function () { var _err = new Error('LOL') _err.status = 404 var err = create(_err) assert.equal(err, _err) assert.equal(err.name, 'Error') assert.equal(err.message, 'LOL') assert.equal(err.status, 404) assert.equal(err.statusCode, 404) assert.equal(err.expose, true) _err = new Error('LOL') err = create(_err) assert.equal(err, _err) assert.equal(err.name, 'Error') assert.equal(err.message, 'LOL') assert.equal(err.status, 500) assert.equal(err.statusCode, 500) assert.equal(err.expose, false) }) it('create(err) with invalid err.status', function () { var _err = new Error('Connection refused') _err.status = -1 var err = create(_err) assert.equal(err, _err) assert.equal(err.name, 'Error') assert.equal(err.message, 'Connection refused') assert.equal(err.status, 500) assert.equal(err.statusCode, 500) assert.equal(err.expose, false) }) it('create(err, props)', function () { var _err = new Error('LOL') _err.status = 404 var err = create(_err, { id: 1 }) assert.equal(err.name, 'Error') assert.equal(err.message, 'LOL') assert.equal(err.status, 404) assert.equal(err.statusCode, 404) assert.equal(err.id, 1) assert.equal(err.expose, true) }) it('create(status, err, props)', function () { var _err = new Error('LOL') var err = create(404, _err, { id: 1 }) assert.equal(err, _err) assert.equal(err.name, 'Error') assert.equal(err.message, 'LOL') assert.equal(err.status, 404) assert.equal(err.statusCode, 404) assert.equal(err.id, 1) }) it('create(status, msg, props)', function () { var err = create(404, 'LOL', { id: 1 }) assert.equal(err.name, 'NotFoundError') assert.equal(err.message, 'LOL') assert.equal(err.status, 404) assert.equal(err.statusCode, 404) assert.equal(err.id, 1) }) it('create(status, msg, { expose: false })', function () { var err = create(404, 'LOL', { expose: false }) assert.equal(err.expose, false) }) it('new create.HttpError()', function () { assert.throws(function () { new create.HttpError() // eslint-disable-line no-new }, /cannot construct abstract class/) }) it('new create.NotFound()', function () { var err = new create.NotFound() assert.equal(err.name, 'NotFoundError') assert.equal(err.message, 'Not Found') assert.equal(err.status, 404) assert.equal(err.statusCode, 404) assert.equal(err.expose, true) assert(err.stack) }) it('new create.InternalServerError()', function () { var err = new create.InternalServerError() assert.equal(err.name, 'InternalServerError') assert.equal(err.message, 'Internal Server Error') assert.equal(err.status, 500) assert.equal(err.statusCode, 500) assert.equal(err.expose, false) assert(err.stack) }) it('new create["404"]()', function () { var err = new create['404']() assert.equal(err.name, 'NotFoundError') assert.equal(err.message, 'Not Found') assert.equal(err.status, 404) assert.equal(err.statusCode, 404) assert.equal(err.expose, true) assert(err.stack) }) it('should preserve error [[Class]]', function () { assert.equal(Object.prototype.toString.call(create('LOL')), '[object Error]') assert.equal(Object.prototype.toString.call(new create[404]()), '[object Error]') assert.equal(Object.prototype.toString.call(new create[500]()), '[object Error]') }) it('should support err instanceof Error', function () { assert(create(404) instanceof Error) assert((new create['404']()) instanceof Error) assert((new create['500']()) instanceof Error) }) it('should support err instanceof exposed constructor', function () { assert(create(404) instanceof create.NotFound) assert(create(500) instanceof create.InternalServerError) assert((new create['404']()) instanceof create.NotFound) assert((new create['500']()) instanceof create.InternalServerError) assert((new create.NotFound()) instanceof create.NotFound) assert((new create.InternalServerError()) instanceof create.InternalServerError) }) it('should support err instanceof HttpError', function () { assert(create(404) instanceof create.HttpError) assert((new create['404']()) instanceof create.HttpError) assert((new create['500']()) instanceof create.HttpError) }) it('should support util.isError()', function () { /* eslint-disable node/no-deprecated-api */ assert(util.isError(create(404))) assert(util.isError(new create['404']())) assert(util.isError(new create['500']())) /* eslint-enable node/no-deprecated-api */ }) })