UNPKG

12.9 kBMarkdownView Raw
1![boom Logo](https://raw.github.com/hapijs/boom/master/images/boom.png)
2
3HTTP-friendly error objects
4
5[![Build Status](https://secure.travis-ci.org/hapijs/boom.png)](http://travis-ci.org/hapijs/boom)
6
7Lead 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
10object (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
26The `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
33Decorates 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
40var error = new Error('Unexpected input');
41Boom.wrap(error, 400);
42```
43
44### `create(statusCode, [message], [data])`
45
46Generates 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
52var error = Boom.create(400, 'Bad request', { timestamp: Date.now() });
53```
54
55## HTTP 4xx Errors
56
57### `Boom.badRequest([message], [data])`
58
59Returns a 400 Bad Request error where:
60- `message` - optional message.
61- `data` - optional additional error data.
62
63```js
64Boom.badRequest('invalid query');
65```
66
67Generates 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
79Returns 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
86If either `scheme` or `attributes` are set, the resultant `Boom` object will have the 'WWW-Authenticate' header set for the response.
87
88```js
89Boom.unauthorized('invalid password');
90```
91
92Generates 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
104Boom.unauthorized('invalid password', 'sample');
105```
106
107Generates 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
121Boom.unauthorized('invalid password', 'sample', { ttl: 0, cache: null, foo: 'bar' });
122```
123
124Generates 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
139Returns a 403 Forbidden error where:
140- `message` - optional message.
141- `data` - optional additional error data.
142
143```js
144Boom.forbidden('try again some time');
145```
146
147Generates 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
159Returns a 404 Not Found error where:
160- `message` - optional message.
161- `data` - optional additional error data.
162
163```js
164Boom.notFound('missing');
165```
166
167Generates 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
179Returns a 405 Method Not Allowed error where:
180- `message` - optional message.
181- `data` - optional additional error data.
182
183```js
184Boom.methodNotAllowed('that method is not allowed');
185```
186
187Generates 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
199Returns a 406 Not Acceptable error where:
200- `message` - optional message.
201- `data` - optional additional error data.
202
203```js
204Boom.notAcceptable('unacceptable');
205```
206
207Generates 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
219Returns a 407 Proxy Authentication Required error where:
220- `message` - optional message.
221- `data` - optional additional error data.
222
223```js
224Boom.proxyAuthRequired('auth missing');
225```
226
227Generates 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
239Returns a 408 Request Time-out error where:
240- `message` - optional message.
241- `data` - optional additional error data.
242
243```js
244Boom.clientTimeout('timed out');
245```
246
247Generates 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
259Returns a 409 Conflict error where:
260- `message` - optional message.
261- `data` - optional additional error data.
262
263```js
264Boom.conflict('there was a conflict');
265```
266
267Generates 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
279Returns a 410 Gone error where:
280- `message` - optional message.
281- `data` - optional additional error data.
282
283```js
284Boom.resourceGone('it is gone');
285```
286
287Generates 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
299Returns a 411 Length Required error where:
300- `message` - optional message.
301- `data` - optional additional error data.
302
303```js
304Boom.lengthRequired('length needed');
305```
306
307Generates 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
319Returns a 412 Precondition Failed error where:
320- `message` - optional message.
321- `data` - optional additional error data.
322
323```js
324Boom.preconditionFailed();
325```
326
327Generates the following response payload:
328
329```json
330{
331 "statusCode": 412,
332 "error": "Precondition Failed"
333}
334```
335
336### `Boom.entityTooLarge([message], [data])`
337
338Returns a 413 Request Entity Too Large error where:
339- `message` - optional message.
340- `data` - optional additional error data.
341
342```js
343Boom.entityTooLarge('too big');
344```
345
346Generates 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
358Returns a 414 Request-URI Too Large error where:
359- `message` - optional message.
360- `data` - optional additional error data.
361
362```js
363Boom.uriTooLong('uri is too long');
364```
365
366Generates 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
378Returns a 415 Unsupported Media Type error where:
379- `message` - optional message.
380- `data` - optional additional error data.
381
382```js
383Boom.unsupportedMediaType('that media is not supported');
384```
385
386Generates 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
398Returns a 416 Requested Range Not Satisfiable error where:
399- `message` - optional message.
400- `data` - optional additional error data.
401
402```js
403Boom.rangeNotSatisfiable();
404```
405
406Generates 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
417Returns a 417 Expectation Failed error where:
418- `message` - optional message.
419- `data` - optional additional error data.
420
421```js
422Boom.expectationFailed('expected this to work');
423```
424
425Generates 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
437Returns a 422 Unprocessable Entity error where:
438- `message` - optional message.
439- `data` - optional additional error data.
440
441```js
442Boom.badData('your data is bad and you should feel bad');
443```
444
445Generates 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
457Returns a 429 Too Many Requests error where:
458- `message` - optional message.
459- `data` - optional additional error data.
460
461```js
462Boom.tooManyRequests('you have exceeded your request limit');
463```
464
465Generates 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
477All 500 errors hide your message from the end user. Your message is recorded in the server log.
478
479### `Boom.notImplemented([message], [data])`
480
481Returns a 501 Not Implemented error where:
482- `message` - optional message.
483- `data` - optional additional error data.
484
485```js
486Boom.notImplemented('method not implemented');
487```
488
489Generates 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
501Returns a 502 Bad Gateway error where:
502- `message` - optional message.
503- `data` - optional additional error data.
504
505```js
506Boom.badGateway('that is a bad gateway');
507```
508
509Generates 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
521Returns a 503 Service Unavailable error where:
522- `message` - optional message.
523- `data` - optional additional error data.
524
525```js
526Boom.serverTimeout('unavailable');
527```
528
529Generates 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
541Returns a 504 Gateway Time-out error where:
542- `message` - optional message.
543- `data` - optional additional error data.
544
545```js
546Boom.gatewayTimeout();
547```
548
549Generates 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
560Returns a 500 Internal Server Error error where:
561- `message` - optional message.
562- `data` - optional additional error data.
563
564```js
565Boom.badImplementation('terrible implementation');
566```
567
568Generates 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```