HTTP-friendly error objects
Lead Maintainer: Eran Hammer
- Boom
reformat(debug)
- Helper Methods
- HTTP 4xx Errors
Boom.badRequest([message], [data])
Boom.unauthorized([message], [scheme], [attributes])
Boom.paymentRequired([message], [data])
Boom.forbidden([message], [data])
Boom.notFound([message], [data])
Boom.methodNotAllowed([message], [data], [allow])
Boom.notAcceptable([message], [data])
Boom.proxyAuthRequired([message], [data])
Boom.clientTimeout([message], [data])
Boom.conflict([message], [data])
Boom.resourceGone([message], [data])
Boom.lengthRequired([message], [data])
Boom.preconditionFailed([message], [data])
Boom.entityTooLarge([message], [data])
Boom.uriTooLong([message], [data])
Boom.unsupportedMediaType([message], [data])
Boom.rangeNotSatisfiable([message], [data])
Boom.expectationFailed([message], [data])
Boom.teapot([message], [data])
Boom.badData([message], [data])
Boom.locked([message], [data])
Boom.failedDependency([message], [data])
Boom.preconditionRequired([message], [data])
Boom.tooManyRequests([message], [data])
Boom.illegal([message], [data])
- HTTP 5xx Errors
- F.A.Q.
Boom
boom provides a set of utilities for returning HTTP errors. Each utility returns a Boom
error response object which includes the following properties:
isBoom
- iftrue
, indicates this is aBoom
object instance. Note that this boolean should only be used if the error is an instance ofError
. If it is not certain, useBoom.isBoom()
instead.isServer
- convenience bool indicating status code >= 500.message
- the error message.typeof
- the constructor used to create the error (e.g.Boom.badRequest
).output
- the formatted response. Can be directly manipulated after object construction to return a custom error response. Allowed root keys:statusCode
- the HTTP status code (typically 4xx or 5xx).headers
- an object containing any HTTP headers where each key is a header name and value is the header content.payload
- the formatted object used as the response payload (stringified). Can be directly manipulated but any changes will be lost ifreformat()
is called. Any content allowed and by default includes the following content:statusCode
- the HTTP status code, derived fromerror.output.statusCode
.error
- the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived fromstatusCode
.message
- the error message derived fromerror.message
.
- inherited
Error
properties.
The Boom
object also supports the following method:
reformat(debug)
Rebuilds error.output
using the other object properties where:
debug
- a Boolean that, whentrue
, causes Internal Server Error messages to be left in tact. Defaults tofalse
, meaning that Internal Server Error messages are redacted.
Note that Boom
object will return true
when used with instanceof Boom
, but do not use the
Boom
prototype (they are either plain Error
or the error prototype passed in). This means
Boom
objects should only be tested using instanceof Boom
or Boom.isBoom()
but not by looking
at the prototype or contructor information. This limitation is to avoid manipulating the prototype
chain which is very slow.
Helper Methods
new Boom(message, [options])
Creates a new Boom
object using the provided message
and then calling
boomify()
to decorate the error with the Boom
properties, where:
message
- the error message. Ifmessage
is an error, it is the same as callingboomify()
directly.options
- and optional object where:statusCode
- the HTTP status code. Defaults to500
if no status code is already set.data
- additional error information (assigned toerror.data
).decorate
- an option with extra properties to set on the error object.ctor
- constructor reference used to crop the exception call stack output.- if
message
is an error object, also supports the otherboomify()
options.
boomify(err, [options])
Decorates an error with the Boom
properties where:
err
- theError
object to decorate.options
- optional object with the following optional settings:statusCode
- the HTTP status code. Defaults to500
if no status code is already set anderr
is not aBoom
object.message
- error message string. If the error already has a message, the providedmessage
is added as a prefix. Defaults to no message.decorate
- an option with extra properties to set on the error object.override
- iffalse
, theerr
provided is aBoom
object, and astatusCode
ormessage
are provided, the values are ignored. Defaults totrue
(apply the providedstatusCode
andmessage
options to the error regardless of its type,Error
orBoom
object).
var error = 'Unexpected input';Boom;
isBoom(err)
Identifies whether an error is a Boom
object. Same as calling instanceof Boom
.
HTTP 4xx Errors
Boom.badRequest([message], [data])
Returns a 400 Bad Request error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.unauthorized([message], [scheme], [attributes])
Returns a 401 Unauthorized error where:
message
- optional message.scheme
can be one of the following:- an authentication scheme name
- an array of string values. These values will be separated by ', ' and set to the 'WWW-Authenticate' header.
attributes
- an object of values to use while setting the 'WWW-Authenticate' header. This value is only used whenscheme
is a string, otherwise it is ignored. Every key/value pair will be included in the 'WWW-Authenticate' in the format of 'key="value"' as well as in the response payload under theattributes
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.null
andundefined
will be replaced with an empty string. Ifattributes
is set,message
will be used as the 'error' segment of the 'WWW-Authenticate' header. Ifmessage
is unset, the 'error' segment of the header will not be present andisMissing
will be true on the error object.
If either scheme
or attributes
are set, the resultant Boom
object will have the
'WWW-Authenticate' header set for the response.
Boom;
Generates the following response:
"payload": ,"headers"
Boom;
Generates the following response:
"payload": ,"headers"
Boom;
Generates the following response:
"payload": ,"headers"
Boom;
Generates the following response:
"payload": ,"headers"
Boom.paymentRequired([message], [data])
Returns a 402 Payment Required error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.forbidden([message], [data])
Returns a 403 Forbidden error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.notFound([message], [data])
Returns a 404 Not Found error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.methodNotAllowed([message], [data], [allow])
Returns a 405 Method Not Allowed error where:
message
- optional message.data
- optional additional error data.allow
- optional string or array of strings (to be combined and separated by ', ') which is set to the 'Allow' header.
Boom;
Generates the following response payload:
Boom.notAcceptable([message], [data])
Returns a 406 Not Acceptable error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.proxyAuthRequired([message], [data])
Returns a 407 Proxy Authentication Required error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.clientTimeout([message], [data])
Returns a 408 Request Time-out error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.conflict([message], [data])
Returns a 409 Conflict error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.resourceGone([message], [data])
Returns a 410 Gone error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.lengthRequired([message], [data])
Returns a 411 Length Required error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.preconditionFailed([message], [data])
Returns a 412 Precondition Failed error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.entityTooLarge([message], [data])
Returns a 413 Request Entity Too Large error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.uriTooLong([message], [data])
Returns a 414 Request-URI Too Large error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.unsupportedMediaType([message], [data])
Returns a 415 Unsupported Media Type error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.rangeNotSatisfiable([message], [data])
Returns a 416 Requested Range Not Satisfiable error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.expectationFailed([message], [data])
Returns a 417 Expectation Failed error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.teapot([message], [data])
Returns a 418 I'm a Teapot error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.badData([message], [data])
Returns a 422 Unprocessable Entity error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.locked([message], [data])
Returns a 423 Locked error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.failedDependency([message], [data])
Returns a 424 Failed Dependency error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.preconditionRequired([message], [data])
Returns a 428 Precondition Required error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.tooManyRequests([message], [data])
Returns a 429 Too Many Requests error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.illegal([message], [data])
Returns a 451 Unavailable For Legal Reasons error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
HTTP 5xx Errors
All 500 errors hide your message from the end user. Your message is recorded in the server log.
Boom.badImplementation([message], [data])
- (alias: internal
)
Returns a 500 Internal Server Error error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.notImplemented([message], [data])
Returns a 501 Not Implemented error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.badGateway([message], [data])
Returns a 502 Bad Gateway error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.serverUnavailable([message], [data])
Returns a 503 Service Unavailable error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
Boom.gatewayTimeout([message], [data])
Returns a 504 Gateway Time-out error where:
message
- optional message.data
- optional additional error data.
Boom;
Generates the following response payload:
F.A.Q.
Q How do I include extra information in my responses? output.payload
is missing data
, what gives?
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" section in the hapi documentation.