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