├── .npmignore ├── images └── boom.png ├── .travis.yml ├── CONTRIBUTING.md ├── .gitignore ├── generate-toc.js ├── package.json ├── LICENSE ├── lib └── index.js ├── README.md └── test └── index.js /.npmignore: -------------------------------------------------------------------------------- 1 | * 2 | !lib/** -------------------------------------------------------------------------------- /images/boom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tkh44/boom/master/images/boom.png -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - "8" 5 | - "9" 6 | - "node" 7 | 8 | sudo: false 9 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Please view our [hapijs contributing guide](https://github.com/hapijs/hapi/blob/master/.github/CONTRIBUTING.md). 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | npm-debug.log 4 | dump.rdb 5 | node_modules 6 | results.tap 7 | results.xml 8 | config.json 9 | .DS_Store 10 | */.DS_Store 11 | */*/.DS_Store 12 | ._* 13 | */._* 14 | */*/._* 15 | coverage.* 16 | .settings 17 | package-lock.json 18 | 19 | -------------------------------------------------------------------------------- /generate-toc.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // Load modules 4 | 5 | const Toc = require('markdown-toc'); 6 | const Fs = require('fs'); 7 | 8 | // Declare internals 9 | 10 | const internals = { 11 | filename: './README.md' 12 | }; 13 | 14 | 15 | internals.generate = function () { 16 | 17 | const api = Fs.readFileSync(internals.filename, 'utf8'); 18 | const tocOptions = { 19 | bullets: '-', 20 | slugify: function (text) { 21 | 22 | return text.toLowerCase() 23 | .replace(/\s/g, '-') 24 | .replace(/[^\w-]/g, ''); 25 | } 26 | }; 27 | 28 | const output = Toc.insert(api, tocOptions); 29 | Fs.writeFileSync(internals.filename, output); 30 | }; 31 | 32 | internals.generate(); 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "boom", 3 | "description": "HTTP-friendly error objects", 4 | "version": "7.1.1", 5 | "repository": "git://github.com/hapijs/boom", 6 | "main": "lib/index.js", 7 | "keywords": [ 8 | "error", 9 | "http" 10 | ], 11 | "engines": { 12 | "node": ">=8.9.0" 13 | }, 14 | "dependencies": { 15 | "hoek": "5.x.x" 16 | }, 17 | "devDependencies": { 18 | "code": "5.x.x", 19 | "lab": "15.x.x", 20 | "markdown-toc": "0.12.x" 21 | }, 22 | "scripts": { 23 | "test": "lab -a code -t 100 -L -v", 24 | "test-cov-html": "lab -a code -r html -o coverage.html -L", 25 | "toc": "node generate-toc.js", 26 | "version": "npm run toc && git add README.md" 27 | }, 28 | "license": "BSD-3-Clause" 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2017, Project contributors. 2 | Copyright (c) 2012-2014, Walmart. 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * The names of any contributors may not be used to endorse or promote 13 | products derived from this software without specific prior written 14 | permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | 27 | * * * 28 | 29 | The complete list of contributors can be found at: https://github.com/hapijs/boom/graphs/contributors 30 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // Load modules 4 | 5 | const Hoek = require('hoek'); 6 | 7 | 8 | // Declare internals 9 | 10 | const internals = { 11 | codes: new Map([ 12 | [100, 'Continue'], 13 | [101, 'Switching Protocols'], 14 | [102, 'Processing'], 15 | [200, 'OK'], 16 | [201, 'Created'], 17 | [202, 'Accepted'], 18 | [203, 'Non-Authoritative Information'], 19 | [204, 'No Content'], 20 | [205, 'Reset Content'], 21 | [206, 'Partial Content'], 22 | [207, 'Multi-Status'], 23 | [300, 'Multiple Choices'], 24 | [301, 'Moved Permanently'], 25 | [302, 'Moved Temporarily'], 26 | [303, 'See Other'], 27 | [304, 'Not Modified'], 28 | [305, 'Use Proxy'], 29 | [307, 'Temporary Redirect'], 30 | [400, 'Bad Request'], 31 | [401, 'Unauthorized'], 32 | [402, 'Payment Required'], 33 | [403, 'Forbidden'], 34 | [404, 'Not Found'], 35 | [405, 'Method Not Allowed'], 36 | [406, 'Not Acceptable'], 37 | [407, 'Proxy Authentication Required'], 38 | [408, 'Request Time-out'], 39 | [409, 'Conflict'], 40 | [410, 'Gone'], 41 | [411, 'Length Required'], 42 | [412, 'Precondition Failed'], 43 | [413, 'Request Entity Too Large'], 44 | [414, 'Request-URI Too Large'], 45 | [415, 'Unsupported Media Type'], 46 | [416, 'Requested Range Not Satisfiable'], 47 | [417, 'Expectation Failed'], 48 | [418, 'I\'m a teapot'], 49 | [422, 'Unprocessable Entity'], 50 | [423, 'Locked'], 51 | [424, 'Failed Dependency'], 52 | [425, 'Unordered Collection'], 53 | [426, 'Upgrade Required'], 54 | [428, 'Precondition Required'], 55 | [429, 'Too Many Requests'], 56 | [431, 'Request Header Fields Too Large'], 57 | [451, 'Unavailable For Legal Reasons'], 58 | [500, 'Internal Server Error'], 59 | [501, 'Not Implemented'], 60 | [502, 'Bad Gateway'], 61 | [503, 'Service Unavailable'], 62 | [504, 'Gateway Time-out'], 63 | [505, 'HTTP Version Not Supported'], 64 | [506, 'Variant Also Negotiates'], 65 | [507, 'Insufficient Storage'], 66 | [509, 'Bandwidth Limit Exceeded'], 67 | [510, 'Not Extended'], 68 | [511, 'Network Authentication Required'] 69 | ]) 70 | }; 71 | 72 | 73 | module.exports = internals.Boom = class extends Error { 74 | 75 | static [Symbol.hasInstance](instance) { 76 | 77 | return internals.Boom.isBoom(instance); 78 | } 79 | 80 | constructor(message, options = {}) { 81 | 82 | if (message instanceof Error) { 83 | return internals.Boom.boomify(Hoek.clone(message), options); 84 | } 85 | 86 | const { statusCode = 500, data = null, ctor = internals.Boom } = options; 87 | const error = new Error(message ? message : undefined); // Avoids settings null message 88 | Error.captureStackTrace(error, ctor); // Filter the stack to our external API 89 | error.data = data; 90 | internals.initialize(error, statusCode); 91 | error.typeof = ctor; 92 | 93 | if (options.decorate) { 94 | Object.assign(error, options.decorate); 95 | } 96 | 97 | return error; 98 | } 99 | 100 | static isBoom(err) { 101 | 102 | return (err instanceof Error && !!err.isBoom); 103 | } 104 | 105 | static boomify(err, options) { 106 | 107 | Hoek.assert(err instanceof Error, 'Cannot wrap non-Error object'); 108 | 109 | options = options || {}; 110 | 111 | if (options.data !== undefined) { 112 | err.data = options.data; 113 | } 114 | 115 | if (options.decorate) { 116 | Object.assign(err, options.decorate); 117 | } 118 | 119 | if (!err.isBoom) { 120 | return internals.initialize(err, options.statusCode || 500, options.message); 121 | } 122 | 123 | if (options.override === false || // Defaults to true 124 | (!options.statusCode && !options.message)) { 125 | 126 | return err; 127 | } 128 | 129 | return internals.initialize(err, options.statusCode || err.output.statusCode, options.message); 130 | } 131 | 132 | // 4xx Client Errors 133 | 134 | static badRequest(message, data) { 135 | 136 | return new internals.Boom(message, { statusCode: 400, data, ctor: internals.Boom.badRequest }); 137 | } 138 | 139 | static unauthorized(message, scheme, attributes) { // Or function (message, wwwAuthenticate[]) 140 | 141 | const err = new internals.Boom(message, { statusCode: 401, ctor: internals.Boom.unauthorized }); 142 | 143 | if (!scheme) { 144 | return err; 145 | } 146 | 147 | let wwwAuthenticate = ''; 148 | 149 | if (typeof scheme === 'string') { 150 | 151 | // function (message, scheme, attributes) 152 | 153 | wwwAuthenticate = scheme; 154 | 155 | if (attributes || message) { 156 | err.output.payload.attributes = {}; 157 | } 158 | 159 | if (attributes) { 160 | if (typeof attributes === 'string') { 161 | wwwAuthenticate = wwwAuthenticate + ' ' + Hoek.escapeHeaderAttribute(attributes); 162 | err.output.payload.attributes = attributes; 163 | } 164 | else { 165 | const names = Object.keys(attributes); 166 | for (let i = 0; i < names.length; ++i) { 167 | const name = names[i]; 168 | if (i) { 169 | wwwAuthenticate = wwwAuthenticate + ','; 170 | } 171 | 172 | let value = attributes[name]; 173 | if (value === null || 174 | value === undefined) { // Value can be zero 175 | 176 | value = ''; 177 | } 178 | wwwAuthenticate = wwwAuthenticate + ' ' + name + '="' + Hoek.escapeHeaderAttribute(value.toString()) + '"'; 179 | err.output.payload.attributes[name] = value; 180 | } 181 | } 182 | } 183 | 184 | 185 | if (message) { 186 | if (attributes) { 187 | wwwAuthenticate = wwwAuthenticate + ','; 188 | } 189 | wwwAuthenticate = wwwAuthenticate + ' error="' + Hoek.escapeHeaderAttribute(message) + '"'; 190 | err.output.payload.attributes.error = message; 191 | } 192 | else { 193 | err.isMissing = true; 194 | } 195 | } 196 | else { 197 | 198 | // function (message, wwwAuthenticate[]) 199 | 200 | const wwwArray = scheme; 201 | for (let i = 0; i < wwwArray.length; ++i) { 202 | if (i) { 203 | wwwAuthenticate = wwwAuthenticate + ', '; 204 | } 205 | 206 | wwwAuthenticate = wwwAuthenticate + wwwArray[i]; 207 | } 208 | } 209 | 210 | err.output.headers['WWW-Authenticate'] = wwwAuthenticate; 211 | 212 | return err; 213 | } 214 | 215 | static paymentRequired(message, data) { 216 | 217 | return new internals.Boom(message, { statusCode: 402, data, ctor: internals.Boom.paymentRequired }); 218 | } 219 | 220 | static forbidden(message, data) { 221 | 222 | return new internals.Boom(message, { statusCode: 403, data, ctor: internals.Boom.forbidden }); 223 | } 224 | 225 | static notFound(message, data) { 226 | 227 | return new internals.Boom(message, { statusCode: 404, data, ctor: internals.Boom.notFound }); 228 | } 229 | 230 | static methodNotAllowed(message, data, allow) { 231 | 232 | const err = new internals.Boom(message, { statusCode: 405, data, ctor: internals.Boom.methodNotAllowed }); 233 | 234 | if (typeof allow === 'string') { 235 | allow = [allow]; 236 | } 237 | 238 | if (Array.isArray(allow)) { 239 | err.output.headers.Allow = allow.join(', '); 240 | } 241 | 242 | return err; 243 | } 244 | 245 | static notAcceptable(message, data) { 246 | 247 | return new internals.Boom(message, { statusCode: 406, data, ctor: internals.Boom.notAcceptable }); 248 | } 249 | 250 | static proxyAuthRequired(message, data) { 251 | 252 | return new internals.Boom(message, { statusCode: 407, data, ctor: internals.Boom.proxyAuthRequired }); 253 | } 254 | 255 | static clientTimeout(message, data) { 256 | 257 | return new internals.Boom(message, { statusCode: 408, data, ctor: internals.Boom.clientTimeout }); 258 | } 259 | 260 | static conflict(message, data) { 261 | 262 | return new internals.Boom(message, { statusCode: 409, data, ctor: internals.Boom.conflict }); 263 | } 264 | 265 | static resourceGone(message, data) { 266 | 267 | return new internals.Boom(message, { statusCode: 410, data, ctor: internals.Boom.resourceGone }); 268 | } 269 | 270 | static lengthRequired(message, data) { 271 | 272 | return new internals.Boom(message, { statusCode: 411, data, ctor: internals.Boom.lengthRequired }); 273 | } 274 | 275 | static preconditionFailed(message, data) { 276 | 277 | return new internals.Boom(message, { statusCode: 412, data, ctor: internals.Boom.preconditionFailed }); 278 | } 279 | 280 | static entityTooLarge(message, data) { 281 | 282 | return new internals.Boom(message, { statusCode: 413, data, ctor: internals.Boom.entityTooLarge }); 283 | } 284 | 285 | static uriTooLong(message, data) { 286 | 287 | return new internals.Boom(message, { statusCode: 414, data, ctor: internals.Boom.uriTooLong }); 288 | } 289 | 290 | static unsupportedMediaType(message, data) { 291 | 292 | return new internals.Boom(message, { statusCode: 415, data, ctor: internals.Boom.unsupportedMediaType }); 293 | } 294 | 295 | static rangeNotSatisfiable(message, data) { 296 | 297 | return new internals.Boom(message, { statusCode: 416, data, ctor: internals.Boom.rangeNotSatisfiable }); 298 | } 299 | 300 | static expectationFailed(message, data) { 301 | 302 | return new internals.Boom(message, { statusCode: 417, data, ctor: internals.Boom.expectationFailed }); 303 | } 304 | 305 | static teapot(message, data) { 306 | 307 | return new internals.Boom(message, { statusCode: 418, data, ctor: internals.Boom.teapot }); 308 | } 309 | 310 | static badData(message, data) { 311 | 312 | return new internals.Boom(message, { statusCode: 422, data, ctor: internals.Boom.badData }); 313 | } 314 | 315 | static locked(message, data) { 316 | 317 | return new internals.Boom(message, { statusCode: 423, data, ctor: internals.Boom.locked }); 318 | } 319 | 320 | static preconditionRequired(message, data) { 321 | 322 | return new internals.Boom(message, { statusCode: 428, data, ctor: internals.Boom.preconditionRequired }); 323 | } 324 | 325 | static tooManyRequests(message, data) { 326 | 327 | return new internals.Boom(message, { statusCode: 429, data, ctor: internals.Boom.tooManyRequests }); 328 | } 329 | 330 | static illegal(message, data) { 331 | 332 | return new internals.Boom(message, { statusCode: 451, data, ctor: internals.Boom.illegal }); 333 | } 334 | 335 | // 5xx Server Errors 336 | 337 | static internal(message, data, statusCode = 500) { 338 | 339 | return internals.serverError(message, data, statusCode, internals.Boom.internal); 340 | } 341 | 342 | static notImplemented(message, data) { 343 | 344 | return internals.serverError(message, data, 501, internals.Boom.notImplemented); 345 | } 346 | 347 | static badGateway(message, data) { 348 | 349 | return internals.serverError(message, data, 502, internals.Boom.badGateway); 350 | } 351 | 352 | static serverUnavailable(message, data) { 353 | 354 | return internals.serverError(message, data, 503, internals.Boom.serverUnavailable); 355 | } 356 | 357 | static gatewayTimeout(message, data) { 358 | 359 | return internals.serverError(message, data, 504, internals.Boom.gatewayTimeout); 360 | } 361 | 362 | static badImplementation(message, data) { 363 | 364 | const err = internals.serverError(message, data, 500, internals.Boom.badImplementation); 365 | err.isDeveloperError = true; 366 | return err; 367 | } 368 | }; 369 | 370 | 371 | 372 | internals.initialize = function (err, statusCode, message) { 373 | 374 | const numberCode = parseInt(statusCode, 10); 375 | Hoek.assert(!isNaN(numberCode) && numberCode >= 400, 'First argument must be a number (400+):', statusCode); 376 | 377 | err.isBoom = true; 378 | err.isServer = numberCode >= 500; 379 | 380 | if (!err.hasOwnProperty('data')) { 381 | err.data = null; 382 | } 383 | 384 | err.output = { 385 | statusCode: numberCode, 386 | payload: {}, 387 | headers: {} 388 | }; 389 | 390 | err.reformat = internals.reformat; 391 | 392 | if (!message && 393 | !err.message) { 394 | 395 | err.reformat(); 396 | message = err.output.payload.error; 397 | } 398 | 399 | if (message) { 400 | err.message = (message + (err.message ? ': ' + err.message : '')); 401 | err.output.payload.message = err.message; 402 | } 403 | 404 | err.reformat(); 405 | return err; 406 | }; 407 | 408 | 409 | internals.reformat = function () { 410 | 411 | this.output.payload.statusCode = this.output.statusCode; 412 | this.output.payload.error = internals.codes.get(this.output.statusCode) || 'Unknown'; 413 | 414 | if (this.output.statusCode === 500) { 415 | this.output.payload.message = 'An internal server error occurred'; // Hide actual error from user 416 | } 417 | else if (this.message) { 418 | this.output.payload.message = this.message; 419 | } 420 | }; 421 | 422 | 423 | internals.serverError = function (message, data, statusCode, ctor) { 424 | 425 | if (data instanceof Error && 426 | !data.isBoom) { 427 | 428 | return internals.Boom.boomify(data, { statusCode, message }); 429 | } 430 | 431 | return new internals.Boom(message, { statusCode, data, ctor }); 432 | }; 433 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![boom Logo](https://raw.github.com/hapijs/boom/master/images/boom.png) 2 | 3 | HTTP-friendly error objects 4 | 5 | [![Build Status](https://secure.travis-ci.org/hapijs/boom.svg)](http://travis-ci.org/hapijs/boom) 6 | [![Current Version](https://img.shields.io/npm/v/boom.svg)](https://www.npmjs.com/package/boom) 7 | 8 | Lead Maintainer: [Adam Bretz](https://github.com/arb) 9 | 10 | 11 | 12 | - [Boom](#boom) 13 | - [Helper Methods](#helper-methods) 14 | - [`new Boom(message, [options])`](#new-boommessage-options) 15 | - [`boomify(err, [options])`](#boomifyerr-options) 16 | - [`isBoom(err)`](#isboomerr) 17 | - [HTTP 4xx Errors](#http-4xx-errors) 18 | - [`Boom.badRequest([message], [data])`](#boombadrequestmessage-data) 19 | - [`Boom.unauthorized([message], [scheme], [attributes])`](#boomunauthorizedmessage-scheme-attributes) 20 | - [`Boom.paymentRequired([message], [data])`](#boompaymentrequiredmessage-data) 21 | - [`Boom.forbidden([message], [data])`](#boomforbiddenmessage-data) 22 | - [`Boom.notFound([message], [data])`](#boomnotfoundmessage-data) 23 | - [`Boom.methodNotAllowed([message], [data], [allow])`](#boommethodnotallowedmessage-data-allow) 24 | - [`Boom.notAcceptable([message], [data])`](#boomnotacceptablemessage-data) 25 | - [`Boom.proxyAuthRequired([message], [data])`](#boomproxyauthrequiredmessage-data) 26 | - [`Boom.clientTimeout([message], [data])`](#boomclienttimeoutmessage-data) 27 | - [`Boom.conflict([message], [data])`](#boomconflictmessage-data) 28 | - [`Boom.resourceGone([message], [data])`](#boomresourcegonemessage-data) 29 | - [`Boom.lengthRequired([message], [data])`](#boomlengthrequiredmessage-data) 30 | - [`Boom.preconditionFailed([message], [data])`](#boompreconditionfailedmessage-data) 31 | - [`Boom.entityTooLarge([message], [data])`](#boomentitytoolargemessage-data) 32 | - [`Boom.uriTooLong([message], [data])`](#boomuritoolongmessage-data) 33 | - [`Boom.unsupportedMediaType([message], [data])`](#boomunsupportedmediatypemessage-data) 34 | - [`Boom.rangeNotSatisfiable([message], [data])`](#boomrangenotsatisfiablemessage-data) 35 | - [`Boom.expectationFailed([message], [data])`](#boomexpectationfailedmessage-data) 36 | - [`Boom.teapot([message], [data])`](#boomteapotmessage-data) 37 | - [`Boom.badData([message], [data])`](#boombaddatamessage-data) 38 | - [`Boom.locked([message], [data])`](#boomlockedmessage-data) 39 | - [`Boom.preconditionRequired([message], [data])`](#boompreconditionrequiredmessage-data) 40 | - [`Boom.tooManyRequests([message], [data])`](#boomtoomanyrequestsmessage-data) 41 | - [`Boom.illegal([message], [data])`](#boomillegalmessage-data) 42 | - [HTTP 5xx Errors](#http-5xx-errors) 43 | - [`Boom.badImplementation([message], [data])` - (*alias: `internal`*)](#boombadimplementationmessage-data---alias-internal) 44 | - [`Boom.notImplemented([message], [data])`](#boomnotimplementedmessage-data) 45 | - [`Boom.badGateway([message], [data])`](#boombadgatewaymessage-data) 46 | - [`Boom.serverUnavailable([message], [data])`](#boomserverunavailablemessage-data) 47 | - [`Boom.gatewayTimeout([message], [data])`](#boomgatewaytimeoutmessage-data) 48 | - [F.A.Q.](#faq) 49 | 50 | 51 | 52 | # Boom 53 | 54 | **boom** provides a set of utilities for returning HTTP errors. Each utility returns a `Boom` 55 | error response object which includes the following properties: 56 | - `isBoom` - if `true`, indicates this is a `Boom` object instance. Note that this boolean should 57 | only be used if the error is an instance of `Error`. If it is not certain, use `Boom.isBoom()` 58 | instead. 59 | - `isServer` - convenience bool indicating status code >= 500. 60 | - `message` - the error message. 61 | - `typeof` - the constructor used to create the error (e.g. `Boom.badRequest`). 62 | - `output` - the formatted response. Can be directly manipulated after object construction to return a custom 63 | error response. Allowed root keys: 64 | - `statusCode` - the HTTP status code (typically 4xx or 5xx). 65 | - `headers` - an object containing any HTTP headers where each key is a header name and value is the header content. 66 | - `payload` - the formatted object used as the response payload (stringified). Can be directly manipulated but any 67 | changes will be lost 68 | if `reformat()` is called. Any content allowed and by default includes the following content: 69 | - `statusCode` - the HTTP status code, derived from `error.output.statusCode`. 70 | - `error` - the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived from `statusCode`. 71 | - `message` - the error message derived from `error.message`. 72 | - inherited `Error` properties. 73 | 74 | The `Boom` object also supports the following method: 75 | - `reformat()` - rebuilds `error.output` using the other object properties. 76 | 77 | Note that `Boom` object will return `true` when used with `instanceof Boom`, but do not use the 78 | `Boom` prototype (they are either plain `Error` or the error prototype passed in). This means 79 | `Boom` objects should only be tested using `instanceof Boom` or `Boom.isBoom()` but not by looking 80 | at the prototype or contructor information. This limitation is to avoid manipulating the prototype 81 | chain which is very slow. 82 | 83 | ## Helper Methods 84 | 85 | ### `new Boom(message, [options])` 86 | 87 | Creates a new `Boom` object using the provided `message` and then calling 88 | [`boomify()`](#boomifyerr-options) to decorate the error with the `Boom` properties, where: 89 | - `message` - the error message. If `message` is an error, it is the same as calling 90 | [`boomify()`](#boomifyerr-options) directly. 91 | - `options` - and optional object where: 92 | - `statusCode` - the HTTP status code. Defaults to `500` if no status code is already set. 93 | - `data` - additional error information (assigned to `error.data`). 94 | - `decorate` - an option with extra properties to set on the error object. 95 | - `ctor` - constructor reference used to crop the exception call stack output. 96 | - if `message` is an error object, also supports the other [`boomify()`](#boomifyerr-options) 97 | options. 98 | 99 | ### `boomify(err, [options])` 100 | 101 | Decorates an error with the `Boom` properties where: 102 | - `err` - the `Error` object to decorate. 103 | - `options` - optional object with the following optional settings: 104 | - `statusCode` - the HTTP status code. Defaults to `500` if no status code is already set and `err` is not a `Boom` object. 105 | - `message` - error message string. If the error already has a message, the provided `message` is added as a prefix. 106 | Defaults to no message. 107 | - `decorate` - an option with extra properties to set on the error object. 108 | - `override` - if `false`, the `err` provided is a `Boom` object, and a `statusCode` or `message` are provided, 109 | the values are ignored. Defaults to `true` (apply the provided `statusCode` and `message` options to the error 110 | regardless of its type, `Error` or `Boom` object). 111 | 112 | ```js 113 | var error = new Error('Unexpected input'); 114 | Boom.boomify(error, { statusCode: 400 }); 115 | ``` 116 | 117 | ### `isBoom(err)` 118 | 119 | Identifies whether an error is a `Boom` object. Same as calling `instanceof Boom`. 120 | 121 | ## HTTP 4xx Errors 122 | 123 | ### `Boom.badRequest([message], [data])` 124 | 125 | Returns a 400 Bad Request error where: 126 | - `message` - optional message. 127 | - `data` - optional additional error data. 128 | 129 | ```js 130 | Boom.badRequest('invalid query'); 131 | ``` 132 | 133 | Generates the following response payload: 134 | 135 | ```json 136 | { 137 | "statusCode": 400, 138 | "error": "Bad Request", 139 | "message": "invalid query" 140 | } 141 | ``` 142 | 143 | ### `Boom.unauthorized([message], [scheme], [attributes])` 144 | 145 | Returns a 401 Unauthorized error where: 146 | - `message` - optional message. 147 | - `scheme` can be one of the following: 148 | - an authentication scheme name 149 | - an array of string values. These values will be separated by ', ' and set to the 'WWW-Authenticate' header. 150 | - `attributes` - an object of values to use while setting the 'WWW-Authenticate' header. This value is only used 151 | when `scheme` is a string, otherwise it is ignored. Every key/value pair will be included in the 152 | '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. 153 | `null` and `undefined` will be replaced with an empty string. If `attributes` is set, `message` will be used as 154 | the 'error' segment of the 'WWW-Authenticate' header. If `message` is unset, the 'error' segment of the header 155 | will not be present and `isMissing` will be true on the error object. 156 | 157 | If either `scheme` or `attributes` are set, the resultant `Boom` object will have the 158 | 'WWW-Authenticate' header set for the response. 159 | 160 | ```js 161 | Boom.unauthorized('invalid password'); 162 | ``` 163 | 164 | Generates the following response: 165 | 166 | ```json 167 | "payload": { 168 | "statusCode": 401, 169 | "error": "Unauthorized", 170 | "message": "invalid password" 171 | }, 172 | "headers" {} 173 | ``` 174 | 175 | ```js 176 | Boom.unauthorized('invalid password', 'sample'); 177 | ``` 178 | 179 | Generates the following response: 180 | 181 | ```json 182 | "payload": { 183 | "statusCode": 401, 184 | "error": "Unauthorized", 185 | "message": "invalid password", 186 | "attributes": { 187 | "error": "invalid password" 188 | } 189 | }, 190 | "headers" { 191 | "WWW-Authenticate": "sample error=\"invalid password\"" 192 | } 193 | ``` 194 | 195 | ```js 196 | Boom.unauthorized(null, 'Negotiate', 'VGhpcyBpcyBhIHRlc3QgdG9rZW4='); 197 | ``` 198 | 199 | Generates the following response: 200 | 201 | ```json 202 | "payload": { 203 | "statusCode": 401, 204 | "error": "Unauthorized", 205 | "attributes": "VGhpcyBpcyBhIHRlc3QgdG9rZW4=" 206 | }, 207 | "headers" { 208 | "WWW-Authenticate": "Negotiate VGhpcyBpcyBhIHRlc3QgdG9rZW4=" 209 | } 210 | ``` 211 | 212 | ```js 213 | Boom.unauthorized('invalid password', 'sample', { ttl: 0, cache: null, foo: 'bar' }); 214 | ``` 215 | 216 | Generates the following response: 217 | 218 | ```json 219 | "payload": { 220 | "statusCode": 401, 221 | "error": "Unauthorized", 222 | "message": "invalid password", 223 | "attributes": { 224 | "error": "invalid password", 225 | "ttl": 0, 226 | "cache": "", 227 | "foo": "bar" 228 | } 229 | }, 230 | "headers" { 231 | "WWW-Authenticate": "sample ttl=\"0\", cache=\"\", foo=\"bar\", error=\"invalid password\"" 232 | } 233 | ``` 234 | 235 | ### `Boom.paymentRequired([message], [data])` 236 | 237 | Returns a 402 Payment Required error where: 238 | - `message` - optional message. 239 | - `data` - optional additional error data. 240 | 241 | ```js 242 | Boom.paymentRequired('bandwidth used'); 243 | ``` 244 | 245 | Generates the following response payload: 246 | 247 | ```json 248 | { 249 | "statusCode": 402, 250 | "error": "Payment Required", 251 | "message": "bandwidth used" 252 | } 253 | ``` 254 | 255 | ### `Boom.forbidden([message], [data])` 256 | 257 | Returns a 403 Forbidden error where: 258 | - `message` - optional message. 259 | - `data` - optional additional error data. 260 | 261 | ```js 262 | Boom.forbidden('try again some time'); 263 | ``` 264 | 265 | Generates the following response payload: 266 | 267 | ```json 268 | { 269 | "statusCode": 403, 270 | "error": "Forbidden", 271 | "message": "try again some time" 272 | } 273 | ``` 274 | 275 | ### `Boom.notFound([message], [data])` 276 | 277 | Returns a 404 Not Found error where: 278 | - `message` - optional message. 279 | - `data` - optional additional error data. 280 | 281 | ```js 282 | Boom.notFound('missing'); 283 | ``` 284 | 285 | Generates the following response payload: 286 | 287 | ```json 288 | { 289 | "statusCode": 404, 290 | "error": "Not Found", 291 | "message": "missing" 292 | } 293 | ``` 294 | 295 | ### `Boom.methodNotAllowed([message], [data], [allow])` 296 | 297 | Returns a 405 Method Not Allowed error where: 298 | - `message` - optional message. 299 | - `data` - optional additional error data. 300 | - `allow` - optional string or array of strings (to be combined and separated by ', ') which is set to the 'Allow' header. 301 | 302 | ```js 303 | Boom.methodNotAllowed('that method is not allowed'); 304 | ``` 305 | 306 | Generates the following response payload: 307 | 308 | ```json 309 | { 310 | "statusCode": 405, 311 | "error": "Method Not Allowed", 312 | "message": "that method is not allowed" 313 | } 314 | ``` 315 | 316 | ### `Boom.notAcceptable([message], [data])` 317 | 318 | Returns a 406 Not Acceptable error where: 319 | - `message` - optional message. 320 | - `data` - optional additional error data. 321 | 322 | ```js 323 | Boom.notAcceptable('unacceptable'); 324 | ``` 325 | 326 | Generates the following response payload: 327 | 328 | ```json 329 | { 330 | "statusCode": 406, 331 | "error": "Not Acceptable", 332 | "message": "unacceptable" 333 | } 334 | ``` 335 | 336 | ### `Boom.proxyAuthRequired([message], [data])` 337 | 338 | Returns a 407 Proxy Authentication Required error where: 339 | - `message` - optional message. 340 | - `data` - optional additional error data. 341 | 342 | ```js 343 | Boom.proxyAuthRequired('auth missing'); 344 | ``` 345 | 346 | Generates the following response payload: 347 | 348 | ```json 349 | { 350 | "statusCode": 407, 351 | "error": "Proxy Authentication Required", 352 | "message": "auth missing" 353 | } 354 | ``` 355 | 356 | ### `Boom.clientTimeout([message], [data])` 357 | 358 | Returns a 408 Request Time-out error where: 359 | - `message` - optional message. 360 | - `data` - optional additional error data. 361 | 362 | ```js 363 | Boom.clientTimeout('timed out'); 364 | ``` 365 | 366 | Generates the following response payload: 367 | 368 | ```json 369 | { 370 | "statusCode": 408, 371 | "error": "Request Time-out", 372 | "message": "timed out" 373 | } 374 | ``` 375 | 376 | ### `Boom.conflict([message], [data])` 377 | 378 | Returns a 409 Conflict error where: 379 | - `message` - optional message. 380 | - `data` - optional additional error data. 381 | 382 | ```js 383 | Boom.conflict('there was a conflict'); 384 | ``` 385 | 386 | Generates the following response payload: 387 | 388 | ```json 389 | { 390 | "statusCode": 409, 391 | "error": "Conflict", 392 | "message": "there was a conflict" 393 | } 394 | ``` 395 | 396 | ### `Boom.resourceGone([message], [data])` 397 | 398 | Returns a 410 Gone error where: 399 | - `message` - optional message. 400 | - `data` - optional additional error data. 401 | 402 | ```js 403 | Boom.resourceGone('it is gone'); 404 | ``` 405 | 406 | Generates the following response payload: 407 | 408 | ```json 409 | { 410 | "statusCode": 410, 411 | "error": "Gone", 412 | "message": "it is gone" 413 | } 414 | ``` 415 | 416 | ### `Boom.lengthRequired([message], [data])` 417 | 418 | Returns a 411 Length Required error where: 419 | - `message` - optional message. 420 | - `data` - optional additional error data. 421 | 422 | ```js 423 | Boom.lengthRequired('length needed'); 424 | ``` 425 | 426 | Generates the following response payload: 427 | 428 | ```json 429 | { 430 | "statusCode": 411, 431 | "error": "Length Required", 432 | "message": "length needed" 433 | } 434 | ``` 435 | 436 | ### `Boom.preconditionFailed([message], [data])` 437 | 438 | Returns a 412 Precondition Failed error where: 439 | - `message` - optional message. 440 | - `data` - optional additional error data. 441 | 442 | ```js 443 | Boom.preconditionFailed(); 444 | ``` 445 | 446 | Generates the following response payload: 447 | 448 | ```json 449 | { 450 | "statusCode": 412, 451 | "error": "Precondition Failed" 452 | } 453 | ``` 454 | 455 | ### `Boom.entityTooLarge([message], [data])` 456 | 457 | Returns a 413 Request Entity Too Large error where: 458 | - `message` - optional message. 459 | - `data` - optional additional error data. 460 | 461 | ```js 462 | Boom.entityTooLarge('too big'); 463 | ``` 464 | 465 | Generates the following response payload: 466 | 467 | ```json 468 | { 469 | "statusCode": 413, 470 | "error": "Request Entity Too Large", 471 | "message": "too big" 472 | } 473 | ``` 474 | 475 | ### `Boom.uriTooLong([message], [data])` 476 | 477 | Returns a 414 Request-URI Too Large error where: 478 | - `message` - optional message. 479 | - `data` - optional additional error data. 480 | 481 | ```js 482 | Boom.uriTooLong('uri is too long'); 483 | ``` 484 | 485 | Generates the following response payload: 486 | 487 | ```json 488 | { 489 | "statusCode": 414, 490 | "error": "Request-URI Too Large", 491 | "message": "uri is too long" 492 | } 493 | ``` 494 | 495 | ### `Boom.unsupportedMediaType([message], [data])` 496 | 497 | Returns a 415 Unsupported Media Type error where: 498 | - `message` - optional message. 499 | - `data` - optional additional error data. 500 | 501 | ```js 502 | Boom.unsupportedMediaType('that media is not supported'); 503 | ``` 504 | 505 | Generates the following response payload: 506 | 507 | ```json 508 | { 509 | "statusCode": 415, 510 | "error": "Unsupported Media Type", 511 | "message": "that media is not supported" 512 | } 513 | ``` 514 | 515 | ### `Boom.rangeNotSatisfiable([message], [data])` 516 | 517 | Returns a 416 Requested Range Not Satisfiable error where: 518 | - `message` - optional message. 519 | - `data` - optional additional error data. 520 | 521 | ```js 522 | Boom.rangeNotSatisfiable(); 523 | ``` 524 | 525 | Generates the following response payload: 526 | 527 | ```json 528 | { 529 | "statusCode": 416, 530 | "error": "Requested Range Not Satisfiable" 531 | } 532 | ``` 533 | 534 | ### `Boom.expectationFailed([message], [data])` 535 | 536 | Returns a 417 Expectation Failed error where: 537 | - `message` - optional message. 538 | - `data` - optional additional error data. 539 | 540 | ```js 541 | Boom.expectationFailed('expected this to work'); 542 | ``` 543 | 544 | Generates the following response payload: 545 | 546 | ```json 547 | { 548 | "statusCode": 417, 549 | "error": "Expectation Failed", 550 | "message": "expected this to work" 551 | } 552 | ``` 553 | 554 | ### `Boom.teapot([message], [data])` 555 | 556 | Returns a 418 I'm a Teapot error where: 557 | - `message` - optional message. 558 | - `data` - optional additional error data. 559 | 560 | ```js 561 | Boom.teapot('sorry, no coffee...'); 562 | ``` 563 | 564 | Generates the following response payload: 565 | 566 | ```json 567 | { 568 | "statusCode": 418, 569 | "error": "I'm a Teapot", 570 | "message": "Sorry, no coffee..." 571 | } 572 | ``` 573 | 574 | ### `Boom.badData([message], [data])` 575 | 576 | Returns a 422 Unprocessable Entity error where: 577 | - `message` - optional message. 578 | - `data` - optional additional error data. 579 | 580 | ```js 581 | Boom.badData('your data is bad and you should feel bad'); 582 | ``` 583 | 584 | Generates the following response payload: 585 | 586 | ```json 587 | { 588 | "statusCode": 422, 589 | "error": "Unprocessable Entity", 590 | "message": "your data is bad and you should feel bad" 591 | } 592 | ``` 593 | 594 | ### `Boom.locked([message], [data])` 595 | 596 | Returns a 423 Locked error where: 597 | - `message` - optional message. 598 | - `data` - optional additional error data. 599 | 600 | ```js 601 | Boom.locked('this resource has been locked'); 602 | ``` 603 | 604 | Generates the following response payload: 605 | 606 | ```json 607 | { 608 | "statusCode": 423, 609 | "error": "Locked", 610 | "message": "this resource has been locked" 611 | } 612 | ``` 613 | 614 | ### `Boom.preconditionRequired([message], [data])` 615 | 616 | Returns a 428 Precondition Required error where: 617 | - `message` - optional message. 618 | - `data` - optional additional error data. 619 | 620 | ```js 621 | Boom.preconditionRequired('you must supply an If-Match header'); 622 | ``` 623 | 624 | Generates the following response payload: 625 | 626 | ```json 627 | { 628 | "statusCode": 428, 629 | "error": "Precondition Required", 630 | "message": "you must supply an If-Match header" 631 | } 632 | ``` 633 | 634 | ### `Boom.tooManyRequests([message], [data])` 635 | 636 | Returns a 429 Too Many Requests error where: 637 | - `message` - optional message. 638 | - `data` - optional additional error data. 639 | 640 | ```js 641 | Boom.tooManyRequests('you have exceeded your request limit'); 642 | ``` 643 | 644 | Generates the following response payload: 645 | 646 | ```json 647 | { 648 | "statusCode": 429, 649 | "error": "Too Many Requests", 650 | "message": "you have exceeded your request limit" 651 | } 652 | ``` 653 | 654 | ### `Boom.illegal([message], [data])` 655 | 656 | Returns a 451 Unavailable For Legal Reasons error where: 657 | - `message` - optional message. 658 | - `data` - optional additional error data. 659 | 660 | ```js 661 | Boom.illegal('you are not permitted to view this resource for legal reasons'); 662 | ``` 663 | 664 | Generates the following response payload: 665 | 666 | ```json 667 | { 668 | "statusCode": 451, 669 | "error": "Unavailable For Legal Reasons", 670 | "message": "you are not permitted to view this resource for legal reasons" 671 | } 672 | ``` 673 | 674 | ## HTTP 5xx Errors 675 | 676 | All 500 errors hide your message from the end user. Your message is recorded in the server log. 677 | 678 | ### `Boom.badImplementation([message], [data])` - (*alias: `internal`*) 679 | 680 | Returns a 500 Internal Server Error error where: 681 | - `message` - optional message. 682 | - `data` - optional additional error data. 683 | 684 | ```js 685 | Boom.badImplementation('terrible implementation'); 686 | ``` 687 | 688 | Generates the following response payload: 689 | 690 | ```json 691 | { 692 | "statusCode": 500, 693 | "error": "Internal Server Error", 694 | "message": "An internal server error occurred" 695 | } 696 | ``` 697 | 698 | ### `Boom.notImplemented([message], [data])` 699 | 700 | Returns a 501 Not Implemented error where: 701 | - `message` - optional message. 702 | - `data` - optional additional error data. 703 | 704 | ```js 705 | Boom.notImplemented('method not implemented'); 706 | ``` 707 | 708 | Generates the following response payload: 709 | 710 | ```json 711 | { 712 | "statusCode": 501, 713 | "error": "Not Implemented", 714 | "message": "method not implemented" 715 | } 716 | ``` 717 | 718 | ### `Boom.badGateway([message], [data])` 719 | 720 | Returns a 502 Bad Gateway error where: 721 | - `message` - optional message. 722 | - `data` - optional additional error data. 723 | 724 | ```js 725 | Boom.badGateway('that is a bad gateway'); 726 | ``` 727 | 728 | Generates the following response payload: 729 | 730 | ```json 731 | { 732 | "statusCode": 502, 733 | "error": "Bad Gateway", 734 | "message": "that is a bad gateway" 735 | } 736 | ``` 737 | 738 | ### `Boom.serverUnavailable([message], [data])` 739 | 740 | Returns a 503 Service Unavailable error where: 741 | - `message` - optional message. 742 | - `data` - optional additional error data. 743 | 744 | ```js 745 | Boom.serverUnavailable('unavailable'); 746 | ``` 747 | 748 | Generates the following response payload: 749 | 750 | ```json 751 | { 752 | "statusCode": 503, 753 | "error": "Service Unavailable", 754 | "message": "unavailable" 755 | } 756 | ``` 757 | 758 | ### `Boom.gatewayTimeout([message], [data])` 759 | 760 | Returns a 504 Gateway Time-out error where: 761 | - `message` - optional message. 762 | - `data` - optional additional error data. 763 | 764 | ```js 765 | Boom.gatewayTimeout(); 766 | ``` 767 | 768 | Generates the following response payload: 769 | 770 | ```json 771 | { 772 | "statusCode": 504, 773 | "error": "Gateway Time-out" 774 | } 775 | ``` 776 | 777 | ## F.A.Q. 778 | 779 | **Q** How do I include extra information in my responses? `output.payload` is missing `data`, what gives? 780 | 781 | **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. 782 | 783 | --- 784 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // Load modules 4 | 5 | const Code = require('code'); 6 | const Boom = require('../lib'); 7 | const Lab = require('lab'); 8 | 9 | 10 | // Declare internals 11 | 12 | const internals = {}; 13 | 14 | 15 | // Test shortcuts 16 | 17 | const { describe, it } = exports.lab = Lab.script(); 18 | const expect = Code.expect; 19 | 20 | 21 | describe('Boom', () => { 22 | 23 | it('constructs error object (new)', () => { 24 | 25 | const err = new Boom('oops', { statusCode: 400 }); 26 | expect(err.output.payload.message).to.equal('oops'); 27 | expect(err.output.statusCode).to.equal(400); 28 | }); 29 | 30 | it('clones error object', () => { 31 | 32 | const oops = new Error('oops'); 33 | const err = new Boom(oops, { statusCode: 400 }); 34 | expect(err).to.not.shallow.equal(oops); 35 | expect(err.output.payload.message).to.equal('oops'); 36 | expect(err.output.statusCode).to.equal(400); 37 | }); 38 | 39 | it('decorates error', () => { 40 | 41 | const err = new Boom('oops', { statusCode: 400, decorate: { x: 1 } }); 42 | expect(err.output.payload.message).to.equal('oops'); 43 | expect(err.output.statusCode).to.equal(400); 44 | expect(err.x).to.equal(1); 45 | }); 46 | 47 | it('throws when statusCode is not a number', () => { 48 | 49 | expect(() => { 50 | 51 | new Boom('message', { statusCode: 'x' }); 52 | }).to.throw('First argument must be a number (400+): x'); 53 | }); 54 | 55 | it('will cast a number-string to an integer', () => { 56 | 57 | const codes = [ 58 | { input: '404', result: 404 }, 59 | { input: '404.1', result: 404 }, 60 | { input: 400, result: 400 }, 61 | { input: 400.123, result: 400 } 62 | ]; 63 | 64 | for (let i = 0; i < codes.length; ++i) { 65 | const code = codes[i]; 66 | const err = new Boom('', { statusCode: code.input }); 67 | expect(err.output.statusCode).to.equal(code.result); 68 | } 69 | }); 70 | 71 | it('throws when statusCode is not finite', () => { 72 | 73 | expect(() => { 74 | 75 | new Boom('', { statusCode: 1 / 0 }); 76 | }).to.throw('First argument must be a number (400+): null'); 77 | }); 78 | 79 | it('sets error code to unknown', () => { 80 | 81 | const err = new Boom('', { statusCode: 999 }); 82 | expect(err.output.payload.error).to.equal('Unknown'); 83 | }); 84 | 85 | describe('instanceof', () => { 86 | 87 | it('identifies a boom object', () => { 88 | 89 | expect(new Boom('oops') instanceof Boom).to.be.true(); 90 | expect(Boom.badRequest('oops') instanceof Boom).to.be.true(); 91 | expect(new Error('oops') instanceof Boom).to.be.false(); 92 | expect(Boom.boomify(new Error('oops')) instanceof Boom).to.be.true(); 93 | expect({ isBoom: true } instanceof Boom).to.be.false(); 94 | expect(null instanceof Boom).to.be.false(); 95 | }); 96 | }); 97 | 98 | describe('isBoom()', () => { 99 | 100 | it('identifies a boom object', () => { 101 | 102 | expect(Boom.isBoom(new Boom('oops'))).to.be.true(); 103 | expect(Boom.isBoom(new Error('oops'))).to.be.false(); 104 | expect(Boom.isBoom({ isBoom: true })).to.be.false(); 105 | expect(Boom.isBoom(null)).to.be.false(); 106 | }); 107 | }); 108 | 109 | describe('boomify()', () => { 110 | 111 | it('returns the same object when already boom', () => { 112 | 113 | const error = Boom.badRequest(); 114 | expect(error).to.equal(Boom.boomify(error)); 115 | expect(error).to.equal(Boom.boomify(error, { statusCode: 444 })); 116 | }); 117 | 118 | it('decorates error', () => { 119 | 120 | const err = new Error('oops'); 121 | Boom.boomify(err, { statusCode: 400, decorate: { x: 1 } }); 122 | expect(err.x).to.equal(1); 123 | }); 124 | 125 | it('returns an error with info when constructed using another error', () => { 126 | 127 | const error = new Error('ka-boom'); 128 | error.xyz = 123; 129 | const err = Boom.boomify(error); 130 | expect(err.xyz).to.equal(123); 131 | expect(err.message).to.equal('ka-boom'); 132 | expect(err.output).to.equal({ 133 | statusCode: 500, 134 | payload: { 135 | statusCode: 500, 136 | error: 'Internal Server Error', 137 | message: 'An internal server error occurred' 138 | }, 139 | headers: {} 140 | }); 141 | expect(err.data).to.equal(null); 142 | }); 143 | 144 | it('does not override data when constructed using another error', () => { 145 | 146 | const error = new Error('ka-boom'); 147 | error.data = { useful: 'data' }; 148 | const err = Boom.boomify(error); 149 | expect(err.data).to.equal(error.data); 150 | }); 151 | 152 | it('sets new message when none exists', () => { 153 | 154 | const error = new Error(); 155 | const wrapped = Boom.boomify(error, { statusCode: 400, message: 'something bad' }); 156 | expect(wrapped.message).to.equal('something bad'); 157 | }); 158 | 159 | it('returns boom error unchanged', () => { 160 | 161 | const error = Boom.badRequest('Missing data', { type: 'user' }); 162 | const boom = Boom.boomify(error); 163 | 164 | expect(boom).to.shallow.equal(error); 165 | expect(error.data.type).to.equal('user'); 166 | expect(error.output.payload.message).to.equal('Missing data'); 167 | expect(error.output.statusCode).to.equal(400); 168 | }); 169 | 170 | it('defaults to 500', () => { 171 | 172 | const error = new Error('Missing data'); 173 | const boom = Boom.boomify(error); 174 | 175 | expect(boom).to.shallow.equal(error); 176 | expect(error.output.payload.message).to.equal('An internal server error occurred'); 177 | expect(error.output.statusCode).to.equal(500); 178 | }); 179 | 180 | it('overrides message and statusCode', () => { 181 | 182 | const error = Boom.badRequest('Missing data', { type: 'user' }); 183 | const boom = Boom.boomify(error, { message: 'Override message', statusCode: 599 }); 184 | 185 | expect(boom).to.shallow.equal(error); 186 | expect(error.data.type).to.equal('user'); 187 | expect(error.output.payload.message).to.equal('Override message: Missing data'); 188 | expect(error.output.statusCode).to.equal(599); 189 | }); 190 | 191 | it('overrides message', () => { 192 | 193 | const error = Boom.badRequest('Missing data', { type: 'user' }); 194 | const boom = Boom.boomify(error, { message: 'Override message' }); 195 | 196 | expect(boom).to.shallow.equal(error); 197 | expect(error.data.type).to.equal('user'); 198 | expect(error.output.payload.message).to.equal('Override message: Missing data'); 199 | expect(error.output.statusCode).to.equal(400); 200 | }); 201 | 202 | it('overrides statusCode', () => { 203 | 204 | const error = Boom.badRequest('Missing data', { type: 'user' }); 205 | const boom = Boom.boomify(error, { statusCode: 599 }); 206 | 207 | expect(boom).to.shallow.equal(error); 208 | expect(error.data.type).to.equal('user'); 209 | expect(error.output.payload.message).to.equal('Missing data'); 210 | expect(error.output.statusCode).to.equal(599); 211 | }); 212 | 213 | it('skips override', () => { 214 | 215 | const error = Boom.badRequest('Missing data', { type: 'user' }); 216 | const boom = Boom.boomify(error, { message: 'Override message', statusCode: 599, override: false }); 217 | 218 | expect(boom).to.shallow.equal(error); 219 | expect(error.data.type).to.equal('user'); 220 | expect(error.output.payload.message).to.equal('Missing data'); 221 | expect(error.output.statusCode).to.equal(400); 222 | }); 223 | 224 | it('initializes plain error', () => { 225 | 226 | const error = new Error('Missing data'); 227 | const boom = Boom.boomify(error, { message: 'Override message', statusCode: 599, override: false }); 228 | 229 | expect(boom).to.shallow.equal(error); 230 | expect(error.output.payload.message).to.equal('Override message: Missing data'); 231 | expect(error.output.statusCode).to.equal(599); 232 | }); 233 | }); 234 | 235 | describe('create()', () => { 236 | 237 | it('does not sets null message', () => { 238 | 239 | const error = Boom.unauthorized(null); 240 | expect(error.output.payload.message).to.equal('Unauthorized'); 241 | expect(error.isServer).to.be.false(); 242 | }); 243 | 244 | it('sets message and data', () => { 245 | 246 | const error = Boom.badRequest('Missing data', { type: 'user' }); 247 | expect(error.data.type).to.equal('user'); 248 | expect(error.output.payload.message).to.equal('Missing data'); 249 | }); 250 | }); 251 | 252 | describe('initialize()', () => { 253 | 254 | it('does not sets null message', () => { 255 | 256 | const err = new Error('some error message'); 257 | const boom = Boom.boomify(err, { statusCode: 400, message: 'modified error message' }); 258 | expect(boom.output.payload.message).to.equal('modified error message: some error message'); 259 | }); 260 | }); 261 | 262 | describe('isBoom()', () => { 263 | 264 | it('returns true for Boom object', () => { 265 | 266 | expect(Boom.badRequest().isBoom).to.equal(true); 267 | }); 268 | 269 | it('returns false for Error object', () => { 270 | 271 | expect((new Error()).isBoom).to.not.exist(); 272 | }); 273 | }); 274 | 275 | describe('badRequest()', () => { 276 | 277 | it('returns a 400 error statusCode', () => { 278 | 279 | const error = Boom.badRequest(); 280 | 281 | expect(error.output.statusCode).to.equal(400); 282 | expect(error.isServer).to.be.false(); 283 | }); 284 | 285 | it('sets the message with the passed in message', () => { 286 | 287 | expect(Boom.badRequest('my message').message).to.equal('my message'); 288 | }); 289 | 290 | it('sets the message to HTTP status if none provided', () => { 291 | 292 | expect(Boom.badRequest().message).to.equal('Bad Request'); 293 | }); 294 | }); 295 | 296 | describe('unauthorized()', () => { 297 | 298 | it('returns a 401 error statusCode', () => { 299 | 300 | const err = Boom.unauthorized(); 301 | expect(err.output.statusCode).to.equal(401); 302 | expect(err.output.headers).to.equal({}); 303 | }); 304 | 305 | it('sets the message with the passed in message', () => { 306 | 307 | expect(Boom.unauthorized('my message').message).to.equal('my message'); 308 | }); 309 | 310 | it('returns a WWW-Authenticate header when passed a scheme', () => { 311 | 312 | const err = Boom.unauthorized('boom', 'Test'); 313 | expect(err.output.statusCode).to.equal(401); 314 | expect(err.output.headers['WWW-Authenticate']).to.equal('Test error="boom"'); 315 | }); 316 | 317 | it('returns a WWW-Authenticate header set to the schema array value', () => { 318 | 319 | const err = Boom.unauthorized(null, ['Test', 'one', 'two']); 320 | expect(err.output.statusCode).to.equal(401); 321 | expect(err.output.headers['WWW-Authenticate']).to.equal('Test, one, two'); 322 | }); 323 | 324 | it('returns a WWW-Authenticate header when passed a scheme and attributes', () => { 325 | 326 | const err = Boom.unauthorized('boom', 'Test', { a: 1, b: 'something', c: null, d: 0 }); 327 | expect(err.output.statusCode).to.equal(401); 328 | expect(err.output.headers['WWW-Authenticate']).to.equal('Test a="1", b="something", c="", d="0", error="boom"'); 329 | expect(err.output.payload.attributes).to.equal({ a: 1, b: 'something', c: '', d: 0, error: 'boom' }); 330 | }); 331 | 332 | it('returns a WWW-Authenticate header from string input instead of object', () => { 333 | 334 | const err = Boom.unauthorized(null, 'Negotiate', 'VGhpcyBpcyBhIHRlc3QgdG9rZW4='); 335 | expect(err.output.statusCode).to.equal(401); 336 | expect(err.output.headers['WWW-Authenticate']).to.equal('Negotiate VGhpcyBpcyBhIHRlc3QgdG9rZW4='); 337 | expect(err.output.payload.attributes).to.equal('VGhpcyBpcyBhIHRlc3QgdG9rZW4='); 338 | }); 339 | 340 | it('returns a WWW-Authenticate header when passed attributes, missing error', () => { 341 | 342 | const err = Boom.unauthorized(null, 'Test', { a: 1, b: 'something', c: null, d: 0 }); 343 | expect(err.output.statusCode).to.equal(401); 344 | expect(err.output.headers['WWW-Authenticate']).to.equal('Test a="1", b="something", c="", d="0"'); 345 | expect(err.isMissing).to.equal(true); 346 | }); 347 | 348 | it('sets the isMissing flag when error message is empty', () => { 349 | 350 | const err = Boom.unauthorized('', 'Basic'); 351 | expect(err.isMissing).to.equal(true); 352 | }); 353 | 354 | it('does not set the isMissing flag when error message is not empty', () => { 355 | 356 | const err = Boom.unauthorized('message', 'Basic'); 357 | expect(err.isMissing).to.equal(undefined); 358 | }); 359 | 360 | it('sets a WWW-Authenticate when passed as an array', () => { 361 | 362 | const err = Boom.unauthorized('message', ['Basic', 'Example e="1"', 'Another x="3", y="4"']); 363 | expect(err.output.headers['WWW-Authenticate']).to.equal('Basic, Example e="1", Another x="3", y="4"'); 364 | }); 365 | }); 366 | 367 | 368 | describe('paymentRequired()', () => { 369 | 370 | it('returns a 402 error statusCode', () => { 371 | 372 | expect(Boom.paymentRequired().output.statusCode).to.equal(402); 373 | }); 374 | 375 | it('sets the message with the passed in message', () => { 376 | 377 | expect(Boom.paymentRequired('my message').message).to.equal('my message'); 378 | }); 379 | 380 | it('sets the message to HTTP status if none provided', () => { 381 | 382 | expect(Boom.paymentRequired().message).to.equal('Payment Required'); 383 | }); 384 | }); 385 | 386 | 387 | describe('methodNotAllowed()', () => { 388 | 389 | it('returns a 405 error statusCode', () => { 390 | 391 | expect(Boom.methodNotAllowed().output.statusCode).to.equal(405); 392 | }); 393 | 394 | it('sets the message with the passed in message', () => { 395 | 396 | expect(Boom.methodNotAllowed('my message').message).to.equal('my message'); 397 | }); 398 | 399 | it('returns an Allow header when passed a string', () => { 400 | 401 | const err = Boom.methodNotAllowed('my message', null, 'GET'); 402 | expect(err.output.statusCode).to.equal(405); 403 | expect(err.output.headers.Allow).to.equal('GET'); 404 | }); 405 | 406 | it('returns an Allow header when passed an array', () => { 407 | 408 | const err = Boom.methodNotAllowed('my message', null, ['GET', 'POST']); 409 | expect(err.output.statusCode).to.equal(405); 410 | expect(err.output.headers.Allow).to.equal('GET, POST'); 411 | }); 412 | }); 413 | 414 | 415 | describe('notAcceptable()', () => { 416 | 417 | it('returns a 406 error statusCode', () => { 418 | 419 | expect(Boom.notAcceptable().output.statusCode).to.equal(406); 420 | }); 421 | 422 | it('sets the message with the passed in message', () => { 423 | 424 | expect(Boom.notAcceptable('my message').message).to.equal('my message'); 425 | }); 426 | }); 427 | 428 | 429 | describe('proxyAuthRequired()', () => { 430 | 431 | it('returns a 407 error statusCode', () => { 432 | 433 | expect(Boom.proxyAuthRequired().output.statusCode).to.equal(407); 434 | }); 435 | 436 | it('sets the message with the passed in message', () => { 437 | 438 | expect(Boom.proxyAuthRequired('my message').message).to.equal('my message'); 439 | }); 440 | }); 441 | 442 | 443 | describe('clientTimeout()', () => { 444 | 445 | it('returns a 408 error statusCode', () => { 446 | 447 | expect(Boom.clientTimeout().output.statusCode).to.equal(408); 448 | }); 449 | 450 | it('sets the message with the passed in message', () => { 451 | 452 | expect(Boom.clientTimeout('my message').message).to.equal('my message'); 453 | }); 454 | }); 455 | 456 | 457 | describe('conflict()', () => { 458 | 459 | it('returns a 409 error statusCode', () => { 460 | 461 | expect(Boom.conflict().output.statusCode).to.equal(409); 462 | }); 463 | 464 | it('sets the message with the passed in message', () => { 465 | 466 | expect(Boom.conflict('my message').message).to.equal('my message'); 467 | }); 468 | }); 469 | 470 | 471 | describe('resourceGone()', () => { 472 | 473 | it('returns a 410 error statusCode', () => { 474 | 475 | expect(Boom.resourceGone().output.statusCode).to.equal(410); 476 | }); 477 | 478 | it('sets the message with the passed in message', () => { 479 | 480 | expect(Boom.resourceGone('my message').message).to.equal('my message'); 481 | }); 482 | }); 483 | 484 | 485 | describe('lengthRequired()', () => { 486 | 487 | it('returns a 411 error statusCode', () => { 488 | 489 | expect(Boom.lengthRequired().output.statusCode).to.equal(411); 490 | }); 491 | 492 | it('sets the message with the passed in message', () => { 493 | 494 | expect(Boom.lengthRequired('my message').message).to.equal('my message'); 495 | }); 496 | }); 497 | 498 | 499 | describe('preconditionFailed()', () => { 500 | 501 | it('returns a 412 error statusCode', () => { 502 | 503 | expect(Boom.preconditionFailed().output.statusCode).to.equal(412); 504 | }); 505 | 506 | it('sets the message with the passed in message', () => { 507 | 508 | expect(Boom.preconditionFailed('my message').message).to.equal('my message'); 509 | }); 510 | }); 511 | 512 | 513 | describe('entityTooLarge()', () => { 514 | 515 | it('returns a 413 error statusCode', () => { 516 | 517 | expect(Boom.entityTooLarge().output.statusCode).to.equal(413); 518 | }); 519 | 520 | it('sets the message with the passed in message', () => { 521 | 522 | expect(Boom.entityTooLarge('my message').message).to.equal('my message'); 523 | }); 524 | }); 525 | 526 | 527 | describe('uriTooLong()', () => { 528 | 529 | it('returns a 414 error statusCode', () => { 530 | 531 | expect(Boom.uriTooLong().output.statusCode).to.equal(414); 532 | }); 533 | 534 | it('sets the message with the passed in message', () => { 535 | 536 | expect(Boom.uriTooLong('my message').message).to.equal('my message'); 537 | }); 538 | }); 539 | 540 | 541 | describe('unsupportedMediaType()', () => { 542 | 543 | it('returns a 415 error statusCode', () => { 544 | 545 | expect(Boom.unsupportedMediaType().output.statusCode).to.equal(415); 546 | }); 547 | 548 | it('sets the message with the passed in message', () => { 549 | 550 | expect(Boom.unsupportedMediaType('my message').message).to.equal('my message'); 551 | }); 552 | }); 553 | 554 | 555 | describe('rangeNotSatisfiable()', () => { 556 | 557 | it('returns a 416 error statusCode', () => { 558 | 559 | expect(Boom.rangeNotSatisfiable().output.statusCode).to.equal(416); 560 | }); 561 | 562 | it('sets the message with the passed in message', () => { 563 | 564 | expect(Boom.rangeNotSatisfiable('my message').message).to.equal('my message'); 565 | }); 566 | }); 567 | 568 | 569 | describe('expectationFailed()', () => { 570 | 571 | it('returns a 417 error statusCode', () => { 572 | 573 | expect(Boom.expectationFailed().output.statusCode).to.equal(417); 574 | }); 575 | 576 | it('sets the message with the passed in message', () => { 577 | 578 | expect(Boom.expectationFailed('my message').message).to.equal('my message'); 579 | }); 580 | }); 581 | 582 | 583 | describe('teapot()', () => { 584 | 585 | it('returns a 418 error statusCode', () => { 586 | 587 | expect(Boom.teapot().output.statusCode).to.equal(418); 588 | }); 589 | 590 | it('sets the message with the passed in message', () => { 591 | 592 | expect(Boom.teapot('Sorry, no coffee...').message).to.equal('Sorry, no coffee...'); 593 | }); 594 | }); 595 | 596 | 597 | describe('badData()', () => { 598 | 599 | it('returns a 422 error statusCode', () => { 600 | 601 | expect(Boom.badData().output.statusCode).to.equal(422); 602 | }); 603 | 604 | it('sets the message with the passed in message', () => { 605 | 606 | expect(Boom.badData('my message').message).to.equal('my message'); 607 | }); 608 | }); 609 | 610 | 611 | describe('locked()', () => { 612 | 613 | it('returns a 423 error statusCode', () => { 614 | 615 | expect(Boom.locked().output.statusCode).to.equal(423); 616 | }); 617 | 618 | it('sets the message with the passed in message', () => { 619 | 620 | expect(Boom.locked('my message').message).to.equal('my message'); 621 | }); 622 | }); 623 | 624 | 625 | describe('preconditionRequired()', () => { 626 | 627 | it('returns a 428 error statusCode', () => { 628 | 629 | expect(Boom.preconditionRequired().output.statusCode).to.equal(428); 630 | }); 631 | 632 | it('sets the message with the passed in message', () => { 633 | 634 | expect(Boom.preconditionRequired('my message').message).to.equal('my message'); 635 | }); 636 | }); 637 | 638 | 639 | describe('tooManyRequests()', () => { 640 | 641 | it('returns a 429 error statusCode', () => { 642 | 643 | expect(Boom.tooManyRequests().output.statusCode).to.equal(429); 644 | }); 645 | 646 | it('sets the message with the passed-in message', () => { 647 | 648 | expect(Boom.tooManyRequests('my message').message).to.equal('my message'); 649 | }); 650 | }); 651 | 652 | 653 | describe('illegal()', () => { 654 | 655 | it('returns a 451 error statusCode', () => { 656 | 657 | expect(Boom.illegal().output.statusCode).to.equal(451); 658 | }); 659 | 660 | it('sets the message with the passed-in message', () => { 661 | 662 | expect(Boom.illegal('my message').message).to.equal('my message'); 663 | }); 664 | }); 665 | 666 | describe('serverUnavailable()', () => { 667 | 668 | it('returns a 503 error statusCode', () => { 669 | 670 | expect(Boom.serverUnavailable().output.statusCode).to.equal(503); 671 | }); 672 | 673 | it('sets the message with the passed in message', () => { 674 | 675 | expect(Boom.serverUnavailable('my message').message).to.equal('my message'); 676 | }); 677 | }); 678 | 679 | describe('forbidden()', () => { 680 | 681 | it('returns a 403 error statusCode', () => { 682 | 683 | expect(Boom.forbidden().output.statusCode).to.equal(403); 684 | }); 685 | 686 | it('sets the message with the passed in message', () => { 687 | 688 | expect(Boom.forbidden('my message').message).to.equal('my message'); 689 | }); 690 | }); 691 | 692 | describe('notFound()', () => { 693 | 694 | it('returns a 404 error statusCode', () => { 695 | 696 | expect(Boom.notFound().output.statusCode).to.equal(404); 697 | }); 698 | 699 | it('sets the message with the passed in message', () => { 700 | 701 | expect(Boom.notFound('my message').message).to.equal('my message'); 702 | }); 703 | }); 704 | 705 | describe('internal()', () => { 706 | 707 | it('returns a 500 error statusCode', () => { 708 | 709 | expect(Boom.internal().output.statusCode).to.equal(500); 710 | }); 711 | 712 | it('sets the message with the passed in message', () => { 713 | 714 | const err = Boom.internal('my message'); 715 | expect(err.message).to.equal('my message'); 716 | expect(err.isServer).to.true(); 717 | expect(err.output.payload.message).to.equal('An internal server error occurred'); 718 | }); 719 | 720 | it('passes data on the callback if its passed in', () => { 721 | 722 | expect(Boom.internal('my message', { my: 'data' }).data.my).to.equal('data'); 723 | }); 724 | 725 | it('returns an error with composite message', () => { 726 | 727 | const x = {}; 728 | 729 | try { 730 | x.foo(); 731 | } 732 | catch (err) { 733 | const boom = Boom.internal('Someting bad', err); 734 | expect(boom.message).to.equal('Someting bad: x.foo is not a function'); 735 | expect(boom.isServer).to.be.true(); 736 | } 737 | }); 738 | }); 739 | 740 | describe('notImplemented()', () => { 741 | 742 | it('returns a 501 error statusCode', () => { 743 | 744 | expect(Boom.notImplemented().output.statusCode).to.equal(501); 745 | }); 746 | 747 | it('sets the message with the passed in message', () => { 748 | 749 | expect(Boom.notImplemented('my message').message).to.equal('my message'); 750 | }); 751 | }); 752 | 753 | describe('badGateway()', () => { 754 | 755 | it('returns a 502 error statusCode', () => { 756 | 757 | expect(Boom.badGateway().output.statusCode).to.equal(502); 758 | }); 759 | 760 | it('sets the message with the passed in message', () => { 761 | 762 | expect(Boom.badGateway('my message').message).to.equal('my message'); 763 | }); 764 | 765 | it('retains source boom error as data when wrapped', () => { 766 | 767 | const upstream = Boom.serverUnavailable(); 768 | const boom = Boom.badGateway('Upstream error', upstream); 769 | expect(boom.output.statusCode).to.equal(502); 770 | expect(boom.data).to.equal(upstream); 771 | }); 772 | }); 773 | 774 | describe('gatewayTimeout()', () => { 775 | 776 | it('returns a 504 error statusCode', () => { 777 | 778 | expect(Boom.gatewayTimeout().output.statusCode).to.equal(504); 779 | }); 780 | 781 | it('sets the message with the passed in message', () => { 782 | 783 | expect(Boom.gatewayTimeout('my message').message).to.equal('my message'); 784 | }); 785 | }); 786 | 787 | describe('badImplementation()', () => { 788 | 789 | it('returns a 500 error statusCode', () => { 790 | 791 | const err = Boom.badImplementation(); 792 | expect(err.output.statusCode).to.equal(500); 793 | expect(err.isDeveloperError).to.equal(true); 794 | expect(err.isServer).to.be.true(); 795 | }); 796 | 797 | it('hides error from user when error data is included', () => { 798 | 799 | const err = Boom.badImplementation('Invalid', new Error('kaboom')); 800 | expect(err.output).to.equal({ 801 | headers: {}, 802 | statusCode: 500, 803 | payload: { 804 | error: 'Internal Server Error', 805 | message: 'An internal server error occurred', 806 | statusCode: 500 807 | } 808 | }); 809 | }); 810 | 811 | it('hides error from user when error data is included (boom)', () => { 812 | 813 | const err = Boom.badImplementation('Invalid', Boom.badRequest('kaboom')); 814 | expect(err.isDeveloperError).to.equal(true); 815 | expect(err.output).to.equal({ 816 | headers: {}, 817 | statusCode: 500, 818 | payload: { 819 | error: 'Internal Server Error', 820 | message: 'An internal server error occurred', 821 | statusCode: 500 822 | } 823 | }); 824 | }); 825 | }); 826 | 827 | describe('stack trace', () => { 828 | 829 | it('should omit lib', () => { 830 | 831 | ['badRequest', 'unauthorized', 'forbidden', 'notFound', 'methodNotAllowed', 832 | 'notAcceptable', 'proxyAuthRequired', 'clientTimeout', 'conflict', 833 | 'resourceGone', 'lengthRequired', 'preconditionFailed', 'entityTooLarge', 834 | 'uriTooLong', 'unsupportedMediaType', 'rangeNotSatisfiable', 'expectationFailed', 835 | 'badData', 'preconditionRequired', 'tooManyRequests', 836 | 837 | // 500s 838 | 'internal', 'notImplemented', 'badGateway', 'serverUnavailable', 839 | 'gatewayTimeout', 'badImplementation' 840 | ].forEach((name) => { 841 | 842 | const err = Boom[name](); 843 | expect(err.stack).to.not.match(/\/lib\/index\.js/); 844 | }); 845 | }); 846 | }); 847 | 848 | describe('method with error object instead of message', () => { 849 | 850 | [ 851 | 'badRequest', 852 | 'unauthorized', 853 | 'forbidden', 854 | 'notFound', 855 | 'methodNotAllowed', 856 | 'notAcceptable', 857 | 'proxyAuthRequired', 858 | 'clientTimeout', 859 | 'conflict', 860 | 'resourceGone', 861 | 'lengthRequired', 862 | 'preconditionFailed', 863 | 'entityTooLarge', 864 | 'uriTooLong', 865 | 'unsupportedMediaType', 866 | 'rangeNotSatisfiable', 867 | 'expectationFailed', 868 | 'badData', 869 | 'preconditionRequired', 870 | 'tooManyRequests', 871 | 'internal', 872 | 'notImplemented', 873 | 'badGateway', 874 | 'serverUnavailable', 875 | 'gatewayTimeout', 876 | 'badImplementation' 877 | ].forEach((name) => { 878 | 879 | it(`should allow \`Boom${name}(err)\` and preserve the error`, () => { 880 | 881 | const error = new Error('An example mongoose validation error'); 882 | error.name = 'ValidationError'; 883 | const err = Boom[name](error); 884 | expect(err.name).to.equal('ValidationError'); 885 | expect(err.message).to.equal('An example mongoose validation error'); 886 | }); 887 | 888 | // exclude unauthorized 889 | 890 | if (name !== 'unauthorized') { 891 | 892 | it(`should allow \`Boom.${name}(err, data)\` and preserve the data`, () => { 893 | 894 | const error = new Error(); 895 | const err = Boom[name](error, { foo: 'bar' }); 896 | expect(err.data).to.equal({ foo: 'bar' }); 897 | }); 898 | } 899 | }); 900 | }); 901 | 902 | describe('error.typeof', () => { 903 | 904 | const types = [ 905 | 'badRequest', 906 | 'unauthorized', 907 | 'forbidden', 908 | 'notFound', 909 | 'methodNotAllowed', 910 | 'notAcceptable', 911 | 'proxyAuthRequired', 912 | 'clientTimeout', 913 | 'conflict', 914 | 'resourceGone', 915 | 'lengthRequired', 916 | 'preconditionFailed', 917 | 'entityTooLarge', 918 | 'uriTooLong', 919 | 'unsupportedMediaType', 920 | 'rangeNotSatisfiable', 921 | 'expectationFailed', 922 | 'badData', 923 | 'preconditionRequired', 924 | 'tooManyRequests', 925 | 'internal', 926 | 'notImplemented', 927 | 'badGateway', 928 | 'serverUnavailable', 929 | 'gatewayTimeout', 930 | 'badImplementation' 931 | ]; 932 | 933 | types.forEach((name) => { 934 | 935 | it(`matches typeof Boom.${name}`, () => { 936 | 937 | const error = Boom[name](); 938 | types.forEach((type) => { 939 | 940 | if (type === name) { 941 | expect(error.typeof).to.equal(Boom[name]); 942 | } 943 | else { 944 | expect(error.typeof).to.not.equal(Boom[type]); 945 | } 946 | }); 947 | }); 948 | }); 949 | }); 950 | }); 951 | --------------------------------------------------------------------------------