1 | ![boom Logo](https://raw.github.com/hapijs/boom/master/images/boom.png)
|
2 |
|
3 | HTTP-friendly error objects
|
4 |
|
5 | [![Build Status](https://secure.travis-ci.org/hapijs/boom.png)](http://travis-ci.org/hapijs/boom)
|
6 |
|
7 | Lead Maintainer: [Adam Bretz](https://github.com/arb)
|
8 |
|
9 | **boom** provides a set of utilities for returning HTTP errors. Each utility returns a `Boom` error response
|
10 | object (instance of `Error`) which includes the following properties:
|
11 | - `isBoom` - if `true`, indicates this is a `Boom` object instance.
|
12 | - `isServer` - convenience bool indicating status code >= 500.
|
13 | - `message` - the error message.
|
14 | - `output` - the formatted response. Can be directly manipulated after object construction to return a custom
|
15 | error response. Allowed root keys:
|
16 | - `statusCode` - the HTTP status code (typically 4xx or 5xx).
|
17 | - `headers` - an object containing any HTTP headers where each key is a header name and value is the header content.
|
18 | - `payload` - the formatted object used as the response payload (stringified). Can be directly manipulated but any
|
19 | changes will be lost
|
20 | if `reformat()` is called. Any content allowed and by default includes the following content:
|
21 | - `statusCode` - the HTTP status code, derived from `error.output.statusCode`.
|
22 | - `error` - the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived from `statusCode`.
|
23 | - `message` - the error message derived from `error.message`.
|
24 | - inherited `Error` properties.
|
25 |
|
26 | The `Boom` object also supports the following method:
|
27 | - `reformat()` - rebuilds `error.output` using the other object properties.
|
28 |
|
29 | ## Helper Methods
|
30 |
|
31 | ### `wrap(error, [statusCode], [message])`
|
32 |
|
33 | Decorates an error with the **boom** properties where:
|
34 | - `error` - the error object to wrap. If `error` is already a **boom** object, returns back the same object.
|
35 | - `statusCode` - optional HTTP status code. Defaults to `500`.
|
36 | - `message` - optional message string. If the error already has a message, it adds the message as a prefix.
|
37 | Defaults to no message.
|
38 |
|
39 | ```js
|
40 | var error = new Error('Unexpected input');
|
41 | Boom.wrap(error, 400);
|
42 | ```
|
43 |
|
44 | ### `create(statusCode, [message], [data])`
|
45 |
|
46 | Generates an `Error` object with the **boom** decorations where:
|
47 | - `statusCode` - an HTTP error code number. Must be greater or equal 400.
|
48 | - `message` - optional message string.
|
49 | - `data` - additional error data set to `error.data` property.
|
50 |
|
51 | ```js
|
52 | var error = Boom.create(400, 'Bad request', { timestamp: Date.now() });
|
53 | ```
|
54 |
|
55 | ## HTTP 4xx Errors
|
56 |
|
57 | ### `Boom.badRequest([message], [data])`
|
58 |
|
59 | Returns a 400 Bad Request error where:
|
60 | - `message` - optional message.
|
61 | - `data` - optional additional error data.
|
62 |
|
63 | ```js
|
64 | Boom.badRequest('invalid query');
|
65 | ```
|
66 |
|
67 | Generates the following response payload:
|
68 |
|
69 | ```json
|
70 | {
|
71 | "statusCode": 400,
|
72 | "error": "Bad Request",
|
73 | "message": "invalid query"
|
74 | }
|
75 | ```
|
76 |
|
77 | ### `Boom.unauthorized([message], [scheme], [attributes])`
|
78 |
|
79 | Returns a 401 Unauthorized error where:
|
80 | - `message` - optional message.
|
81 | - `scheme` can be one of the following:
|
82 | - an authentication scheme name
|
83 | - an array of string values. These values will be separated by ', ' and set to the 'WWW-Authenticate' header.
|
84 | - `attributes` - an object of values to use while setting the 'WWW-Authenticate' header. This value is only used when `schema` is a string, otherwise it is ignored. Every key/value pair will be included in the 'WWW-Authenticate' in the format of 'key="value"'. `null` and `undefined` will be replaced with an empty string. If `attributes` is set, `message` will be used as the 'error' segment of the 'WWW-Authenticate' header. If `message` is unset, the 'error' segment of the header will not be present and `isMissing` will be true on the error object.
|
85 |
|
86 | If either `scheme` or `attributes` are set, the resultant `Boom` object will have the 'WWW-Authenticate' header set for the response.
|
87 |
|
88 | ```js
|
89 | Boom.unauthorized('invalid password');
|
90 | ```
|
91 |
|
92 | Generates the following response:
|
93 |
|
94 | ```json
|
95 | "payload": {
|
96 | "statusCode": 401,
|
97 | "error": "Unauthorized",
|
98 | "message": "invalid password"
|
99 | },
|
100 | "headers" {}
|
101 | ```
|
102 |
|
103 | ```js
|
104 | Boom.unauthorized('invalid password', 'sample');
|
105 | ```
|
106 |
|
107 | Generates the following response:
|
108 |
|
109 | ```json
|
110 | "payload": {
|
111 | "statusCode": 401,
|
112 | "error": "Unauthorized",
|
113 | "message": "invalid password"
|
114 | },
|
115 | "headers" {
|
116 | "WWW-Authenticate": "sample error=\"invalid password\""
|
117 | }
|
118 | ```
|
119 |
|
120 | ```js
|
121 | Boom.unauthorized('invalid password', 'sample', { ttl: 0, cache: null, foo: 'bar' });
|
122 | ```
|
123 |
|
124 | Generates the following response:
|
125 |
|
126 | ```json
|
127 | "payload": {
|
128 | "statusCode": 401,
|
129 | "error": "Unauthorized",
|
130 | "message": "invalid password"
|
131 | },
|
132 | "headers" {
|
133 | "WWW-Authenticate": "sample ttl=\"0\", cache=\"\", foo=\"bar\", error=\"invalid password\""
|
134 | }
|
135 | ```
|
136 |
|
137 | ### `Boom.forbidden([message], [data])`
|
138 |
|
139 | Returns a 403 Forbidden error where:
|
140 | - `message` - optional message.
|
141 | - `data` - optional additional error data.
|
142 |
|
143 | ```js
|
144 | Boom.forbidden('try again some time');
|
145 | ```
|
146 |
|
147 | Generates the following response payload:
|
148 |
|
149 | ```json
|
150 | {
|
151 | "statusCode": 403,
|
152 | "error": "Forbidden",
|
153 | "message": "try again some time"
|
154 | }
|
155 | ```
|
156 |
|
157 | ### `Boom.notFound([message], [data])`
|
158 |
|
159 | Returns a 404 Not Found error where:
|
160 | - `message` - optional message.
|
161 | - `data` - optional additional error data.
|
162 |
|
163 | ```js
|
164 | Boom.notFound('missing');
|
165 | ```
|
166 |
|
167 | Generates the following response payload:
|
168 |
|
169 | ```json
|
170 | {
|
171 | "statusCode": 404,
|
172 | "error": "Not Found",
|
173 | "message": "missing"
|
174 | }
|
175 | ```
|
176 |
|
177 | ### `Boom.methodNotAllowed([message], [data])`
|
178 |
|
179 | Returns a 405 Method Not Allowed error where:
|
180 | - `message` - optional message.
|
181 | - `data` - optional additional error data.
|
182 |
|
183 | ```js
|
184 | Boom.methodNotAllowed('that method is not allowed');
|
185 | ```
|
186 |
|
187 | Generates the following response payload:
|
188 |
|
189 | ```json
|
190 | {
|
191 | "statusCode": 405,
|
192 | "error": "Method Not Allowed",
|
193 | "message": "that method is not allowed"
|
194 | }
|
195 | ```
|
196 |
|
197 | ### `Boom.notAcceptable([message], [data])`
|
198 |
|
199 | Returns a 406 Not Acceptable error where:
|
200 | - `message` - optional message.
|
201 | - `data` - optional additional error data.
|
202 |
|
203 | ```js
|
204 | Boom.notAcceptable('unacceptable');
|
205 | ```
|
206 |
|
207 | Generates the following response payload:
|
208 |
|
209 | ```json
|
210 | {
|
211 | "statusCode": 406,
|
212 | "error": "Not Acceptable",
|
213 | "message": "unacceptable"
|
214 | }
|
215 | ```
|
216 |
|
217 | ### `Boom.proxyAuthRequired([message], [data])`
|
218 |
|
219 | Returns a 407 Proxy Authentication Required error where:
|
220 | - `message` - optional message.
|
221 | - `data` - optional additional error data.
|
222 |
|
223 | ```js
|
224 | Boom.proxyAuthRequired('auth missing');
|
225 | ```
|
226 |
|
227 | Generates the following response payload:
|
228 |
|
229 | ```json
|
230 | {
|
231 | "statusCode": 407,
|
232 | "error": "Proxy Authentication Required",
|
233 | "message": "auth missing"
|
234 | }
|
235 | ```
|
236 |
|
237 | ### `Boom.clientTimeout([message], [data])`
|
238 |
|
239 | Returns a 408 Request Time-out error where:
|
240 | - `message` - optional message.
|
241 | - `data` - optional additional error data.
|
242 |
|
243 | ```js
|
244 | Boom.clientTimeout('timed out');
|
245 | ```
|
246 |
|
247 | Generates the following response payload:
|
248 |
|
249 | ```json
|
250 | {
|
251 | "statusCode": 408,
|
252 | "error": "Request Time-out",
|
253 | "message": "timed out"
|
254 | }
|
255 | ```
|
256 |
|
257 | ### `Boom.conflict([message], [data])`
|
258 |
|
259 | Returns a 409 Conflict error where:
|
260 | - `message` - optional message.
|
261 | - `data` - optional additional error data.
|
262 |
|
263 | ```js
|
264 | Boom.conflict('there was a conflict');
|
265 | ```
|
266 |
|
267 | Generates the following response payload:
|
268 |
|
269 | ```json
|
270 | {
|
271 | "statusCode": 409,
|
272 | "error": "Conflict",
|
273 | "message": "there was a conflict"
|
274 | }
|
275 | ```
|
276 |
|
277 | ### `Boom.resourceGone([message], [data])`
|
278 |
|
279 | Returns a 410 Gone error where:
|
280 | - `message` - optional message.
|
281 | - `data` - optional additional error data.
|
282 |
|
283 | ```js
|
284 | Boom.resourceGone('it is gone');
|
285 | ```
|
286 |
|
287 | Generates the following response payload:
|
288 |
|
289 | ```json
|
290 | {
|
291 | "statusCode": 410,
|
292 | "error": "Gone",
|
293 | "message": "it is gone"
|
294 | }
|
295 | ```
|
296 |
|
297 | ### `Boom.lengthRequired([message], [data])`
|
298 |
|
299 | Returns a 411 Length Required error where:
|
300 | - `message` - optional message.
|
301 | - `data` - optional additional error data.
|
302 |
|
303 | ```js
|
304 | Boom.lengthRequired('length needed');
|
305 | ```
|
306 |
|
307 | Generates the following response payload:
|
308 |
|
309 | ```json
|
310 | {
|
311 | "statusCode": 411,
|
312 | "error": "Length Required",
|
313 | "message": "length needed"
|
314 | }
|
315 | ```
|
316 |
|
317 | ### `Boom.preconditionFailed([message], [data])`
|
318 |
|
319 | Returns a 412 Precondition Failed error where:
|
320 | - `message` - optional message.
|
321 | - `data` - optional additional error data.
|
322 |
|
323 | ```js
|
324 | Boom.preconditionFailed();
|
325 | ```
|
326 |
|
327 | Generates the following response payload:
|
328 |
|
329 | ```json
|
330 | {
|
331 | "statusCode": 412,
|
332 | "error": "Precondition Failed"
|
333 | }
|
334 | ```
|
335 |
|
336 | ### `Boom.entityTooLarge([message], [data])`
|
337 |
|
338 | Returns a 413 Request Entity Too Large error where:
|
339 | - `message` - optional message.
|
340 | - `data` - optional additional error data.
|
341 |
|
342 | ```js
|
343 | Boom.entityTooLarge('too big');
|
344 | ```
|
345 |
|
346 | Generates the following response payload:
|
347 |
|
348 | ```json
|
349 | {
|
350 | "statusCode": 413,
|
351 | "error": "Request Entity Too Large",
|
352 | "message": "too big"
|
353 | }
|
354 | ```
|
355 |
|
356 | ### `Boom.uriTooLong([message], [data])`
|
357 |
|
358 | Returns a 414 Request-URI Too Large error where:
|
359 | - `message` - optional message.
|
360 | - `data` - optional additional error data.
|
361 |
|
362 | ```js
|
363 | Boom.uriTooLong('uri is too long');
|
364 | ```
|
365 |
|
366 | Generates the following response payload:
|
367 |
|
368 | ```json
|
369 | {
|
370 | "statusCode": 414,
|
371 | "error": "Request-URI Too Large",
|
372 | "message": "uri is too long"
|
373 | }
|
374 | ```
|
375 |
|
376 | ### `Boom.unsupportedMediaType([message], [data])`
|
377 |
|
378 | Returns a 415 Unsupported Media Type error where:
|
379 | - `message` - optional message.
|
380 | - `data` - optional additional error data.
|
381 |
|
382 | ```js
|
383 | Boom.unsupportedMediaType('that media is not supported');
|
384 | ```
|
385 |
|
386 | Generates the following response payload:
|
387 |
|
388 | ```json
|
389 | {
|
390 | "statusCode": 415,
|
391 | "error": "Unsupported Media Type",
|
392 | "message": "that media is not supported"
|
393 | }
|
394 | ```
|
395 |
|
396 | ### `Boom.rangeNotSatisfiable([message], [data])`
|
397 |
|
398 | Returns a 416 Requested Range Not Satisfiable error where:
|
399 | - `message` - optional message.
|
400 | - `data` - optional additional error data.
|
401 |
|
402 | ```js
|
403 | Boom.rangeNotSatisfiable();
|
404 | ```
|
405 |
|
406 | Generates the following response payload:
|
407 |
|
408 | ```json
|
409 | {
|
410 | "statusCode": 416,
|
411 | "error": "Requested Range Not Satisfiable"
|
412 | }
|
413 | ```
|
414 |
|
415 | ### `Boom.expectationFailed([message], [data])`
|
416 |
|
417 | Returns a 417 Expectation Failed error where:
|
418 | - `message` - optional message.
|
419 | - `data` - optional additional error data.
|
420 |
|
421 | ```js
|
422 | Boom.expectationFailed('expected this to work');
|
423 | ```
|
424 |
|
425 | Generates the following response payload:
|
426 |
|
427 | ```json
|
428 | {
|
429 | "statusCode": 417,
|
430 | "error": "Expectation Failed",
|
431 | "message": "expected this to work"
|
432 | }
|
433 | ```
|
434 |
|
435 | ### `Boom.badData([message], [data])`
|
436 |
|
437 | Returns a 422 Unprocessable Entity error where:
|
438 | - `message` - optional message.
|
439 | - `data` - optional additional error data.
|
440 |
|
441 | ```js
|
442 | Boom.badData('your data is bad and you should feel bad');
|
443 | ```
|
444 |
|
445 | Generates the following response payload:
|
446 |
|
447 | ```json
|
448 | {
|
449 | "statusCode": 422,
|
450 | "error": "Unprocessable Entity",
|
451 | "message": "your data is bad and you should feel bad"
|
452 | }
|
453 | ```
|
454 |
|
455 | ### `Boom.tooManyRequests([message], [data])`
|
456 |
|
457 | Returns a 429 Too Many Requests error where:
|
458 | - `message` - optional message.
|
459 | - `data` - optional additional error data.
|
460 |
|
461 | ```js
|
462 | Boom.tooManyRequests('you have exceeded your request limit');
|
463 | ```
|
464 |
|
465 | Generates the following response payload:
|
466 |
|
467 | ```json
|
468 | {
|
469 | "statusCode": 429,
|
470 | "error": "Too Many Requests",
|
471 | "message": "you have exceeded your request limit"
|
472 | }
|
473 | ```
|
474 |
|
475 | ## HTTP 5xx Errors
|
476 |
|
477 | All 500 errors hide your message from the end user. Your message is recorded in the server log.
|
478 |
|
479 | ### `Boom.notImplemented([message], [data])`
|
480 |
|
481 | Returns a 501 Not Implemented error where:
|
482 | - `message` - optional message.
|
483 | - `data` - optional additional error data.
|
484 |
|
485 | ```js
|
486 | Boom.notImplemented('method not implemented');
|
487 | ```
|
488 |
|
489 | Generates the following response payload:
|
490 |
|
491 | ```json
|
492 | {
|
493 | "statusCode": 501,
|
494 | "error": "Not Implemented",
|
495 | "message": "method not implemented"
|
496 | }
|
497 | ```
|
498 |
|
499 | ### `Boom.badGateway([message], [data])`
|
500 |
|
501 | Returns a 502 Bad Gateway error where:
|
502 | - `message` - optional message.
|
503 | - `data` - optional additional error data.
|
504 |
|
505 | ```js
|
506 | Boom.badGateway('that is a bad gateway');
|
507 | ```
|
508 |
|
509 | Generates the following response payload:
|
510 |
|
511 | ```json
|
512 | {
|
513 | "statusCode": 502,
|
514 | "error": "Bad Gateway",
|
515 | "message": "that is a bad gateway"
|
516 | }
|
517 | ```
|
518 |
|
519 | ### `Boom.serverTimeout([message], [data])`
|
520 |
|
521 | Returns a 503 Service Unavailable error where:
|
522 | - `message` - optional message.
|
523 | - `data` - optional additional error data.
|
524 |
|
525 | ```js
|
526 | Boom.serverTimeout('unavailable');
|
527 | ```
|
528 |
|
529 | Generates the following response payload:
|
530 |
|
531 | ```json
|
532 | {
|
533 | "statusCode": 503,
|
534 | "error": "Service Unavailable",
|
535 | "message": "unavailable"
|
536 | }
|
537 | ```
|
538 |
|
539 | ### `Boom.gatewayTimeout([message], [data])`
|
540 |
|
541 | Returns a 504 Gateway Time-out error where:
|
542 | - `message` - optional message.
|
543 | - `data` - optional additional error data.
|
544 |
|
545 | ```js
|
546 | Boom.gatewayTimeout();
|
547 | ```
|
548 |
|
549 | Generates the following response payload:
|
550 |
|
551 | ```json
|
552 | {
|
553 | "statusCode": 504,
|
554 | "error": "Gateway Time-out"
|
555 | }
|
556 | ```
|
557 |
|
558 | ### `Boom.badImplementation([message], [data])`
|
559 |
|
560 | Returns a 500 Internal Server Error error where:
|
561 | - `message` - optional message.
|
562 | - `data` - optional additional error data.
|
563 |
|
564 | ```js
|
565 | Boom.badImplementation('terrible implementation');
|
566 | ```
|
567 |
|
568 | Generates the following response payload:
|
569 |
|
570 | ```json
|
571 | {
|
572 | "statusCode": 500,
|
573 | "error": "Internal Server Error",
|
574 | "message": "An internal server error occurred"
|
575 | }
|
576 | ```
|