boom provides a set of utilities for returning HTTP errors. Each utility returns a Boom
error response object which includes the following properties:
message- the error message.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. 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.
stack- string with stack trace from where error was created.- optional
cause- the error cause, as set by constructor.
The object has additional properties from the Boom prototype:
name- string with error name. Set to'Boom'.isBoom- set totrue, indicating this is aBoomobject instance. Note that this boolean should only be tested if the error is an instance ofError. If it is not certain, useBoom.isBoom()instead.isServer- convenience boolean indicating status code >= 500.
The object also supports the following method:
Rebuilds error.output using the other object properties where:
debug- a Boolean that, whentrue, causes Internal Server Error messages to be left intact. Defaults tofalse, meaning that Internal Server Error messages are redacted.
Creates a new Boom sub-classed Error object, where:
message- the error message.options- and optional object where:statusCode- the HTTP status code. Defaults to500.cause- the error that caused the boom error.data- additional error information, assigned tothis.data.headers- an object containing any HTTP headers where each key is a header name and value is the header content.ctor- constructor reference used to crop the exception call stack output.
Creates a Boom object similar to new Boom(), except it
applies the options to the existing error when it is a Boom object, where:
err- the object to boomify, set ascausewhenerris not aBoomobject.options- optional object with the following optional settings:statusCode- the HTTP status code. Defaults to500if no status code is already set anderris not aBoomobject.message- error message string. If the error already has a message, the providedmessageis added as a prefix.override- iffalse, theerrprovided is aBoomobject, and astatusCodeormessageare provided, the values are ignored. Defaults totrue(apply the providedstatusCodeandmessageoptions to the error regardless of its type).
- it returns a
Boomobject with the boomified error
Note that new Boom() should generally be preferred in cases where the error can come from awaited logic, or has been passed around.
const error = new Error('Unexpected input');
const boomified = Boom.boomify(error, { statusCode: 400 });Identifies whether an error is a Boom object. Same as calling err instanceof Boom.Boom.
err- Error object.statusCode- optional status code.
Boom.isBoom(Boom.badRequest()); // true
Boom.isBoom(Boom.badRequest(), 400); // trueReturns a 400 Bad Request error where:
message- optional message.data- optional additional error data.
Boom.badRequest('invalid query');Generates the following response payload:
{
"statusCode": 400,
"error": "Bad Request",
"message": "invalid query"
}Returns a 401 Unauthorized error where:
message- optional message.schemecan 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 whenschemeis a string, otherwise it is ignored. Every key/value pair will be included in the 'WWW-Authenticate' in the format of 'key="value"'. Alternatively value can be a string which is used 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.nullandundefinedwill be replaced with an empty string. Ifattributesis set,messagewill be used as the 'error' segment of the 'WWW-Authenticate' header. Ifmessageis unset, the 'error' segment of the header will not be present andisMissingwill 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.unauthorized('invalid password');Generates the following response:
"payload": {
"statusCode": 401,
"error": "Unauthorized",
"message": "invalid password"
},
"headers": {}Boom.unauthorized('invalid password', 'sample');Generates the following response:
"payload": {
"statusCode": 401,
"error": "Unauthorized",
"message": "invalid password"
},
"headers": {
"WWW-Authenticate": "sample error=\"invalid password\""
}Boom.unauthorized(null, 'Negotiate', 'VGhpcyBpcyBhIHRlc3QgdG9rZW4=');Generates the following response:
"payload": {
"statusCode": 401,
"error": "Unauthorized"
},
"headers": {
"WWW-Authenticate": "Negotiate VGhpcyBpcyBhIHRlc3QgdG9rZW4="
}Boom.unauthorized('invalid password', 'sample', { ttl: 0, cache: null, foo: 'bar' });Generates the following response:
"payload": {
"statusCode": 401,
"error": "Unauthorized",
"message": "invalid password"
},
"headers": {
"WWW-Authenticate": "sample ttl=\"0\", cache=\"\", foo=\"bar\", error=\"invalid password\""
}Returns a 402 Payment Required error where:
message- optional message.data- optional additional error data.
Boom.paymentRequired('bandwidth used');Generates the following response payload:
{
"statusCode": 402,
"error": "Payment Required",
"message": "bandwidth used"
}Returns a 403 Forbidden error where:
message- optional message.data- optional additional error data.
Boom.forbidden('try again some time');Generates the following response payload:
{
"statusCode": 403,
"error": "Forbidden",
"message": "try again some time"
}Returns a 404 Not Found error where:
message- optional message.data- optional additional error data.
Boom.notFound('missing');Generates the following response payload:
{
"statusCode": 404,
"error": "Not Found",
"message": "missing"
}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.methodNotAllowed('that method is not allowed');Generates the following response payload:
{
"statusCode": 405,
"error": "Method Not Allowed",
"message": "that method is not allowed"
}Returns a 406 Not Acceptable error where:
message- optional message.data- optional additional error data.
Boom.notAcceptable('unacceptable');Generates the following response payload:
{
"statusCode": 406,
"error": "Not Acceptable",
"message": "unacceptable"
}Returns a 407 Proxy Authentication Required error where:
message- optional message.data- optional additional error data.
Boom.proxyAuthRequired('auth missing');Generates the following response payload:
{
"statusCode": 407,
"error": "Proxy Authentication Required",
"message": "auth missing"
}Returns a 408 Request Time-out error where:
message- optional message.data- optional additional error data.
Boom.clientTimeout('timed out');Generates the following response payload:
{
"statusCode": 408,
"error": "Request Time-out",
"message": "timed out"
}Returns a 409 Conflict error where:
message- optional message.data- optional additional error data.
Boom.conflict('there was a conflict');Generates the following response payload:
{
"statusCode": 409,
"error": "Conflict",
"message": "there was a conflict"
}Returns a 410 Gone error where:
message- optional message.data- optional additional error data.
Boom.resourceGone('it is gone');Generates the following response payload:
{
"statusCode": 410,
"error": "Gone",
"message": "it is gone"
}Returns a 411 Length Required error where:
message- optional message.data- optional additional error data.
Boom.lengthRequired('length needed');Generates the following response payload:
{
"statusCode": 411,
"error": "Length Required",
"message": "length needed"
}Returns a 412 Precondition Failed error where:
message- optional message.data- optional additional error data.
Boom.preconditionFailed();Generates the following response payload:
{
"statusCode": 412,
"error": "Precondition Failed"
}Returns a 413 Request Entity Too Large error where:
message- optional message.data- optional additional error data.
Boom.entityTooLarge('too big');Generates the following response payload:
{
"statusCode": 413,
"error": "Request Entity Too Large",
"message": "too big"
}Returns a 414 Request-URI Too Large error where:
message- optional message.data- optional additional error data.
Boom.uriTooLong('uri is too long');Generates the following response payload:
{
"statusCode": 414,
"error": "Request-URI Too Large",
"message": "uri is too long"
}Returns a 415 Unsupported Media Type error where:
message- optional message.data- optional additional error data.
Boom.unsupportedMediaType('that media is not supported');Generates the following response payload:
{
"statusCode": 415,
"error": "Unsupported Media Type",
"message": "that media is not supported"
}Returns a 416 Requested Range Not Satisfiable error where:
message- optional message.data- optional additional error data.
Boom.rangeNotSatisfiable();Generates the following response payload:
{
"statusCode": 416,
"error": "Requested Range Not Satisfiable"
}Returns a 417 Expectation Failed error where:
message- optional message.data- optional additional error data.
Boom.expectationFailed('expected this to work');Generates the following response payload:
{
"statusCode": 417,
"error": "Expectation Failed",
"message": "expected this to work"
}Returns a 418 I'm a Teapot error where:
message- optional message.data- optional additional error data.
Boom.teapot('sorry, no coffee...');Generates the following response payload:
{
"statusCode": 418,
"error": "I'm a Teapot",
"message": "Sorry, no coffee..."
}Returns a 422 Unprocessable Entity error where:
message- optional message.data- optional additional error data.
Boom.badData('your data is bad and you should feel bad');Generates the following response payload:
{
"statusCode": 422,
"error": "Unprocessable Entity",
"message": "your data is bad and you should feel bad"
}Returns a 423 Locked error where:
message- optional message.data- optional additional error data.
Boom.locked('this resource has been locked');Generates the following response payload:
{
"statusCode": 423,
"error": "Locked",
"message": "this resource has been locked"
}Returns a 424 Failed Dependency error where:
message- optional message.data- optional additional error data.
Boom.failedDependency('an external resource failed');Generates the following response payload:
{
"statusCode": 424,
"error": "Failed Dependency",
"message": "an external resource failed"
}Returns a 425 Too Early error where:
message- optional message.data- optional additional error data.
Boom.tooEarly('the server is unwilling to risk processing the request');Generates the following response payload:
{
"statusCode": 425,
"error": "Too Early",
"message": "the server is unwilling to risk processing the request"
}Returns a 428 Precondition Required error where:
message- optional message.data- optional additional error data.
Boom.preconditionRequired('you must supply an If-Match header');Generates the following response payload:
{
"statusCode": 428,
"error": "Precondition Required",
"message": "you must supply an If-Match header"
}Returns a 429 Too Many Requests error where:
message- optional message.data- optional additional error data.
Boom.tooManyRequests('you have exceeded your request limit');Generates the following response payload:
{
"statusCode": 429,
"error": "Too Many Requests",
"message": "you have exceeded your request limit"
}Returns a 451 Unavailable For Legal Reasons error where:
message- optional message.data- optional additional error data.
Boom.illegal('you are not permitted to view this resource for legal reasons');Generates the following response payload:
{
"statusCode": 451,
"error": "Unavailable For Legal Reasons",
"message": "you are not permitted to view this resource for legal reasons"
}All 500 errors hide your message from the end user.
Returns a 500 Internal Server Error error where:
message- optional message.data- optional additional error data. Used ascausewhen when anError.
Boom.badImplementation('terrible implementation');Generates the following response payload:
{
"statusCode": 500,
"error": "Internal Server Error",
"message": "An internal server error occurred"
}Returns a 501 Not Implemented error where:
message- optional message.data- optional additional error data. Used ascausewhen when anError.
Boom.notImplemented('method not implemented');Generates the following response payload:
{
"statusCode": 501,
"error": "Not Implemented",
"message": "method not implemented"
}Returns a 502 Bad Gateway error where:
message- optional message.data- optional additional error data. Used ascausewhen when anError.
Boom.badGateway('that is a bad gateway');Generates the following response payload:
{
"statusCode": 502,
"error": "Bad Gateway",
"message": "that is a bad gateway"
}Returns a 503 Service Unavailable error where:
message- optional message.data- optional additional error data. Used ascausewhen when anError.
Boom.serverUnavailable('unavailable');Generates the following response payload:
{
"statusCode": 503,
"error": "Service Unavailable",
"message": "unavailable"
}Returns a 504 Gateway Time-out error where:
message- optional message.data- optional additional error data. Used ascausewhen when anError.
Boom.gatewayTimeout();Generates the following response payload:
{
"statusCode": 504,
"error": "Gateway Time-out"
}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.