pax_global_header00006660000000000000000000000064135247115650014523gustar00rootroot0000000000000052 comment=c14e7d38dae756f31737d61613b0dfae0eccd89f boom-7.4.3/000077500000000000000000000000001352471156500124725ustar00rootroot00000000000000boom-7.4.3/.gitignore000066400000000000000000000001541352471156500144620ustar00rootroot00000000000000**/node_modules **/package-lock.json coverage.* **/.DS_Store **/._* **/*.pem **/.vs **/.vscode **/.idea boom-7.4.3/.npmignore000066400000000000000000000000111352471156500144610ustar00rootroot00000000000000* !lib/**boom-7.4.3/.travis.yml000077500000000000000000000002231352471156500146030ustar00rootroot00000000000000language: node_js node_js: - "8" - "10" - "12" - "node" sudo: false install: - "npm install" os: - "linux" - "osx" - "windows" boom-7.4.3/API.md000077500000000000000000000513771352471156500134450ustar00rootroot00000000000000# Table of Contents ## boom - [Boom](#boom) - [`reformat(debug)`](#reformatdebug) - [Helper Methods](#helper-methods) - [`new Boom(message, [options])`](#new-boommessage-options) - [`boomify(err, [options])`](#boomifyerr-options) - [`isBoom(err)`](#isboomerr) - [HTTP 4xx Errors](#http-4xx-errors) - [`Boom.badRequest([message], [data])`](#boombadrequestmessage-data) - [`Boom.unauthorized([message], [scheme], [attributes])`](#boomunauthorizedmessage-scheme-attributes) - [`Boom.paymentRequired([message], [data])`](#boompaymentrequiredmessage-data) - [`Boom.forbidden([message], [data])`](#boomforbiddenmessage-data) - [`Boom.notFound([message], [data])`](#boomnotfoundmessage-data) - [`Boom.methodNotAllowed([message], [data], [allow])`](#boommethodnotallowedmessage-data-allow) - [`Boom.notAcceptable([message], [data])`](#boomnotacceptablemessage-data) - [`Boom.proxyAuthRequired([message], [data])`](#boomproxyauthrequiredmessage-data) - [`Boom.clientTimeout([message], [data])`](#boomclienttimeoutmessage-data) - [`Boom.conflict([message], [data])`](#boomconflictmessage-data) - [`Boom.resourceGone([message], [data])`](#boomresourcegonemessage-data) - [`Boom.lengthRequired([message], [data])`](#boomlengthrequiredmessage-data) - [`Boom.preconditionFailed([message], [data])`](#boompreconditionfailedmessage-data) - [`Boom.entityTooLarge([message], [data])`](#boomentitytoolargemessage-data) - [`Boom.uriTooLong([message], [data])`](#boomuritoolongmessage-data) - [`Boom.unsupportedMediaType([message], [data])`](#boomunsupportedmediatypemessage-data) - [`Boom.rangeNotSatisfiable([message], [data])`](#boomrangenotsatisfiablemessage-data) - [`Boom.expectationFailed([message], [data])`](#boomexpectationfailedmessage-data) - [`Boom.teapot([message], [data])`](#boomteapotmessage-data) - [`Boom.badData([message], [data])`](#boombaddatamessage-data) - [`Boom.locked([message], [data])`](#boomlockedmessage-data) - [`Boom.failedDependency([message], [data])`](#boomfaileddependencymessage-data) - [`Boom.preconditionRequired([message], [data])`](#boompreconditionrequiredmessage-data) - [`Boom.tooManyRequests([message], [data])`](#boomtoomanyrequestsmessage-data) - [`Boom.illegal([message], [data])`](#boomillegalmessage-data) - [HTTP 5xx Errors](#http-5xx-errors) - [`Boom.badImplementation([message], [data])` - (*alias: `internal`*)](#boombadimplementationmessage-data---alias-internal) - [`Boom.notImplemented([message], [data])`](#boomnotimplementedmessage-data) - [`Boom.badGateway([message], [data])`](#boombadgatewaymessage-data) - [`Boom.serverUnavailable([message], [data])`](#boomserverunavailablemessage-data) - [`Boom.gatewayTimeout([message], [data])`](#boomgatewaytimeoutmessage-data) - [F.A.Q.](#faq) ### Boom **boom** provides a set of utilities for returning HTTP errors. Each utility returns a `Boom` error response object which includes the following properties: - `isBoom` - if `true`, indicates this is a `Boom` object instance. Note that this boolean should only be used if the error is an instance of `Error`. If it is not certain, use `Boom.isBoom()` instead. - `isServer` - convenience bool indicating status code >= 500. - `message` - the error message. - `typeof` - the constructor used to create the error (e.g. `Boom.badRequest`). - `output` - the formatted response. Can be directly manipulated after object construction to return a custom error response. Allowed root keys: - `statusCode` - the HTTP status code (typically 4xx or 5xx). - `headers` - an object containing any HTTP headers where each key is a header name and value is the header content. - `payload` - the formatted object used as the response payload (stringified). Can be directly manipulated but any changes will be lost if `reformat()` is called. Any content allowed and by default includes the following content: - `statusCode` - the HTTP status code, derived from `error.output.statusCode`. - `error` - the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived from `statusCode`. - `message` - the error message derived from `error.message`. - inherited `Error` properties. The `Boom` object also supports the following method: #### `reformat(debug)` Rebuilds `error.output` using the other object properties where: - `debug` - a Boolean that, when `true`, causes Internal Server Error messages to be left in tact. Defaults to `false`, meaning that Internal Server Error messages are redacted. Note that `Boom` object will return `true` when used with `instanceof Boom`, but do not use the `Boom` prototype (they are either plain `Error` or the error prototype passed in). This means `Boom` objects should only be tested using `instanceof Boom` or `Boom.isBoom()` but not by looking at the prototype or contructor information. This limitation is to avoid manipulating the prototype chain which is very slow. #### Helper Methods ##### `new Boom(message, [options])` Creates a new `Boom` object using the provided `message` and then calling [`boomify()`](#boomifyerr-options) to decorate the error with the `Boom` properties, where: - `message` - the error message. If `message` is an error, it is the same as calling [`boomify()`](#boomifyerr-options) directly. - `options` - and optional object where: - `statusCode` - the HTTP status code. Defaults to `500` if no status code is already set. - `data` - additional error information (assigned to `error.data`). - `decorate` - an option with extra properties to set on the error object. - `ctor` - constructor reference used to crop the exception call stack output. - if `message` is an error object, also supports the other [`boomify()`](#boomifyerr-options) options. ##### `boomify(err, [options])` Decorates an error with the `Boom` properties where: - `err` - the `Error` object to decorate. - `options` - optional object with the following optional settings: - `statusCode` - the HTTP status code. Defaults to `500` if no status code is already set and `err` is not a `Boom` object. - `message` - error message string. If the error already has a message, the provided `message` is added as a prefix. Defaults to no message. - `decorate` - an option with extra properties to set on the error object. - `override` - if `false`, the `err` provided is a `Boom` object, and a `statusCode` or `message` are provided, the values are ignored. Defaults to `true` (apply the provided `statusCode` and `message` options to the error regardless of its type, `Error` or `Boom` object). ```js var error = new Error('Unexpected input'); Boom.boomify(error, { statusCode: 400 }); ``` ##### `isBoom(err)` Identifies whether an error is a `Boom` object. Same as calling `instanceof Boom`. #### HTTP 4xx Errors ##### `Boom.badRequest([message], [data])` Returns a 400 Bad Request error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.badRequest('invalid query'); ``` Generates the following response payload: ```json { "statusCode": 400, "error": "Bad Request", "message": "invalid query" } ``` ##### `Boom.unauthorized([message], [scheme], [attributes])` Returns a 401 Unauthorized error where: - `message` - optional message. - `scheme` can be one of the following: - an authentication scheme name - an array of string values. These values will be separated by ', ' and set to the 'WWW-Authenticate' header. - `attributes` - an object of values to use while setting the 'WWW-Authenticate' header. This value is only used when `scheme` is a string, otherwise it is ignored. Every key/value pair will be included in the 'WWW-Authenticate' in the format of 'key="value"' as well as in the response payload under the `attributes` key. Alternatively value can be a string which is use to set the value of the scheme, for example setting the token value for negotiate header. If string is used message parameter must be null. `null` and `undefined` will be replaced with an empty string. If `attributes` is set, `message` will be used as the 'error' segment of the 'WWW-Authenticate' header. If `message` is unset, the 'error' segment of the header will not be present and `isMissing` will be true on the error object. If either `scheme` or `attributes` are set, the resultant `Boom` object will have the 'WWW-Authenticate' header set for the response. ```js Boom.unauthorized('invalid password'); ``` Generates the following response: ```json "payload": { "statusCode": 401, "error": "Unauthorized", "message": "invalid password" }, "headers" {} ``` ```js Boom.unauthorized('invalid password', 'sample'); ``` Generates the following response: ```json "payload": { "statusCode": 401, "error": "Unauthorized", "message": "invalid password", "attributes": { "error": "invalid password" } }, "headers" { "WWW-Authenticate": "sample error=\"invalid password\"" } ``` ```js Boom.unauthorized(null, 'Negotiate', 'VGhpcyBpcyBhIHRlc3QgdG9rZW4='); ``` Generates the following response: ```json "payload": { "statusCode": 401, "error": "Unauthorized", "attributes": "VGhpcyBpcyBhIHRlc3QgdG9rZW4=" }, "headers" { "WWW-Authenticate": "Negotiate VGhpcyBpcyBhIHRlc3QgdG9rZW4=" } ``` ```js Boom.unauthorized('invalid password', 'sample', { ttl: 0, cache: null, foo: 'bar' }); ``` Generates the following response: ```json "payload": { "statusCode": 401, "error": "Unauthorized", "message": "invalid password", "attributes": { "error": "invalid password", "ttl": 0, "cache": "", "foo": "bar" } }, "headers" { "WWW-Authenticate": "sample ttl=\"0\", cache=\"\", foo=\"bar\", error=\"invalid password\"" } ``` ##### `Boom.paymentRequired([message], [data])` Returns a 402 Payment Required error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.paymentRequired('bandwidth used'); ``` Generates the following response payload: ```json { "statusCode": 402, "error": "Payment Required", "message": "bandwidth used" } ``` ##### `Boom.forbidden([message], [data])` Returns a 403 Forbidden error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.forbidden('try again some time'); ``` Generates the following response payload: ```json { "statusCode": 403, "error": "Forbidden", "message": "try again some time" } ``` ##### `Boom.notFound([message], [data])` Returns a 404 Not Found error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.notFound('missing'); ``` Generates the following response payload: ```json { "statusCode": 404, "error": "Not Found", "message": "missing" } ``` ##### `Boom.methodNotAllowed([message], [data], [allow])` Returns a 405 Method Not Allowed error where: - `message` - optional message. - `data` - optional additional error data. - `allow` - optional string or array of strings (to be combined and separated by ', ') which is set to the 'Allow' header. ```js Boom.methodNotAllowed('that method is not allowed'); ``` Generates the following response payload: ```json { "statusCode": 405, "error": "Method Not Allowed", "message": "that method is not allowed" } ``` ##### `Boom.notAcceptable([message], [data])` Returns a 406 Not Acceptable error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.notAcceptable('unacceptable'); ``` Generates the following response payload: ```json { "statusCode": 406, "error": "Not Acceptable", "message": "unacceptable" } ``` ##### `Boom.proxyAuthRequired([message], [data])` Returns a 407 Proxy Authentication Required error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.proxyAuthRequired('auth missing'); ``` Generates the following response payload: ```json { "statusCode": 407, "error": "Proxy Authentication Required", "message": "auth missing" } ``` ##### `Boom.clientTimeout([message], [data])` Returns a 408 Request Time-out error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.clientTimeout('timed out'); ``` Generates the following response payload: ```json { "statusCode": 408, "error": "Request Time-out", "message": "timed out" } ``` ##### `Boom.conflict([message], [data])` Returns a 409 Conflict error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.conflict('there was a conflict'); ``` Generates the following response payload: ```json { "statusCode": 409, "error": "Conflict", "message": "there was a conflict" } ``` ##### `Boom.resourceGone([message], [data])` Returns a 410 Gone error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.resourceGone('it is gone'); ``` Generates the following response payload: ```json { "statusCode": 410, "error": "Gone", "message": "it is gone" } ``` ##### `Boom.lengthRequired([message], [data])` Returns a 411 Length Required error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.lengthRequired('length needed'); ``` Generates the following response payload: ```json { "statusCode": 411, "error": "Length Required", "message": "length needed" } ``` ##### `Boom.preconditionFailed([message], [data])` Returns a 412 Precondition Failed error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.preconditionFailed(); ``` Generates the following response payload: ```json { "statusCode": 412, "error": "Precondition Failed" } ``` ##### `Boom.entityTooLarge([message], [data])` Returns a 413 Request Entity Too Large error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.entityTooLarge('too big'); ``` Generates the following response payload: ```json { "statusCode": 413, "error": "Request Entity Too Large", "message": "too big" } ``` ##### `Boom.uriTooLong([message], [data])` Returns a 414 Request-URI Too Large error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.uriTooLong('uri is too long'); ``` Generates the following response payload: ```json { "statusCode": 414, "error": "Request-URI Too Large", "message": "uri is too long" } ``` ##### `Boom.unsupportedMediaType([message], [data])` Returns a 415 Unsupported Media Type error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.unsupportedMediaType('that media is not supported'); ``` Generates the following response payload: ```json { "statusCode": 415, "error": "Unsupported Media Type", "message": "that media is not supported" } ``` ##### `Boom.rangeNotSatisfiable([message], [data])` Returns a 416 Requested Range Not Satisfiable error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.rangeNotSatisfiable(); ``` Generates the following response payload: ```json { "statusCode": 416, "error": "Requested Range Not Satisfiable" } ``` ##### `Boom.expectationFailed([message], [data])` Returns a 417 Expectation Failed error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.expectationFailed('expected this to work'); ``` Generates the following response payload: ```json { "statusCode": 417, "error": "Expectation Failed", "message": "expected this to work" } ``` ##### `Boom.teapot([message], [data])` Returns a 418 I'm a Teapot error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.teapot('sorry, no coffee...'); ``` Generates the following response payload: ```json { "statusCode": 418, "error": "I'm a Teapot", "message": "Sorry, no coffee..." } ``` ##### `Boom.badData([message], [data])` Returns a 422 Unprocessable Entity error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.badData('your data is bad and you should feel bad'); ``` Generates the following response payload: ```json { "statusCode": 422, "error": "Unprocessable Entity", "message": "your data is bad and you should feel bad" } ``` ##### `Boom.locked([message], [data])` Returns a 423 Locked error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.locked('this resource has been locked'); ``` Generates the following response payload: ```json { "statusCode": 423, "error": "Locked", "message": "this resource has been locked" } ``` ##### `Boom.failedDependency([message], [data])` Returns a 424 Failed Dependency error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.failedDependency('an external resource failed'); ``` Generates the following response payload: ```json { "statusCode": 424, "error": "Failed Dependency", "message": "an external resource failed" } ``` ##### `Boom.preconditionRequired([message], [data])` Returns a 428 Precondition Required error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.preconditionRequired('you must supply an If-Match header'); ``` Generates the following response payload: ```json { "statusCode": 428, "error": "Precondition Required", "message": "you must supply an If-Match header" } ``` ##### `Boom.tooManyRequests([message], [data])` Returns a 429 Too Many Requests error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.tooManyRequests('you have exceeded your request limit'); ``` Generates the following response payload: ```json { "statusCode": 429, "error": "Too Many Requests", "message": "you have exceeded your request limit" } ``` ##### `Boom.illegal([message], [data])` Returns a 451 Unavailable For Legal Reasons error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.illegal('you are not permitted to view this resource for legal reasons'); ``` Generates the following response payload: ```json { "statusCode": 451, "error": "Unavailable For Legal Reasons", "message": "you are not permitted to view this resource for legal reasons" } ``` #### HTTP 5xx Errors All 500 errors hide your message from the end user. Your message is recorded in the server log. ##### `Boom.badImplementation([message], [data])` - (*alias: `internal`*) Returns a 500 Internal Server Error error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.badImplementation('terrible implementation'); ``` Generates the following response payload: ```json { "statusCode": 500, "error": "Internal Server Error", "message": "An internal server error occurred" } ``` ##### `Boom.notImplemented([message], [data])` Returns a 501 Not Implemented error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.notImplemented('method not implemented'); ``` Generates the following response payload: ```json { "statusCode": 501, "error": "Not Implemented", "message": "method not implemented" } ``` ##### `Boom.badGateway([message], [data])` Returns a 502 Bad Gateway error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.badGateway('that is a bad gateway'); ``` Generates the following response payload: ```json { "statusCode": 502, "error": "Bad Gateway", "message": "that is a bad gateway" } ``` ##### `Boom.serverUnavailable([message], [data])` Returns a 503 Service Unavailable error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.serverUnavailable('unavailable'); ``` Generates the following response payload: ```json { "statusCode": 503, "error": "Service Unavailable", "message": "unavailable" } ``` ##### `Boom.gatewayTimeout([message], [data])` Returns a 504 Gateway Time-out error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.gatewayTimeout(); ``` Generates the following response payload: ```json { "statusCode": 504, "error": "Gateway Time-out" } ``` #### F.A.Q. **Q** How do I include extra information in my responses? `output.payload` is missing `data`, what gives? **A** There is a reason the values passed back in the response payloads are pretty locked down. It's mostly for security and to not leak any important information back to the client. This means you will need to put in a little more effort to include extra information about your custom error. Check out the ["Error transformation"](https://github.com/hapijs/hapi/blob/master/API.md#error-transformation) section in the hapi documentation. boom-7.4.3/CHANGELOG.md000066400000000000000000000005421352471156500143040ustar00rootroot00000000000000Breaking changes are documented using GitHub issues, see [issues labeled "release notes"](https://github.com/hapijs/boom/issues?q=is%3Aissue+label%3A%22release+notes%22). If you want changes of a specific minor or patch release, you can browse the [GitHub milestones](https://github.com/hapijs/boom/milestones?state=closed&direction=asc&sort=due_date). boom-7.4.3/LICENSE.md000077500000000000000000000027411352471156500141050ustar00rootroot00000000000000Copyright (c) 2012-2019, Sideway Inc, and project contributors Copyright (c) 2012-2014, Walmart. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The names of any contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS OFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. boom-7.4.3/README.md000077500000000000000000000005121352471156500137520ustar00rootroot00000000000000 # boom HTTP-friendly error objects [![Build Status](https://secure.travis-ci.org/hapijs/boom.svg)](http://travis-ci.org/hapijs/boom) ## Documentation [**API Reference**](API.md) boom-7.4.3/lib/000077500000000000000000000000001352471156500132405ustar00rootroot00000000000000boom-7.4.3/lib/index.js000077500000000000000000000310531352471156500147120ustar00rootroot00000000000000'use strict'; const Hoek = require('@hapi/hoek'); const internals = { codes: new Map([ [100, 'Continue'], [101, 'Switching Protocols'], [102, 'Processing'], [200, 'OK'], [201, 'Created'], [202, 'Accepted'], [203, 'Non-Authoritative Information'], [204, 'No Content'], [205, 'Reset Content'], [206, 'Partial Content'], [207, 'Multi-Status'], [300, 'Multiple Choices'], [301, 'Moved Permanently'], [302, 'Moved Temporarily'], [303, 'See Other'], [304, 'Not Modified'], [305, 'Use Proxy'], [307, 'Temporary Redirect'], [400, 'Bad Request'], [401, 'Unauthorized'], [402, 'Payment Required'], [403, 'Forbidden'], [404, 'Not Found'], [405, 'Method Not Allowed'], [406, 'Not Acceptable'], [407, 'Proxy Authentication Required'], [408, 'Request Time-out'], [409, 'Conflict'], [410, 'Gone'], [411, 'Length Required'], [412, 'Precondition Failed'], [413, 'Request Entity Too Large'], [414, 'Request-URI Too Large'], [415, 'Unsupported Media Type'], [416, 'Requested Range Not Satisfiable'], [417, 'Expectation Failed'], [418, 'I\'m a teapot'], [422, 'Unprocessable Entity'], [423, 'Locked'], [424, 'Failed Dependency'], [425, 'Unordered Collection'], [426, 'Upgrade Required'], [428, 'Precondition Required'], [429, 'Too Many Requests'], [431, 'Request Header Fields Too Large'], [451, 'Unavailable For Legal Reasons'], [500, 'Internal Server Error'], [501, 'Not Implemented'], [502, 'Bad Gateway'], [503, 'Service Unavailable'], [504, 'Gateway Time-out'], [505, 'HTTP Version Not Supported'], [506, 'Variant Also Negotiates'], [507, 'Insufficient Storage'], [509, 'Bandwidth Limit Exceeded'], [510, 'Not Extended'], [511, 'Network Authentication Required'] ]) }; module.exports = internals.Boom = class extends Error { static [Symbol.hasInstance](instance) { return internals.Boom.isBoom(instance); } constructor(message, options = {}) { if (message instanceof Error) { return internals.Boom.boomify(Hoek.clone(message), options); } const { statusCode = 500, data = null, ctor = internals.Boom } = options; const error = new Error(message ? message : undefined); // Avoids settings null message Error.captureStackTrace(error, ctor); // Filter the stack to our external API error.data = data; internals.initialize(error, statusCode); error.typeof = ctor; if (options.decorate) { Object.assign(error, options.decorate); } return error; } static isBoom(err) { return (err instanceof Error && !!err.isBoom); } static boomify(err, options) { Hoek.assert(err instanceof Error, 'Cannot wrap non-Error object'); options = options || {}; if (options.data !== undefined) { err.data = options.data; } if (options.decorate) { Object.assign(err, options.decorate); } if (!err.isBoom) { return internals.initialize(err, options.statusCode || 500, options.message); } if (options.override === false || // Defaults to true (!options.statusCode && !options.message)) { return err; } return internals.initialize(err, options.statusCode || err.output.statusCode, options.message); } // 4xx Client Errors static badRequest(message, data) { return new internals.Boom(message, { statusCode: 400, data, ctor: internals.Boom.badRequest }); } static unauthorized(message, scheme, attributes) { // Or function (message, wwwAuthenticate[]) const err = new internals.Boom(message, { statusCode: 401, ctor: internals.Boom.unauthorized }); if (!scheme) { return err; } let wwwAuthenticate = ''; if (typeof scheme === 'string') { // function (message, scheme, attributes) wwwAuthenticate = scheme; if (attributes || message) { err.output.payload.attributes = {}; } if (attributes) { if (typeof attributes === 'string') { wwwAuthenticate = wwwAuthenticate + ' ' + Hoek.escapeHeaderAttribute(attributes); err.output.payload.attributes = attributes; } else { const names = Object.keys(attributes); for (let i = 0; i < names.length; ++i) { const name = names[i]; if (i) { wwwAuthenticate = wwwAuthenticate + ','; } let value = attributes[name]; if (value === null || value === undefined) { // Value can be zero value = ''; } wwwAuthenticate = wwwAuthenticate + ' ' + name + '="' + Hoek.escapeHeaderAttribute(value.toString()) + '"'; err.output.payload.attributes[name] = value; } } } if (message) { if (attributes) { wwwAuthenticate = wwwAuthenticate + ','; } wwwAuthenticate = wwwAuthenticate + ' error="' + Hoek.escapeHeaderAttribute(message) + '"'; err.output.payload.attributes.error = message; } else { err.isMissing = true; } } else { // function (message, wwwAuthenticate[]) const wwwArray = scheme; for (let i = 0; i < wwwArray.length; ++i) { if (i) { wwwAuthenticate = wwwAuthenticate + ', '; } wwwAuthenticate = wwwAuthenticate + wwwArray[i]; } } err.output.headers['WWW-Authenticate'] = wwwAuthenticate; return err; } static paymentRequired(message, data) { return new internals.Boom(message, { statusCode: 402, data, ctor: internals.Boom.paymentRequired }); } static forbidden(message, data) { return new internals.Boom(message, { statusCode: 403, data, ctor: internals.Boom.forbidden }); } static notFound(message, data) { return new internals.Boom(message, { statusCode: 404, data, ctor: internals.Boom.notFound }); } static methodNotAllowed(message, data, allow) { const err = new internals.Boom(message, { statusCode: 405, data, ctor: internals.Boom.methodNotAllowed }); if (typeof allow === 'string') { allow = [allow]; } if (Array.isArray(allow)) { err.output.headers.Allow = allow.join(', '); } return err; } static notAcceptable(message, data) { return new internals.Boom(message, { statusCode: 406, data, ctor: internals.Boom.notAcceptable }); } static proxyAuthRequired(message, data) { return new internals.Boom(message, { statusCode: 407, data, ctor: internals.Boom.proxyAuthRequired }); } static clientTimeout(message, data) { return new internals.Boom(message, { statusCode: 408, data, ctor: internals.Boom.clientTimeout }); } static conflict(message, data) { return new internals.Boom(message, { statusCode: 409, data, ctor: internals.Boom.conflict }); } static resourceGone(message, data) { return new internals.Boom(message, { statusCode: 410, data, ctor: internals.Boom.resourceGone }); } static lengthRequired(message, data) { return new internals.Boom(message, { statusCode: 411, data, ctor: internals.Boom.lengthRequired }); } static preconditionFailed(message, data) { return new internals.Boom(message, { statusCode: 412, data, ctor: internals.Boom.preconditionFailed }); } static entityTooLarge(message, data) { return new internals.Boom(message, { statusCode: 413, data, ctor: internals.Boom.entityTooLarge }); } static uriTooLong(message, data) { return new internals.Boom(message, { statusCode: 414, data, ctor: internals.Boom.uriTooLong }); } static unsupportedMediaType(message, data) { return new internals.Boom(message, { statusCode: 415, data, ctor: internals.Boom.unsupportedMediaType }); } static rangeNotSatisfiable(message, data) { return new internals.Boom(message, { statusCode: 416, data, ctor: internals.Boom.rangeNotSatisfiable }); } static expectationFailed(message, data) { return new internals.Boom(message, { statusCode: 417, data, ctor: internals.Boom.expectationFailed }); } static teapot(message, data) { return new internals.Boom(message, { statusCode: 418, data, ctor: internals.Boom.teapot }); } static badData(message, data) { return new internals.Boom(message, { statusCode: 422, data, ctor: internals.Boom.badData }); } static locked(message, data) { return new internals.Boom(message, { statusCode: 423, data, ctor: internals.Boom.locked }); } static failedDependency(message, data) { return new internals.Boom(message, { statusCode: 424, data, ctor: internals.Boom.failedDependency }); } static preconditionRequired(message, data) { return new internals.Boom(message, { statusCode: 428, data, ctor: internals.Boom.preconditionRequired }); } static tooManyRequests(message, data) { return new internals.Boom(message, { statusCode: 429, data, ctor: internals.Boom.tooManyRequests }); } static illegal(message, data) { return new internals.Boom(message, { statusCode: 451, data, ctor: internals.Boom.illegal }); } // 5xx Server Errors static internal(message, data, statusCode = 500) { return internals.serverError(message, data, statusCode, internals.Boom.internal); } static notImplemented(message, data) { return internals.serverError(message, data, 501, internals.Boom.notImplemented); } static badGateway(message, data) { return internals.serverError(message, data, 502, internals.Boom.badGateway); } static serverUnavailable(message, data) { return internals.serverError(message, data, 503, internals.Boom.serverUnavailable); } static gatewayTimeout(message, data) { return internals.serverError(message, data, 504, internals.Boom.gatewayTimeout); } static badImplementation(message, data) { const err = internals.serverError(message, data, 500, internals.Boom.badImplementation); err.isDeveloperError = true; return err; } }; internals.initialize = function (err, statusCode, message) { const numberCode = parseInt(statusCode, 10); Hoek.assert(!isNaN(numberCode) && numberCode >= 400, 'First argument must be a number (400+):', statusCode); err.isBoom = true; err.isServer = numberCode >= 500; if (!err.hasOwnProperty('data')) { err.data = null; } err.output = { statusCode: numberCode, payload: {}, headers: {} }; err.reformat = internals.reformat; if (!message && !err.message) { err.reformat(); message = err.output.payload.error; } if (message) { err.message = (message + (err.message ? ': ' + err.message : '')); err.output.payload.message = err.message; } err.reformat(); return err; }; internals.reformat = function (debug = false) { this.output.payload.statusCode = this.output.statusCode; this.output.payload.error = internals.codes.get(this.output.statusCode) || 'Unknown'; if (this.output.statusCode === 500 && debug !== true) { this.output.payload.message = 'An internal server error occurred'; // Hide actual error from user } else if (this.message) { this.output.payload.message = this.message; } }; internals.serverError = function (message, data, statusCode, ctor) { if (data instanceof Error && !data.isBoom) { return internals.Boom.boomify(data, { statusCode, message }); } return new internals.Boom(message, { statusCode, data, ctor }); }; boom-7.4.3/package.json000066400000000000000000000007751352471156500147710ustar00rootroot00000000000000{ "name": "@hapi/boom", "description": "HTTP-friendly error objects", "version": "7.4.3", "repository": "git://github.com/hapijs/boom", "main": "lib/index.js", "keywords": [ "error", "http" ], "dependencies": { "@hapi/hoek": "8.x.x" }, "devDependencies": { "@hapi/code": "6.x.x", "@hapi/lab": "20.x.x" }, "scripts": { "test": "lab -a @hapi/code -t 100 -L", "test-cov-html": "lab -a @hapi/code -r html -o coverage.html -L" }, "license": "BSD-3-Clause" } boom-7.4.3/test/000077500000000000000000000000001352471156500134515ustar00rootroot00000000000000boom-7.4.3/test/index.js000077500000000000000000000740651352471156500151350ustar00rootroot00000000000000'use strict'; const Boom = require('..'); const Code = require('@hapi/code'); const Lab = require('@hapi/lab'); const internals = {}; const { describe, it } = exports.lab = Lab.script(); const expect = Code.expect; describe('Boom', () => { it('constructs error object (new)', () => { const err = new Boom('oops', { statusCode: 400 }); expect(err.output.payload.message).to.equal('oops'); expect(err.output.statusCode).to.equal(400); }); it('clones error object', () => { const oops = new Error('oops'); const err = new Boom(oops, { statusCode: 400 }); expect(err).to.not.shallow.equal(oops); expect(err.output.payload.message).to.equal('oops'); expect(err.output.statusCode).to.equal(400); }); it('decorates error', () => { const err = new Boom('oops', { statusCode: 400, decorate: { x: 1 } }); expect(err.output.payload.message).to.equal('oops'); expect(err.output.statusCode).to.equal(400); expect(err.x).to.equal(1); }); it('throws when statusCode is not a number', () => { expect(() => { new Boom('message', { statusCode: 'x' }); }).to.throw('First argument must be a number (400+): x'); }); it('will cast a number-string to an integer', () => { const codes = [ { input: '404', result: 404 }, { input: '404.1', result: 404 }, { input: 400, result: 400 }, { input: 400.123, result: 400 } ]; for (let i = 0; i < codes.length; ++i) { const code = codes[i]; const err = new Boom('', { statusCode: code.input }); expect(err.output.statusCode).to.equal(code.result); } }); it('throws when statusCode is not finite', () => { expect(() => { new Boom('', { statusCode: 1 / 0 }); }).to.throw('First argument must be a number (400+): null'); }); it('sets error code to unknown', () => { const err = new Boom('', { statusCode: 999 }); expect(err.output.payload.error).to.equal('Unknown'); }); describe('instanceof', () => { it('identifies a boom object', () => { expect(new Boom('oops') instanceof Boom).to.be.true(); expect(Boom.badRequest('oops') instanceof Boom).to.be.true(); expect(new Error('oops') instanceof Boom).to.be.false(); expect(Boom.boomify(new Error('oops')) instanceof Boom).to.be.true(); expect({ isBoom: true } instanceof Boom).to.be.false(); expect(null instanceof Boom).to.be.false(); }); }); describe('isBoom()', () => { it('identifies a boom object', () => { expect(Boom.isBoom(new Boom('oops'))).to.be.true(); expect(Boom.isBoom(new Error('oops'))).to.be.false(); expect(Boom.isBoom({ isBoom: true })).to.be.false(); expect(Boom.isBoom(null)).to.be.false(); }); }); describe('boomify()', () => { it('returns the same object when already boom', () => { const error = Boom.badRequest(); expect(error).to.equal(Boom.boomify(error)); expect(error).to.equal(Boom.boomify(error, { statusCode: 444 })); }); it('decorates error', () => { const err = new Error('oops'); Boom.boomify(err, { statusCode: 400, decorate: { x: 1 } }); expect(err.x).to.equal(1); }); it('returns an error with info when constructed using another error', () => { const error = new Error('ka-boom'); error.xyz = 123; const err = Boom.boomify(error); expect(err.xyz).to.equal(123); expect(err.message).to.equal('ka-boom'); expect(err.output).to.equal({ statusCode: 500, payload: { statusCode: 500, error: 'Internal Server Error', message: 'An internal server error occurred' }, headers: {} }); expect(err.data).to.equal(null); }); it('does not override data when constructed using another error', () => { const error = new Error('ka-boom'); error.data = { useful: 'data' }; const err = Boom.boomify(error); expect(err.data).to.equal(error.data); }); it('sets new message when none exists', () => { const error = new Error(); const wrapped = Boom.boomify(error, { statusCode: 400, message: 'something bad' }); expect(wrapped.message).to.equal('something bad'); }); it('returns boom error unchanged', () => { const error = Boom.badRequest('Missing data', { type: 'user' }); const boom = Boom.boomify(error); expect(boom).to.shallow.equal(error); expect(error.data.type).to.equal('user'); expect(error.output.payload.message).to.equal('Missing data'); expect(error.output.statusCode).to.equal(400); }); it('defaults to 500', () => { const error = new Error('Missing data'); const boom = Boom.boomify(error); expect(boom).to.shallow.equal(error); expect(error.output.payload.message).to.equal('An internal server error occurred'); expect(error.output.statusCode).to.equal(500); }); it('overrides message and statusCode', () => { const error = Boom.badRequest('Missing data', { type: 'user' }); const boom = Boom.boomify(error, { message: 'Override message', statusCode: 599 }); expect(boom).to.shallow.equal(error); expect(error.data.type).to.equal('user'); expect(error.output.payload.message).to.equal('Override message: Missing data'); expect(error.output.statusCode).to.equal(599); }); it('overrides message', () => { const error = Boom.badRequest('Missing data', { type: 'user' }); const boom = Boom.boomify(error, { message: 'Override message' }); expect(boom).to.shallow.equal(error); expect(error.data.type).to.equal('user'); expect(error.output.payload.message).to.equal('Override message: Missing data'); expect(error.output.statusCode).to.equal(400); }); it('overrides statusCode', () => { const error = Boom.badRequest('Missing data', { type: 'user' }); const boom = Boom.boomify(error, { statusCode: 599 }); expect(boom).to.shallow.equal(error); expect(error.data.type).to.equal('user'); expect(error.output.payload.message).to.equal('Missing data'); expect(error.output.statusCode).to.equal(599); }); it('skips override', () => { const error = Boom.badRequest('Missing data', { type: 'user' }); const boom = Boom.boomify(error, { message: 'Override message', statusCode: 599, override: false }); expect(boom).to.shallow.equal(error); expect(error.data.type).to.equal('user'); expect(error.output.payload.message).to.equal('Missing data'); expect(error.output.statusCode).to.equal(400); }); it('initializes plain error', () => { const error = new Error('Missing data'); const boom = Boom.boomify(error, { message: 'Override message', statusCode: 599, override: false }); expect(boom).to.shallow.equal(error); expect(error.output.payload.message).to.equal('Override message: Missing data'); expect(error.output.statusCode).to.equal(599); }); }); describe('create()', () => { it('does not sets null message', () => { const error = Boom.unauthorized(null); expect(error.output.payload.message).to.equal('Unauthorized'); expect(error.isServer).to.be.false(); }); it('sets message and data', () => { const error = Boom.badRequest('Missing data', { type: 'user' }); expect(error.data.type).to.equal('user'); expect(error.output.payload.message).to.equal('Missing data'); }); }); describe('initialize()', () => { it('does not sets null message', () => { const err = new Error('some error message'); const boom = Boom.boomify(err, { statusCode: 400, message: 'modified error message' }); expect(boom.output.payload.message).to.equal('modified error message: some error message'); }); }); describe('isBoom()', () => { it('returns true for Boom object', () => { expect(Boom.badRequest().isBoom).to.equal(true); }); it('returns false for Error object', () => { expect((new Error()).isBoom).to.not.exist(); }); }); describe('badRequest()', () => { it('returns a 400 error statusCode', () => { const error = Boom.badRequest(); expect(error.output.statusCode).to.equal(400); expect(error.isServer).to.be.false(); }); it('sets the message with the passed in message', () => { expect(Boom.badRequest('my message').message).to.equal('my message'); }); it('sets the message to HTTP status if none provided', () => { expect(Boom.badRequest().message).to.equal('Bad Request'); }); }); describe('unauthorized()', () => { it('returns a 401 error statusCode', () => { const err = Boom.unauthorized(); expect(err.output.statusCode).to.equal(401); expect(err.output.headers).to.equal({}); }); it('sets the message with the passed in message', () => { expect(Boom.unauthorized('my message').message).to.equal('my message'); }); it('returns a WWW-Authenticate header when passed a scheme', () => { const err = Boom.unauthorized('boom', 'Test'); expect(err.output.statusCode).to.equal(401); expect(err.output.headers['WWW-Authenticate']).to.equal('Test error="boom"'); }); it('returns a WWW-Authenticate header set to the schema array value', () => { const err = Boom.unauthorized(null, ['Test', 'one', 'two']); expect(err.output.statusCode).to.equal(401); expect(err.output.headers['WWW-Authenticate']).to.equal('Test, one, two'); }); it('returns a WWW-Authenticate header when passed a scheme and attributes', () => { const err = Boom.unauthorized('boom', 'Test', { a: 1, b: 'something', c: null, d: 0 }); expect(err.output.statusCode).to.equal(401); expect(err.output.headers['WWW-Authenticate']).to.equal('Test a="1", b="something", c="", d="0", error="boom"'); expect(err.output.payload.attributes).to.equal({ a: 1, b: 'something', c: '', d: 0, error: 'boom' }); }); it('returns a WWW-Authenticate header from string input instead of object', () => { const err = Boom.unauthorized(null, 'Negotiate', 'VGhpcyBpcyBhIHRlc3QgdG9rZW4='); expect(err.output.statusCode).to.equal(401); expect(err.output.headers['WWW-Authenticate']).to.equal('Negotiate VGhpcyBpcyBhIHRlc3QgdG9rZW4='); expect(err.output.payload.attributes).to.equal('VGhpcyBpcyBhIHRlc3QgdG9rZW4='); }); it('returns a WWW-Authenticate header when passed attributes, missing error', () => { const err = Boom.unauthorized(null, 'Test', { a: 1, b: 'something', c: null, d: 0, e: undefined }); expect(err.output.statusCode).to.equal(401); expect(err.output.headers['WWW-Authenticate']).to.equal('Test a="1", b="something", c="", d="0", e=""'); expect(err.isMissing).to.equal(true); }); it('sets the isMissing flag when error message is empty', () => { const err = Boom.unauthorized('', 'Basic'); expect(err.isMissing).to.equal(true); }); it('does not set the isMissing flag when error message is not empty', () => { const err = Boom.unauthorized('message', 'Basic'); expect(err.isMissing).to.equal(undefined); }); it('sets a WWW-Authenticate when passed as an array', () => { const err = Boom.unauthorized('message', ['Basic', 'Example e="1"', 'Another x="3", y="4"']); expect(err.output.headers['WWW-Authenticate']).to.equal('Basic, Example e="1", Another x="3", y="4"'); }); }); describe('paymentRequired()', () => { it('returns a 402 error statusCode', () => { expect(Boom.paymentRequired().output.statusCode).to.equal(402); }); it('sets the message with the passed in message', () => { expect(Boom.paymentRequired('my message').message).to.equal('my message'); }); it('sets the message to HTTP status if none provided', () => { expect(Boom.paymentRequired().message).to.equal('Payment Required'); }); }); describe('methodNotAllowed()', () => { it('returns a 405 error statusCode', () => { expect(Boom.methodNotAllowed().output.statusCode).to.equal(405); }); it('sets the message with the passed in message', () => { expect(Boom.methodNotAllowed('my message').message).to.equal('my message'); }); it('returns an Allow header when passed a string', () => { const err = Boom.methodNotAllowed('my message', null, 'GET'); expect(err.output.statusCode).to.equal(405); expect(err.output.headers.Allow).to.equal('GET'); }); it('returns an Allow header when passed an array', () => { const err = Boom.methodNotAllowed('my message', null, ['GET', 'POST']); expect(err.output.statusCode).to.equal(405); expect(err.output.headers.Allow).to.equal('GET, POST'); }); }); describe('notAcceptable()', () => { it('returns a 406 error statusCode', () => { expect(Boom.notAcceptable().output.statusCode).to.equal(406); }); it('sets the message with the passed in message', () => { expect(Boom.notAcceptable('my message').message).to.equal('my message'); }); }); describe('proxyAuthRequired()', () => { it('returns a 407 error statusCode', () => { expect(Boom.proxyAuthRequired().output.statusCode).to.equal(407); }); it('sets the message with the passed in message', () => { expect(Boom.proxyAuthRequired('my message').message).to.equal('my message'); }); }); describe('clientTimeout()', () => { it('returns a 408 error statusCode', () => { expect(Boom.clientTimeout().output.statusCode).to.equal(408); }); it('sets the message with the passed in message', () => { expect(Boom.clientTimeout('my message').message).to.equal('my message'); }); }); describe('conflict()', () => { it('returns a 409 error statusCode', () => { expect(Boom.conflict().output.statusCode).to.equal(409); }); it('sets the message with the passed in message', () => { expect(Boom.conflict('my message').message).to.equal('my message'); }); }); describe('resourceGone()', () => { it('returns a 410 error statusCode', () => { expect(Boom.resourceGone().output.statusCode).to.equal(410); }); it('sets the message with the passed in message', () => { expect(Boom.resourceGone('my message').message).to.equal('my message'); }); }); describe('lengthRequired()', () => { it('returns a 411 error statusCode', () => { expect(Boom.lengthRequired().output.statusCode).to.equal(411); }); it('sets the message with the passed in message', () => { expect(Boom.lengthRequired('my message').message).to.equal('my message'); }); }); describe('preconditionFailed()', () => { it('returns a 412 error statusCode', () => { expect(Boom.preconditionFailed().output.statusCode).to.equal(412); }); it('sets the message with the passed in message', () => { expect(Boom.preconditionFailed('my message').message).to.equal('my message'); }); }); describe('entityTooLarge()', () => { it('returns a 413 error statusCode', () => { expect(Boom.entityTooLarge().output.statusCode).to.equal(413); }); it('sets the message with the passed in message', () => { expect(Boom.entityTooLarge('my message').message).to.equal('my message'); }); }); describe('uriTooLong()', () => { it('returns a 414 error statusCode', () => { expect(Boom.uriTooLong().output.statusCode).to.equal(414); }); it('sets the message with the passed in message', () => { expect(Boom.uriTooLong('my message').message).to.equal('my message'); }); }); describe('unsupportedMediaType()', () => { it('returns a 415 error statusCode', () => { expect(Boom.unsupportedMediaType().output.statusCode).to.equal(415); }); it('sets the message with the passed in message', () => { expect(Boom.unsupportedMediaType('my message').message).to.equal('my message'); }); }); describe('rangeNotSatisfiable()', () => { it('returns a 416 error statusCode', () => { expect(Boom.rangeNotSatisfiable().output.statusCode).to.equal(416); }); it('sets the message with the passed in message', () => { expect(Boom.rangeNotSatisfiable('my message').message).to.equal('my message'); }); }); describe('expectationFailed()', () => { it('returns a 417 error statusCode', () => { expect(Boom.expectationFailed().output.statusCode).to.equal(417); }); it('sets the message with the passed in message', () => { expect(Boom.expectationFailed('my message').message).to.equal('my message'); }); }); describe('teapot()', () => { it('returns a 418 error statusCode', () => { expect(Boom.teapot().output.statusCode).to.equal(418); }); it('sets the message with the passed in message', () => { expect(Boom.teapot('Sorry, no coffee...').message).to.equal('Sorry, no coffee...'); }); }); describe('badData()', () => { it('returns a 422 error statusCode', () => { expect(Boom.badData().output.statusCode).to.equal(422); }); it('sets the message with the passed in message', () => { expect(Boom.badData('my message').message).to.equal('my message'); }); }); describe('locked()', () => { it('returns a 423 error statusCode', () => { expect(Boom.locked().output.statusCode).to.equal(423); }); it('sets the message with the passed in message', () => { expect(Boom.locked('my message').message).to.equal('my message'); }); }); describe('failedDependency()', () => { it('returns a 424 error statusCode', () => { expect(Boom.failedDependency().output.statusCode).to.equal(424); }); it('sets the message with the passed in message', () => { expect(Boom.failedDependency('my message').message).to.equal('my message'); }); }); describe('preconditionRequired()', () => { it('returns a 428 error statusCode', () => { expect(Boom.preconditionRequired().output.statusCode).to.equal(428); }); it('sets the message with the passed in message', () => { expect(Boom.preconditionRequired('my message').message).to.equal('my message'); }); }); describe('tooManyRequests()', () => { it('returns a 429 error statusCode', () => { expect(Boom.tooManyRequests().output.statusCode).to.equal(429); }); it('sets the message with the passed-in message', () => { expect(Boom.tooManyRequests('my message').message).to.equal('my message'); }); }); describe('illegal()', () => { it('returns a 451 error statusCode', () => { expect(Boom.illegal().output.statusCode).to.equal(451); }); it('sets the message with the passed-in message', () => { expect(Boom.illegal('my message').message).to.equal('my message'); }); }); describe('serverUnavailable()', () => { it('returns a 503 error statusCode', () => { expect(Boom.serverUnavailable().output.statusCode).to.equal(503); }); it('sets the message with the passed in message', () => { expect(Boom.serverUnavailable('my message').message).to.equal('my message'); }); }); describe('forbidden()', () => { it('returns a 403 error statusCode', () => { expect(Boom.forbidden().output.statusCode).to.equal(403); }); it('sets the message with the passed in message', () => { expect(Boom.forbidden('my message').message).to.equal('my message'); }); }); describe('notFound()', () => { it('returns a 404 error statusCode', () => { expect(Boom.notFound().output.statusCode).to.equal(404); }); it('sets the message with the passed in message', () => { expect(Boom.notFound('my message').message).to.equal('my message'); }); }); describe('internal()', () => { it('returns a 500 error statusCode', () => { expect(Boom.internal().output.statusCode).to.equal(500); }); it('sets the message with the passed in message', () => { const err = Boom.internal('my message'); expect(err.message).to.equal('my message'); expect(err.isServer).to.true(); expect(err.output.payload.message).to.equal('An internal server error occurred'); }); it('passes data on the callback if its passed in', () => { expect(Boom.internal('my message', { my: 'data' }).data.my).to.equal('data'); }); it('returns an error with composite message', () => { const x = {}; try { x.foo(); } catch (err) { const boom = Boom.internal('Someting bad', err); expect(boom.message).to.equal('Someting bad: x.foo is not a function'); expect(boom.isServer).to.be.true(); } }); }); describe('notImplemented()', () => { it('returns a 501 error statusCode', () => { expect(Boom.notImplemented().output.statusCode).to.equal(501); }); it('sets the message with the passed in message', () => { expect(Boom.notImplemented('my message').message).to.equal('my message'); }); }); describe('badGateway()', () => { it('returns a 502 error statusCode', () => { expect(Boom.badGateway().output.statusCode).to.equal(502); }); it('sets the message with the passed in message', () => { expect(Boom.badGateway('my message').message).to.equal('my message'); }); it('retains source boom error as data when wrapped', () => { const upstream = Boom.serverUnavailable(); const boom = Boom.badGateway('Upstream error', upstream); expect(boom.output.statusCode).to.equal(502); expect(boom.data).to.equal(upstream); }); }); describe('gatewayTimeout()', () => { it('returns a 504 error statusCode', () => { expect(Boom.gatewayTimeout().output.statusCode).to.equal(504); }); it('sets the message with the passed in message', () => { expect(Boom.gatewayTimeout('my message').message).to.equal('my message'); }); }); describe('badImplementation()', () => { it('returns a 500 error statusCode', () => { const err = Boom.badImplementation(); expect(err.output.statusCode).to.equal(500); expect(err.isDeveloperError).to.equal(true); expect(err.isServer).to.be.true(); }); it('hides error from user when error data is included', () => { const err = Boom.badImplementation('Invalid', new Error('kaboom')); expect(err.output).to.equal({ headers: {}, statusCode: 500, payload: { error: 'Internal Server Error', message: 'An internal server error occurred', statusCode: 500 } }); }); it('hides error from user when error data is included (boom)', () => { const err = Boom.badImplementation('Invalid', Boom.badRequest('kaboom')); expect(err.isDeveloperError).to.equal(true); expect(err.output).to.equal({ headers: {}, statusCode: 500, payload: { error: 'Internal Server Error', message: 'An internal server error occurred', statusCode: 500 } }); }); }); describe('stack trace', () => { it('should omit lib', () => { ['badRequest', 'unauthorized', 'forbidden', 'notFound', 'methodNotAllowed', 'notAcceptable', 'proxyAuthRequired', 'clientTimeout', 'conflict', 'resourceGone', 'lengthRequired', 'preconditionFailed', 'entityTooLarge', 'uriTooLong', 'unsupportedMediaType', 'rangeNotSatisfiable', 'expectationFailed', 'badData', 'preconditionRequired', 'tooManyRequests', // 500s 'internal', 'notImplemented', 'badGateway', 'serverUnavailable', 'gatewayTimeout', 'badImplementation' ].forEach((name) => { const err = Boom[name](); expect(err.stack).to.not.match(/\/lib\/index\.js/); }); }); }); describe('method with error object instead of message', () => { [ 'badRequest', 'unauthorized', 'forbidden', 'notFound', 'methodNotAllowed', 'notAcceptable', 'proxyAuthRequired', 'clientTimeout', 'conflict', 'resourceGone', 'lengthRequired', 'preconditionFailed', 'entityTooLarge', 'uriTooLong', 'unsupportedMediaType', 'rangeNotSatisfiable', 'expectationFailed', 'badData', 'preconditionRequired', 'tooManyRequests', 'internal', 'notImplemented', 'badGateway', 'serverUnavailable', 'gatewayTimeout', 'badImplementation' ].forEach((name) => { it(`should allow \`Boom${name}(err)\` and preserve the error`, () => { const error = new Error('An example mongoose validation error'); error.name = 'ValidationError'; const err = Boom[name](error); expect(err.name).to.equal('ValidationError'); expect(err.message).to.equal('An example mongoose validation error'); }); // exclude unauthorized if (name !== 'unauthorized') { it(`should allow \`Boom.${name}(err, data)\` and preserve the data`, () => { const error = new Error(); const err = Boom[name](error, { foo: 'bar' }); expect(err.data).to.equal({ foo: 'bar' }); }); } }); }); describe('error.typeof', () => { const types = [ 'badRequest', 'unauthorized', 'forbidden', 'notFound', 'methodNotAllowed', 'notAcceptable', 'proxyAuthRequired', 'clientTimeout', 'conflict', 'resourceGone', 'lengthRequired', 'preconditionFailed', 'entityTooLarge', 'uriTooLong', 'unsupportedMediaType', 'rangeNotSatisfiable', 'expectationFailed', 'badData', 'preconditionRequired', 'tooManyRequests', 'internal', 'notImplemented', 'badGateway', 'serverUnavailable', 'gatewayTimeout', 'badImplementation' ]; types.forEach((name) => { it(`matches typeof Boom.${name}`, () => { const error = Boom[name](); types.forEach((type) => { if (type === name) { expect(error.typeof).to.equal(Boom[name]); } else { expect(error.typeof).to.not.equal(Boom[type]); } }); }); }); }); describe('reformat()', () => { it('displays internal server error messages in debug mode', () => { const error = new Error('ka-boom'); const err = Boom.boomify(error, { statusCode: 500 }); err.reformat(false); expect(err.output).to.equal({ statusCode: 500, payload: { statusCode: 500, error: 'Internal Server Error', message: 'An internal server error occurred' }, headers: {} }); err.reformat(true); expect(err.output).to.equal({ statusCode: 500, payload: { statusCode: 500, error: 'Internal Server Error', message: 'ka-boom' }, headers: {} }); }); }); });