@hapi/boom
Advanced tools
Comparing version 7.4.11 to 8.0.1
374
lib/index.js
@@ -69,3 +69,3 @@ 'use strict'; | ||
module.exports = internals.Boom = class extends Error { | ||
exports.Boom = class extends Error { | ||
@@ -75,6 +75,6 @@ constructor(message, options = {}) { | ||
if (message instanceof Error) { | ||
return internals.Boom.boomify(Hoek.clone(message), options); | ||
return exports.boomify(Hoek.clone(message), options); | ||
} | ||
const { statusCode = 500, data = null, ctor = internals.Boom } = options; | ||
const { statusCode = 500, data = null, ctor = exports.Boom } = options; | ||
const error = new Error(message ? message : undefined); // Avoids settings null message | ||
@@ -96,273 +96,303 @@ Error.captureStackTrace(error, ctor); // Filter the stack to our external API | ||
return internals.Boom.isBoom(instance); | ||
return exports.isBoom(instance); | ||
} | ||
}; | ||
static isBoom(err) { | ||
return err instanceof Error && !!err.isBoom; | ||
} | ||
exports.isBoom = function (err) { | ||
static boomify(err, options) { | ||
return err instanceof Error && !!err.isBoom; | ||
}; | ||
Hoek.assert(err instanceof Error, 'Cannot wrap non-Error object'); | ||
options = options || {}; | ||
exports.boomify = function (err, options) { | ||
if (options.data !== undefined) { | ||
err.data = options.data; | ||
} | ||
Hoek.assert(err instanceof Error, 'Cannot wrap non-Error object'); | ||
if (options.decorate) { | ||
Object.assign(err, options.decorate); | ||
} | ||
options = options || {}; | ||
if (!err.isBoom) { | ||
return internals.initialize(err, options.statusCode || 500, options.message); | ||
} | ||
if (options.data !== undefined) { | ||
err.data = options.data; | ||
} | ||
if (options.override === false || // Defaults to true | ||
!options.statusCode && !options.message) { | ||
if (options.decorate) { | ||
Object.assign(err, options.decorate); | ||
} | ||
return err; | ||
} | ||
if (!err.isBoom) { | ||
return internals.initialize(err, options.statusCode || 500, options.message); | ||
} | ||
return internals.initialize(err, options.statusCode || err.output.statusCode, options.message); | ||
if (options.override === false || // Defaults to true | ||
!options.statusCode && !options.message) { | ||
return err; | ||
} | ||
// 4xx Client Errors | ||
return internals.initialize(err, options.statusCode || err.output.statusCode, options.message); | ||
}; | ||
static badRequest(message, data) { | ||
return new internals.Boom(message, { statusCode: 400, data, ctor: internals.Boom.badRequest }); | ||
} | ||
// 4xx Client Errors | ||
static unauthorized(message, scheme, attributes) { // Or (message, wwwAuthenticate[]) | ||
exports.badRequest = function (message, data) { | ||
const err = new internals.Boom(message, { statusCode: 401, ctor: internals.Boom.unauthorized }); | ||
return new exports.Boom(message, { statusCode: 400, data, ctor: exports.badRequest }); | ||
}; | ||
// function (message) | ||
if (!scheme) { | ||
return err; | ||
} | ||
exports.unauthorized = function (message, scheme, attributes) { // Or (message, wwwAuthenticate[]) | ||
// function (message, wwwAuthenticate[]) | ||
const err = new exports.Boom(message, { statusCode: 401, ctor: exports.unauthorized }); | ||
if (typeof scheme !== 'string') { | ||
err.output.headers['WWW-Authenticate'] = scheme.join(', '); | ||
return err; | ||
} | ||
// function (message) | ||
// function (message, scheme, attributes) | ||
if (!scheme) { | ||
return err; | ||
} | ||
let wwwAuthenticate = `${scheme} `; | ||
// function (message, wwwAuthenticate[]) | ||
if (attributes || | ||
message) { | ||
if (typeof scheme !== 'string') { | ||
err.output.headers['WWW-Authenticate'] = scheme.join(', '); | ||
return err; | ||
} | ||
err.output.payload.attributes = {}; | ||
} | ||
// function (message, scheme, attributes) | ||
if (attributes) { | ||
if (typeof attributes === 'string') { | ||
wwwAuthenticate += Hoek.escapeHeaderAttribute(attributes); | ||
err.output.payload.attributes = attributes; | ||
} | ||
else { | ||
wwwAuthenticate += Object.keys(attributes).map((name) => { | ||
let wwwAuthenticate = `${scheme} `; | ||
let value = attributes[name]; | ||
if (value === null || | ||
value === undefined) { | ||
if (attributes || | ||
message) { | ||
value = ''; | ||
} | ||
err.output.payload.attributes = {}; | ||
} | ||
err.output.payload.attributes[name] = value; | ||
return `${name}="${Hoek.escapeHeaderAttribute(value.toString())}"`; | ||
}) | ||
.join(', '); | ||
} | ||
if (attributes) { | ||
if (typeof attributes === 'string') { | ||
wwwAuthenticate += Hoek.escapeHeaderAttribute(attributes); | ||
err.output.payload.attributes = attributes; | ||
} | ||
else { | ||
wwwAuthenticate += Object.keys(attributes).map((name) => { | ||
if (message) { | ||
if (attributes) { | ||
wwwAuthenticate += ', '; | ||
} | ||
let value = attributes[name]; | ||
if (value === null || | ||
value === undefined) { | ||
wwwAuthenticate += `error="${Hoek.escapeHeaderAttribute(message)}"`; | ||
err.output.payload.attributes.error = message; | ||
value = ''; | ||
} | ||
err.output.payload.attributes[name] = value; | ||
return `${name}="${Hoek.escapeHeaderAttribute(value.toString())}"`; | ||
}) | ||
.join(', '); | ||
} | ||
else { | ||
err.isMissing = true; | ||
} | ||
if (message) { | ||
if (attributes) { | ||
wwwAuthenticate += ', '; | ||
} | ||
err.output.headers['WWW-Authenticate'] = wwwAuthenticate; | ||
return err; | ||
wwwAuthenticate += `error="${Hoek.escapeHeaderAttribute(message)}"`; | ||
err.output.payload.attributes.error = message; | ||
} | ||
else { | ||
err.isMissing = true; | ||
} | ||
static paymentRequired(message, data) { | ||
err.output.headers['WWW-Authenticate'] = wwwAuthenticate; | ||
return err; | ||
}; | ||
return new internals.Boom(message, { statusCode: 402, data, ctor: internals.Boom.paymentRequired }); | ||
} | ||
static forbidden(message, data) { | ||
exports.paymentRequired = function (message, data) { | ||
return new internals.Boom(message, { statusCode: 403, data, ctor: internals.Boom.forbidden }); | ||
} | ||
return new exports.Boom(message, { statusCode: 402, data, ctor: exports.paymentRequired }); | ||
}; | ||
static notFound(message, data) { | ||
return new internals.Boom(message, { statusCode: 404, data, ctor: internals.Boom.notFound }); | ||
} | ||
exports.forbidden = function (message, data) { | ||
static methodNotAllowed(message, data, allow) { | ||
return new exports.Boom(message, { statusCode: 403, data, ctor: exports.forbidden }); | ||
}; | ||
const err = new internals.Boom(message, { statusCode: 405, data, ctor: internals.Boom.methodNotAllowed }); | ||
if (typeof allow === 'string') { | ||
allow = [allow]; | ||
} | ||
exports.notFound = function (message, data) { | ||
if (Array.isArray(allow)) { | ||
err.output.headers.Allow = allow.join(', '); | ||
} | ||
return new exports.Boom(message, { statusCode: 404, data, ctor: exports.notFound }); | ||
}; | ||
return err; | ||
} | ||
static notAcceptable(message, data) { | ||
exports.methodNotAllowed = function (message, data, allow) { | ||
return new internals.Boom(message, { statusCode: 406, data, ctor: internals.Boom.notAcceptable }); | ||
const err = new exports.Boom(message, { statusCode: 405, data, ctor: exports.methodNotAllowed }); | ||
if (typeof allow === 'string') { | ||
allow = [allow]; | ||
} | ||
static proxyAuthRequired(message, data) { | ||
return new internals.Boom(message, { statusCode: 407, data, ctor: internals.Boom.proxyAuthRequired }); | ||
if (Array.isArray(allow)) { | ||
err.output.headers.Allow = allow.join(', '); | ||
} | ||
static clientTimeout(message, data) { | ||
return err; | ||
}; | ||
return new internals.Boom(message, { statusCode: 408, data, ctor: internals.Boom.clientTimeout }); | ||
} | ||
static conflict(message, data) { | ||
exports.notAcceptable = function (message, data) { | ||
return new internals.Boom(message, { statusCode: 409, data, ctor: internals.Boom.conflict }); | ||
} | ||
return new exports.Boom(message, { statusCode: 406, data, ctor: exports.notAcceptable }); | ||
}; | ||
static resourceGone(message, data) { | ||
return new internals.Boom(message, { statusCode: 410, data, ctor: internals.Boom.resourceGone }); | ||
} | ||
exports.proxyAuthRequired = function (message, data) { | ||
static lengthRequired(message, data) { | ||
return new exports.Boom(message, { statusCode: 407, data, ctor: exports.proxyAuthRequired }); | ||
}; | ||
return new internals.Boom(message, { statusCode: 411, data, ctor: internals.Boom.lengthRequired }); | ||
} | ||
static preconditionFailed(message, data) { | ||
exports.clientTimeout = function (message, data) { | ||
return new internals.Boom(message, { statusCode: 412, data, ctor: internals.Boom.preconditionFailed }); | ||
} | ||
return new exports.Boom(message, { statusCode: 408, data, ctor: exports.clientTimeout }); | ||
}; | ||
static entityTooLarge(message, data) { | ||
return new internals.Boom(message, { statusCode: 413, data, ctor: internals.Boom.entityTooLarge }); | ||
} | ||
exports.conflict = function (message, data) { | ||
static uriTooLong(message, data) { | ||
return new exports.Boom(message, { statusCode: 409, data, ctor: exports.conflict }); | ||
}; | ||
return new internals.Boom(message, { statusCode: 414, data, ctor: internals.Boom.uriTooLong }); | ||
} | ||
static unsupportedMediaType(message, data) { | ||
exports.resourceGone = function (message, data) { | ||
return new internals.Boom(message, { statusCode: 415, data, ctor: internals.Boom.unsupportedMediaType }); | ||
} | ||
return new exports.Boom(message, { statusCode: 410, data, ctor: exports.resourceGone }); | ||
}; | ||
static rangeNotSatisfiable(message, data) { | ||
return new internals.Boom(message, { statusCode: 416, data, ctor: internals.Boom.rangeNotSatisfiable }); | ||
} | ||
exports.lengthRequired = function (message, data) { | ||
static expectationFailed(message, data) { | ||
return new exports.Boom(message, { statusCode: 411, data, ctor: exports.lengthRequired }); | ||
}; | ||
return new internals.Boom(message, { statusCode: 417, data, ctor: internals.Boom.expectationFailed }); | ||
} | ||
static teapot(message, data) { | ||
exports.preconditionFailed = function (message, data) { | ||
return new internals.Boom(message, { statusCode: 418, data, ctor: internals.Boom.teapot }); | ||
} | ||
return new exports.Boom(message, { statusCode: 412, data, ctor: exports.preconditionFailed }); | ||
}; | ||
static badData(message, data) { | ||
return new internals.Boom(message, { statusCode: 422, data, ctor: internals.Boom.badData }); | ||
} | ||
exports.entityTooLarge = function (message, data) { | ||
static locked(message, data) { | ||
return new exports.Boom(message, { statusCode: 413, data, ctor: exports.entityTooLarge }); | ||
}; | ||
return new internals.Boom(message, { statusCode: 423, data, ctor: internals.Boom.locked }); | ||
} | ||
static failedDependency(message, data) { | ||
exports.uriTooLong = function (message, data) { | ||
return new internals.Boom(message, { statusCode: 424, data, ctor: internals.Boom.failedDependency }); | ||
} | ||
return new exports.Boom(message, { statusCode: 414, data, ctor: exports.uriTooLong }); | ||
}; | ||
static preconditionRequired(message, data) { | ||
return new internals.Boom(message, { statusCode: 428, data, ctor: internals.Boom.preconditionRequired }); | ||
} | ||
exports.unsupportedMediaType = function (message, data) { | ||
static tooManyRequests(message, data) { | ||
return new exports.Boom(message, { statusCode: 415, data, ctor: exports.unsupportedMediaType }); | ||
}; | ||
return new internals.Boom(message, { statusCode: 429, data, ctor: internals.Boom.tooManyRequests }); | ||
} | ||
static illegal(message, data) { | ||
exports.rangeNotSatisfiable = function (message, data) { | ||
return new internals.Boom(message, { statusCode: 451, data, ctor: internals.Boom.illegal }); | ||
} | ||
return new exports.Boom(message, { statusCode: 416, data, ctor: exports.rangeNotSatisfiable }); | ||
}; | ||
// 5xx Server Errors | ||
static internal(message, data, statusCode = 500) { | ||
exports.expectationFailed = function (message, data) { | ||
return internals.serverError(message, data, statusCode, internals.Boom.internal); | ||
} | ||
return new exports.Boom(message, { statusCode: 417, data, ctor: exports.expectationFailed }); | ||
}; | ||
static notImplemented(message, data) { | ||
return internals.serverError(message, data, 501, internals.Boom.notImplemented); | ||
} | ||
exports.teapot = function (message, data) { | ||
static badGateway(message, data) { | ||
return new exports.Boom(message, { statusCode: 418, data, ctor: exports.teapot }); | ||
}; | ||
return internals.serverError(message, data, 502, internals.Boom.badGateway); | ||
} | ||
static serverUnavailable(message, data) { | ||
exports.badData = function (message, data) { | ||
return internals.serverError(message, data, 503, internals.Boom.serverUnavailable); | ||
} | ||
return new exports.Boom(message, { statusCode: 422, data, ctor: exports.badData }); | ||
}; | ||
static gatewayTimeout(message, data) { | ||
return internals.serverError(message, data, 504, internals.Boom.gatewayTimeout); | ||
} | ||
exports.locked = function (message, data) { | ||
static badImplementation(message, data) { | ||
return new exports.Boom(message, { statusCode: 423, data, ctor: exports.locked }); | ||
}; | ||
const err = internals.serverError(message, data, 500, internals.Boom.badImplementation); | ||
err.isDeveloperError = true; | ||
return err; | ||
} | ||
exports.failedDependency = function (message, data) { | ||
return new exports.Boom(message, { statusCode: 424, data, ctor: exports.failedDependency }); | ||
}; | ||
internals.Boom.default = internals.Boom; // Support ES6 module import | ||
exports.preconditionRequired = function (message, data) { | ||
return new exports.Boom(message, { statusCode: 428, data, ctor: exports.preconditionRequired }); | ||
}; | ||
exports.tooManyRequests = function (message, data) { | ||
return new exports.Boom(message, { statusCode: 429, data, ctor: exports.tooManyRequests }); | ||
}; | ||
exports.illegal = function (message, data) { | ||
return new exports.Boom(message, { statusCode: 451, data, ctor: exports.illegal }); | ||
}; | ||
// 5xx Server Errors | ||
exports.internal = function (message, data, statusCode = 500) { | ||
return internals.serverError(message, data, statusCode, exports.internal); | ||
}; | ||
exports.notImplemented = function (message, data) { | ||
return internals.serverError(message, data, 501, exports.notImplemented); | ||
}; | ||
exports.badGateway = function (message, data) { | ||
return internals.serverError(message, data, 502, exports.badGateway); | ||
}; | ||
exports.serverUnavailable = function (message, data) { | ||
return internals.serverError(message, data, 503, exports.serverUnavailable); | ||
}; | ||
exports.gatewayTimeout = function (message, data) { | ||
return internals.serverError(message, data, 504, exports.gatewayTimeout); | ||
}; | ||
exports.badImplementation = function (message, data) { | ||
const err = internals.serverError(message, data, 500, exports.badImplementation); | ||
err.isDeveloperError = true; | ||
return err; | ||
}; | ||
internals.initialize = function (err, statusCode, message) { | ||
@@ -397,3 +427,3 @@ | ||
const props = Object.getOwnPropertyDescriptor(err, 'message') || Object.getOwnPropertyDescriptor(Object.getPrototypeOf(err), 'message'); | ||
Hoek.assert(props.configurable && !props.get, 'The error is not compatible with boom'); | ||
Hoek.assert(!props || props.configurable && !props.get, 'The error is not compatible with boom'); | ||
@@ -428,6 +458,6 @@ err.message = message + (err.message ? ': ' + err.message : ''); | ||
return internals.Boom.boomify(data, { statusCode, message }); | ||
return exports.boomify(data, { statusCode, message }); | ||
} | ||
return new internals.Boom(message, { statusCode, data, ctor }); | ||
return new exports.Boom(message, { statusCode, data, ctor }); | ||
}; |
{ | ||
"name": "@hapi/boom", | ||
"description": "HTTP-friendly error objects", | ||
"version": "7.4.11", | ||
"version": "8.0.1", | ||
"repository": "git://github.com/hapijs/boom", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"keywords": [ | ||
@@ -22,3 +23,3 @@ "error", | ||
"scripts": { | ||
"test": "lab -a @hapi/code -t 100 -L", | ||
"test": "lab -a @hapi/code -t 100 -L -Y", | ||
"test-cov-html": "lab -a @hapi/code -t 100 -L -r html -o coverage.html" | ||
@@ -25,0 +26,0 @@ }, |
@@ -1,11 +0,16 @@ | ||
<a href="http://hapijs.com"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a> | ||
<a href="https://hapi.dev"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a> | ||
# @hapi/boom | ||
HTTP-friendly error objects | ||
#### HTTP-friendly error objects. | ||
[![Build Status](https://secure.travis-ci.org/hapijs/boom.svg)](http://travis-ci.org/hapijs/boom) | ||
**boom** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) – they work even better together. | ||
## Documentation | ||
### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support | ||
[**API Reference**](API.md) | ||
## Useful resources | ||
- [Documentation and API](https://hapi.dev/family/boom/) | ||
- [Version status](https://hapi.dev/resources/status/#boom) (builds, dependencies, node versions, licenses, eol) | ||
- [Project policies](https://hapi.dev/policies/) | ||
- [Free and commercial support options](https://hapi.dev/support/) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
27702
6
729
17
1