• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..22-Nov-2021-

lib/H03-May-2022-

node_modules/hoek/H22-Nov-2021-6453

LICENSEH A D22-Nov-20211.5 KiB2623

README.mdH A D22-Nov-202121.5 KiB805619

package.jsonH A D22-Nov-2021615 2726

README.md

1![boom Logo](https://raw.github.com/hapijs/boom/master/images/boom.png)
2
3HTTP-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
8Lead Maintainer: [Adam Bretz](https://github.com/arb)
9
10<!-- toc -->
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.failedDependency([message], [data])`](#boomfaileddependencymessage-data)
40    - [`Boom.preconditionRequired([message], [data])`](#boompreconditionrequiredmessage-data)
41    - [`Boom.tooManyRequests([message], [data])`](#boomtoomanyrequestsmessage-data)
42    - [`Boom.illegal([message], [data])`](#boomillegalmessage-data)
43  - [HTTP 5xx Errors](#http-5xx-errors)
44    - [`Boom.badImplementation([message], [data])` - (*alias: `internal`*)](#boombadimplementationmessage-data---alias-internal)
45    - [`Boom.notImplemented([message], [data])`](#boomnotimplementedmessage-data)
46    - [`Boom.badGateway([message], [data])`](#boombadgatewaymessage-data)
47    - [`Boom.serverUnavailable([message], [data])`](#boomserverunavailablemessage-data)
48    - [`Boom.gatewayTimeout([message], [data])`](#boomgatewaytimeoutmessage-data)
49  - [F.A.Q.](#faq)
50
51<!-- tocstop -->
52
53# Boom
54
55**boom** provides a set of utilities for returning HTTP errors. Each utility returns a `Boom`
56error response object which includes the following properties:
57- `isBoom` - if `true`, indicates this is a `Boom` object instance. Note that this boolean should
58  only be used if the error is an instance of `Error`. If it is not certain, use `Boom.isBoom()`
59  instead.
60- `isServer` - convenience bool indicating status code >= 500.
61- `message` - the error message.
62- `typeof` - the constructor used to create the error (e.g. `Boom.badRequest`).
63- `output` - the formatted response. Can be directly manipulated after object construction to return a custom
64  error response. Allowed root keys:
65    - `statusCode` - the HTTP status code (typically 4xx or 5xx).
66    - `headers` - an object containing any HTTP headers where each key is a header name and value is the header content.
67    - `payload` - the formatted object used as the response payload (stringified). Can be directly manipulated but any
68      changes will be lost
69      if `reformat()` is called. Any content allowed and by default includes the following content:
70        - `statusCode` - the HTTP status code, derived from `error.output.statusCode`.
71        - `error` - the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived from `statusCode`.
72        - `message` - the error message derived from `error.message`.
73- inherited `Error` properties.
74
75The `Boom` object also supports the following method:
76- `reformat()` - rebuilds `error.output` using the other object properties.
77
78Note that `Boom` object will return `true` when used with `instanceof Boom`, but do not use the
79`Boom` prototype (they are either plain `Error` or the error prototype passed in). This means
80`Boom` objects should only be tested using `instanceof Boom` or `Boom.isBoom()` but not by looking
81at the prototype or contructor information. This limitation is to avoid manipulating the prototype
82chain which is very slow.
83
84## Helper Methods
85
86### `new Boom(message, [options])`
87
88Creates a new `Boom` object using the provided `message` and then calling
89[`boomify()`](#boomifyerr-options) to decorate the error with the `Boom` properties, where:
90- `message` - the error message. If `message` is an error, it is the same as calling
91  [`boomify()`](#boomifyerr-options) directly.
92- `options` - and optional object where:
93	- `statusCode` - the HTTP status code. Defaults to `500` if no status code is already set.
94    - `data` - additional error information (assigned to `error.data`).
95    - `decorate` - an option with extra properties to set on the error object.
96    - `ctor` - constructor reference used to crop the exception call stack output.
97    - if `message` is an error object, also supports the other [`boomify()`](#boomifyerr-options)
98      options.
99
100### `boomify(err, [options])`
101
102Decorates an error with the `Boom` properties where:
103- `err` - the `Error` object to decorate.
104- `options` - optional object with the following optional settings:
105	- `statusCode` - the HTTP status code. Defaults to `500` if no status code is already set and `err` is not a `Boom` object.
106	- `message` - error message string. If the error already has a message, the provided `message` is added as a prefix.
107	  Defaults to no message.
108    - `decorate` - an option with extra properties to set on the error object.
109	- `override` - if `false`, the `err` provided is a `Boom` object, and a `statusCode` or `message` are provided,
110	  the values are ignored. Defaults to `true` (apply the provided `statusCode` and `message` options to the error
111	  regardless of its type, `Error` or `Boom` object).
112
113```js
114var error = new Error('Unexpected input');
115Boom.boomify(error, { statusCode: 400 });
116```
117
118### `isBoom(err)`
119
120Identifies whether an error is a `Boom` object. Same as calling `instanceof Boom`.
121
122## HTTP 4xx Errors
123
124### `Boom.badRequest([message], [data])`
125
126Returns a 400 Bad Request error where:
127- `message` - optional message.
128- `data` - optional additional error data.
129
130```js
131Boom.badRequest('invalid query');
132```
133
134Generates the following response payload:
135
136```json
137{
138    "statusCode": 400,
139    "error": "Bad Request",
140    "message": "invalid query"
141}
142```
143
144### `Boom.unauthorized([message], [scheme], [attributes])`
145
146Returns a 401 Unauthorized error where:
147- `message` - optional message.
148- `scheme` can be one of the following:
149  - an authentication scheme name
150  - an array of string values. These values will be separated by ', ' and set to the 'WWW-Authenticate' header.
151- `attributes` - an object of values to use while setting the 'WWW-Authenticate' header. This value is only used
152  when `scheme` is a string, otherwise it is ignored. Every key/value pair will be included in the
153  '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.
154  `null` and `undefined` will be replaced with an empty string. If `attributes` is set, `message` will be used as
155  the 'error' segment of the 'WWW-Authenticate' header. If `message` is unset, the 'error' segment of the header
156  will not be present and `isMissing` will be true on the error object.
157
158If either `scheme` or `attributes` are set, the resultant `Boom` object will have the
159'WWW-Authenticate' header set for the response.
160
161```js
162Boom.unauthorized('invalid password');
163```
164
165Generates the following response:
166
167```json
168"payload": {
169    "statusCode": 401,
170    "error": "Unauthorized",
171    "message": "invalid password"
172},
173"headers" {}
174```
175
176```js
177Boom.unauthorized('invalid password', 'sample');
178```
179
180Generates the following response:
181
182```json
183"payload": {
184    "statusCode": 401,
185    "error": "Unauthorized",
186    "message": "invalid password",
187    "attributes": {
188        "error": "invalid password"
189    }
190},
191"headers" {
192  "WWW-Authenticate": "sample error=\"invalid password\""
193}
194```
195
196```js
197Boom.unauthorized(null, 'Negotiate', 'VGhpcyBpcyBhIHRlc3QgdG9rZW4=');
198```
199
200Generates the following response:
201
202```json
203"payload": {
204    "statusCode": 401,
205    "error": "Unauthorized",
206    "attributes": "VGhpcyBpcyBhIHRlc3QgdG9rZW4="
207},
208"headers" {
209  "WWW-Authenticate": "Negotiate VGhpcyBpcyBhIHRlc3QgdG9rZW4="
210}
211```
212
213```js
214Boom.unauthorized('invalid password', 'sample', { ttl: 0, cache: null, foo: 'bar' });
215```
216
217Generates the following response:
218
219```json
220"payload": {
221    "statusCode": 401,
222    "error": "Unauthorized",
223    "message": "invalid password",
224    "attributes": {
225        "error": "invalid password",
226        "ttl": 0,
227        "cache": "",
228        "foo": "bar"
229    }
230},
231"headers" {
232  "WWW-Authenticate": "sample ttl=\"0\", cache=\"\", foo=\"bar\", error=\"invalid password\""
233}
234```
235
236### `Boom.paymentRequired([message], [data])`
237
238Returns a 402 Payment Required error where:
239- `message` - optional message.
240- `data` - optional additional error data.
241
242```js
243Boom.paymentRequired('bandwidth used');
244```
245
246Generates the following response payload:
247
248```json
249{
250    "statusCode": 402,
251    "error": "Payment Required",
252    "message": "bandwidth used"
253}
254```
255
256### `Boom.forbidden([message], [data])`
257
258Returns a 403 Forbidden error where:
259- `message` - optional message.
260- `data` - optional additional error data.
261
262```js
263Boom.forbidden('try again some time');
264```
265
266Generates the following response payload:
267
268```json
269{
270    "statusCode": 403,
271    "error": "Forbidden",
272    "message": "try again some time"
273}
274```
275
276### `Boom.notFound([message], [data])`
277
278Returns a 404 Not Found error where:
279- `message` - optional message.
280- `data` - optional additional error data.
281
282```js
283Boom.notFound('missing');
284```
285
286Generates the following response payload:
287
288```json
289{
290    "statusCode": 404,
291    "error": "Not Found",
292    "message": "missing"
293}
294```
295
296### `Boom.methodNotAllowed([message], [data], [allow])`
297
298Returns a 405 Method Not Allowed error where:
299- `message` - optional message.
300- `data` - optional additional error data.
301- `allow` - optional string or array of strings (to be combined and separated by ', ') which is set to the 'Allow' header.
302
303```js
304Boom.methodNotAllowed('that method is not allowed');
305```
306
307Generates the following response payload:
308
309```json
310{
311    "statusCode": 405,
312    "error": "Method Not Allowed",
313    "message": "that method is not allowed"
314}
315```
316
317### `Boom.notAcceptable([message], [data])`
318
319Returns a 406 Not Acceptable error where:
320- `message` - optional message.
321- `data` - optional additional error data.
322
323```js
324Boom.notAcceptable('unacceptable');
325```
326
327Generates the following response payload:
328
329```json
330{
331    "statusCode": 406,
332    "error": "Not Acceptable",
333    "message": "unacceptable"
334}
335```
336
337### `Boom.proxyAuthRequired([message], [data])`
338
339Returns a 407 Proxy Authentication Required error where:
340- `message` - optional message.
341- `data` - optional additional error data.
342
343```js
344Boom.proxyAuthRequired('auth missing');
345```
346
347Generates the following response payload:
348
349```json
350{
351    "statusCode": 407,
352    "error": "Proxy Authentication Required",
353    "message": "auth missing"
354}
355```
356
357### `Boom.clientTimeout([message], [data])`
358
359Returns a 408 Request Time-out error where:
360- `message` - optional message.
361- `data` - optional additional error data.
362
363```js
364Boom.clientTimeout('timed out');
365```
366
367Generates the following response payload:
368
369```json
370{
371    "statusCode": 408,
372    "error": "Request Time-out",
373    "message": "timed out"
374}
375```
376
377### `Boom.conflict([message], [data])`
378
379Returns a 409 Conflict error where:
380- `message` - optional message.
381- `data` - optional additional error data.
382
383```js
384Boom.conflict('there was a conflict');
385```
386
387Generates the following response payload:
388
389```json
390{
391    "statusCode": 409,
392    "error": "Conflict",
393    "message": "there was a conflict"
394}
395```
396
397### `Boom.resourceGone([message], [data])`
398
399Returns a 410 Gone error where:
400- `message` - optional message.
401- `data` - optional additional error data.
402
403```js
404Boom.resourceGone('it is gone');
405```
406
407Generates the following response payload:
408
409```json
410{
411    "statusCode": 410,
412    "error": "Gone",
413    "message": "it is gone"
414}
415```
416
417### `Boom.lengthRequired([message], [data])`
418
419Returns a 411 Length Required error where:
420- `message` - optional message.
421- `data` - optional additional error data.
422
423```js
424Boom.lengthRequired('length needed');
425```
426
427Generates the following response payload:
428
429```json
430{
431    "statusCode": 411,
432    "error": "Length Required",
433    "message": "length needed"
434}
435```
436
437### `Boom.preconditionFailed([message], [data])`
438
439Returns a 412 Precondition Failed error where:
440- `message` - optional message.
441- `data` - optional additional error data.
442
443```js
444Boom.preconditionFailed();
445```
446
447Generates the following response payload:
448
449```json
450{
451    "statusCode": 412,
452    "error": "Precondition Failed"
453}
454```
455
456### `Boom.entityTooLarge([message], [data])`
457
458Returns a 413 Request Entity Too Large error where:
459- `message` - optional message.
460- `data` - optional additional error data.
461
462```js
463Boom.entityTooLarge('too big');
464```
465
466Generates the following response payload:
467
468```json
469{
470    "statusCode": 413,
471    "error": "Request Entity Too Large",
472    "message": "too big"
473}
474```
475
476### `Boom.uriTooLong([message], [data])`
477
478Returns a 414 Request-URI Too Large error where:
479- `message` - optional message.
480- `data` - optional additional error data.
481
482```js
483Boom.uriTooLong('uri is too long');
484```
485
486Generates the following response payload:
487
488```json
489{
490    "statusCode": 414,
491    "error": "Request-URI Too Large",
492    "message": "uri is too long"
493}
494```
495
496### `Boom.unsupportedMediaType([message], [data])`
497
498Returns a 415 Unsupported Media Type error where:
499- `message` - optional message.
500- `data` - optional additional error data.
501
502```js
503Boom.unsupportedMediaType('that media is not supported');
504```
505
506Generates the following response payload:
507
508```json
509{
510    "statusCode": 415,
511    "error": "Unsupported Media Type",
512    "message": "that media is not supported"
513}
514```
515
516### `Boom.rangeNotSatisfiable([message], [data])`
517
518Returns a 416 Requested Range Not Satisfiable error where:
519- `message` - optional message.
520- `data` - optional additional error data.
521
522```js
523Boom.rangeNotSatisfiable();
524```
525
526Generates the following response payload:
527
528```json
529{
530    "statusCode": 416,
531    "error": "Requested Range Not Satisfiable"
532}
533```
534
535### `Boom.expectationFailed([message], [data])`
536
537Returns a 417 Expectation Failed error where:
538- `message` - optional message.
539- `data` - optional additional error data.
540
541```js
542Boom.expectationFailed('expected this to work');
543```
544
545Generates the following response payload:
546
547```json
548{
549    "statusCode": 417,
550    "error": "Expectation Failed",
551    "message": "expected this to work"
552}
553```
554
555### `Boom.teapot([message], [data])`
556
557Returns a 418 I'm a Teapot error where:
558- `message` - optional message.
559- `data` - optional additional error data.
560
561```js
562Boom.teapot('sorry, no coffee...');
563```
564
565Generates the following response payload:
566
567```json
568{
569    "statusCode": 418,
570    "error": "I'm a Teapot",
571    "message": "Sorry, no coffee..."
572}
573```
574
575### `Boom.badData([message], [data])`
576
577Returns a 422 Unprocessable Entity error where:
578- `message` - optional message.
579- `data` - optional additional error data.
580
581```js
582Boom.badData('your data is bad and you should feel bad');
583```
584
585Generates the following response payload:
586
587```json
588{
589    "statusCode": 422,
590    "error": "Unprocessable Entity",
591    "message": "your data is bad and you should feel bad"
592}
593```
594
595### `Boom.locked([message], [data])`
596
597Returns a 423 Locked error where:
598- `message` - optional message.
599- `data` - optional additional error data.
600
601```js
602Boom.locked('this resource has been locked');
603```
604
605Generates the following response payload:
606
607```json
608{
609    "statusCode": 423,
610    "error": "Locked",
611    "message": "this resource has been locked"
612}
613```
614
615### `Boom.failedDependency([message], [data])`
616
617Returns a 424 Failed Dependency error where:
618- `message` - optional message.
619- `data` - optional additional error data.
620
621```js
622Boom.failedDependency('an external resource failed');
623```
624
625Generates the following response payload:
626
627```json
628{
629    "statusCode": 424,
630    "error": "Failed Dependency",
631    "message": "an external resource failed"
632}
633```
634
635### `Boom.preconditionRequired([message], [data])`
636
637Returns a 428 Precondition Required error where:
638- `message` - optional message.
639- `data` - optional additional error data.
640
641```js
642Boom.preconditionRequired('you must supply an If-Match header');
643```
644
645Generates the following response payload:
646
647```json
648{
649    "statusCode": 428,
650    "error": "Precondition Required",
651    "message": "you must supply an If-Match header"
652}
653```
654
655### `Boom.tooManyRequests([message], [data])`
656
657Returns a 429 Too Many Requests error where:
658- `message` - optional message.
659- `data` - optional additional error data.
660
661```js
662Boom.tooManyRequests('you have exceeded your request limit');
663```
664
665Generates the following response payload:
666
667```json
668{
669    "statusCode": 429,
670    "error": "Too Many Requests",
671    "message": "you have exceeded your request limit"
672}
673```
674
675### `Boom.illegal([message], [data])`
676
677Returns a 451 Unavailable For Legal Reasons error where:
678- `message` - optional message.
679- `data` - optional additional error data.
680
681```js
682Boom.illegal('you are not permitted to view this resource for legal reasons');
683```
684
685Generates the following response payload:
686
687```json
688{
689    "statusCode": 451,
690    "error": "Unavailable For Legal Reasons",
691    "message": "you are not permitted to view this resource for legal reasons"
692}
693```
694
695## HTTP 5xx Errors
696
697All 500 errors hide your message from the end user. Your message is recorded in the server log.
698
699### `Boom.badImplementation([message], [data])` - (*alias: `internal`*)
700
701Returns a 500 Internal Server Error error where:
702- `message` - optional message.
703- `data` - optional additional error data.
704
705```js
706Boom.badImplementation('terrible implementation');
707```
708
709Generates the following response payload:
710
711```json
712{
713    "statusCode": 500,
714    "error": "Internal Server Error",
715    "message": "An internal server error occurred"
716}
717```
718
719### `Boom.notImplemented([message], [data])`
720
721Returns a 501 Not Implemented error where:
722- `message` - optional message.
723- `data` - optional additional error data.
724
725```js
726Boom.notImplemented('method not implemented');
727```
728
729Generates the following response payload:
730
731```json
732{
733    "statusCode": 501,
734    "error": "Not Implemented",
735    "message": "method not implemented"
736}
737```
738
739### `Boom.badGateway([message], [data])`
740
741Returns a 502 Bad Gateway error where:
742- `message` - optional message.
743- `data` - optional additional error data.
744
745```js
746Boom.badGateway('that is a bad gateway');
747```
748
749Generates the following response payload:
750
751```json
752{
753    "statusCode": 502,
754    "error": "Bad Gateway",
755    "message": "that is a bad gateway"
756}
757```
758
759### `Boom.serverUnavailable([message], [data])`
760
761Returns a 503 Service Unavailable error where:
762- `message` - optional message.
763- `data` - optional additional error data.
764
765```js
766Boom.serverUnavailable('unavailable');
767```
768
769Generates the following response payload:
770
771```json
772{
773    "statusCode": 503,
774    "error": "Service Unavailable",
775    "message": "unavailable"
776}
777```
778
779### `Boom.gatewayTimeout([message], [data])`
780
781Returns a 504 Gateway Time-out error where:
782- `message` - optional message.
783- `data` - optional additional error data.
784
785```js
786Boom.gatewayTimeout();
787```
788
789Generates the following response payload:
790
791```json
792{
793    "statusCode": 504,
794    "error": "Gateway Time-out"
795}
796```
797
798## F.A.Q.
799
800**Q** How do I include extra information in my responses? `output.payload` is missing `data`, what gives?
801
802**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.
803
804---
805