Blame view
node_modules/boom/README.md
16.2 KB
f7563de62
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 |
 HTTP-friendly error objects [](http://travis-ci.org/hapijs/boom) [](https://www.npmjs.com/package/boom) Lead Maintainer: [Adam Bretz](https://github.com/arb) **boom** provides a set of utilities for returning HTTP errors. Each utility returns a `Boom` error response object (instance of `Error`) which includes the following properties: - `isBoom` - if `true`, indicates this is a `Boom` object instance. - `isServer` - convenience bool indicating status code >= 500. - `message` - the error message. - `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()` - rebuilds `error.output` using the other object properties. ## Overview - Helper methods - [`wrap(error, [statusCode], [message])`](#wraperror-statuscode-message) - [`create(statusCode, [message], [data])`](#createstatuscode-message-data) - HTTP 4xx Errors - 400: [`Boom.badRequest([message], [data])`](#boombadrequestmessage-data) - 401: [`Boom.unauthorized([message], [scheme], [attributes])`](#boomunauthorizedmessage-scheme-attributes) - 403: [`Boom.forbidden([message], [data])`](#boomforbiddenmessage-data) - 404: [`Boom.notFound([message], [data])`](#boomnotfoundmessage-data) - 405: [`Boom.methodNotAllowed([message], [data])`](#boommethodnotallowedmessage-data) - 406: [`Boom.notAcceptable([message], [data])`](#boomnotacceptablemessage-data) - 407: [`Boom.proxyAuthRequired([message], [data])`](#boomproxyauthrequiredmessage-data) - 408: [`Boom.clientTimeout([message], [data])`](#boomclienttimeoutmessage-data) - 409: [`Boom.conflict([message], [data])`](#boomconflictmessage-data) - 410: [`Boom.resourceGone([message], [data])`](#boomresourcegonemessage-data) - 411: [`Boom.lengthRequired([message], [data])`](#boomlengthrequiredmessage-data) - 412: [`Boom.preconditionFailed([message], [data])`](#boompreconditionfailedmessage-data) - 413: [`Boom.entityTooLarge([message], [data])`](#boomentitytoolargemessage-data) - 414: [`Boom.uriTooLong([message], [data])`](#boomuritoolongmessage-data) - 415: [`Boom.unsupportedMediaType([message], [data])`](#boomunsupportedmediatypemessage-data) - 416: [`Boom.rangeNotSatisfiable([message], [data])`](#boomrangenotsatisfiablemessage-data) - 417: [`Boom.expectationFailed([message], [data])`](#boomexpectationfailedmessage-data) - 422: [`Boom.badData([message], [data])`](#boombaddatamessage-data) - 428: [`Boom.preconditionRequired([message], [data])`](#boompreconditionrequiredmessage-data) - 429: [`Boom.tooManyRequests([message], [data])`](#boomtoomanyrequestsmessage-data) - HTTP 5xx Errors - 500: [`Boom.badImplementation([message], [data])`](#boombadimplementationmessage-data) - 501: [`Boom.notImplemented([message], [data])`](#boomnotimplementedmessage-data) - 502: [`Boom.badGateway([message], [data])`](#boombadgatewaymessage-data) - 503: [`Boom.serverTimeout([message], [data])`](#boomservertimeoutmessage-data) - 504: [`Boom.gatewayTimeout([message], [data])`](#boomgatewaytimeoutmessage-data) - [FAQ](#faq) ## Helper Methods ### `wrap(error, [statusCode], [message])` Decorates an error with the **boom** properties where: - `error` - the error object to wrap. If `error` is already a **boom** object, returns back the same object. - `statusCode` - optional HTTP status code. Defaults to `500`. - `message` - optional message string. If the error already has a message, it adds the message as a prefix. Defaults to no message. ```js var error = new Error('Unexpected input'); Boom.wrap(error, 400); ``` ### `create(statusCode, [message], [data])` Generates an `Error` object with the **boom** decorations where: - `statusCode` - an HTTP error code number. Must be greater or equal 400. - `message` - optional message string. - `data` - additional error data set to `error.data` property. ```js var error = Boom.create(400, 'Bad request', { timestamp: Date.now() }); ``` ## 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 `schema` 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. `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('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.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])` Returns a 405 Method Not Allowed error where: - `message` - optional message. - `data` - optional additional error data. ```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.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.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" } ``` ## 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])` 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.serverTimeout([message], [data])` Returns a 503 Service Unavailable error where: - `message` - optional message. - `data` - optional additional error data. ```js Boom.serverTimeout('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. ###### How do I include extra information in my responses? `output.payload` is missing `data`, what gives? 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. |