@hapi/boom
repository·master·Indexed 25 days ago
https://github.com/hapijs/boomA Node.js library providing HTTP-friendly error objects. While designed for the hapi web framework, it functions as a standalone utility for any web framework. It includes factory functions for common 4xx and 5xx HTTP errors, the boomify() function to wrap standard JavaScript Errors, and isBoom() for error validation.
What's inside @hapi/boom
- @hapi/boom provides HTTP-friendly error objects. While it is part of the hapi ecosystem and designed to work seamlessly with the hapi web framework, it is a standalone module that can be used with any web framework.
Access Boom error properties and output
masterA
Boomerror object contains several useful properties for both application logic and generating HTTP responses:isBoom: Boolean indicating if the object is a Boom instance.isServer: Boolean indicating if the status code is $\ge 500$.data: Custom data object passed during creation.message: The error message string.output: An object containing the formatted response structure:output.statusCode: The HTTP status code.output.headers: An object containing HTTP headers.output.payload: The formatted response body (containingstatusCode,error, andmessage).
reformat(debug?: boolean): Returns a string representation of the error. Ifdebugis true, it does not hide the original 500 error message.
Create Boom errors using the Boom class
masterThe
Boomclass can be instantiated directly to create custom error objects. You can provide a message or an existingErrorobject.Options:
statusCode(number): The HTTP status code (defaults to 500).data(any): Additional data to attach to the error.ctor(function): The constructor function to use for stack trace filtering (defaults to the specific factory function).decorate(object): An object containing properties to be assigned to the error instance.
If an
Errorinstance is passed as the first argument,boomify()is used internally to wrap it.Create 4xx Client Error responses
masterBoom provides factory functions for common 4xx HTTP errors. Most functions accept an optional
messageOrError(string or Error) and optionaldata(custom metadata).Common 4xx functions include:
badRequest(messageOrError, data)(400)unauthorized(messageOrError)(401)forbidden(messageOrError, data)(403)notFound(messageOrError, data)(404)methodNotAllowed(messageOrError, data, allow)(405)conflict(messageOrError, data)(409)entityTooLarge(messageOrError, data)(413)tooManyRequests(messageOrError, data)(429)badData(messageOrError, data)(422)
Identify Boom error objects with isBoom()
masterUse theisBoom()function to check if an object is a valid Boom error instance. You can optionally provide astatusCodeto verify if the error matches a specific HTTP status code.Convert standard Errors to Boom with boomify()
masterTheboomify()function takes a standard JavaScriptErrorand wraps it in aBoomobject. You can use thedecorateoption to add custom properties to the resulting Boom error.Configure 401 Unauthorized with authentication schemes
masterThe
unauthorized()function can be used to provide specific details for 401 responses, such as the authentication scheme orWWW-Authenticateheaders.- With scheme and attributes:
unauthorized(message, scheme, attributes) - With WWW-Authenticate header:
unauthorized(message, wwwAuthenticateArray)
If you provide a scheme and attributes, the error will include an
isMissingproperty indicating if credentials were missing versus invalid.- With scheme and attributes:
Check if an error is a Boom error with isBoom()
masterUse
isBoom(err, statusCode)to verify if an object is a Boom error. You can optionally provide astatusCodeto check if the error matches a specific HTTP status.Returns
trueif the error is an instance ofError, has theisBoomproperty, and matches the providedstatusCode(if one was given).Create 5xx Server Error responses
masterBoom provides factory functions for common 5xx HTTP errors. These are typically used to indicate server-side failures.
Common 5xx functions include:
internal(messageOrError, data, statusCode)(500 - allows status code override)badImplementation(messageOrError, data)(500)notImplemented(messageOrError, data)(501)badGateway(messageOrError, data)(502)serverUnavailable(messageOrError, data)(503)gatewayTimeout(messageOrError, data)(504)
Generate 5xx Server Errors
masterBoom provides factory functions for common 5xx server errors.
Common 5xx Factories:
internal(message, data, statusCode)(500) — allows custom status code.notImplemented(message, data)(501)badGateway(message, data)(502)serverUnavailable(message, data)(503)gatewayTimeout(message, data)(504)badImplementation(message, data)(500) — setsisDeveloperError: true.
Note on 500 errors: By default, Boom hides the actual error message in the
output.payload.messagefor 500 errors (replacing it with 'An internal server error occurred') to prevent leaking sensitive information, unlessdebugmode is enabled during reformatting.Generate 4xx Client Errors
masterBoom provides several factory functions to generate common 4xx client errors. Most follow the pattern
function(messageOrError, data).Common 4xx Factories:
badRequest(messageOrError, data)(400)unauthorized(message, scheme, attributes)(401)paymentRequired(messageOrError, data)(402)forbidden(messageOrError, data)(403)notFound(messageOrError, data)(404)methodNotAllowed(messageOrError, data, allow)(405) —allowcan be a string or an array of strings.notAcceptable(messageOrError, data)(406)proxyAuthRequired(messageOrError, data)(407)clientTimeout(messageOrError, data)(408)conflict(messageOrError, data)(409)resourceGone(messageOrError, data)(410)lengthRequired(messageOrError, data)(411)preconditionFailed(messageOrError, data)(412)entityTooLarge(messageOrError, data)(413)uriTooLong(messageOrError, data)(414)unsupportedMediaType(messageOrError, data)(415)rangeNotSatisfiable(messageOrError, data)(416)expectationFailed(messageOrError, data)(417)teapot(messageOrError, data)(418)badData(messageOrError, data)(422)locked(messageOrError, data)(423)failedDependency(messageOrError, data)(424)tooEarly(messageOrError, data)(425)preconditionRequired(messageOrError, data)(428)tooManyRequests(messageOrError, data)(429)illegal(messageOrError, data)(451)
Wrap existing errors with boomify()
masterUse
boomify(err, options)to convert a standard JavaScriptErrorinto a Boom error. This is useful for catching generic errors and converting them into structured HTTP errors.Options:
data(any): Data to attach to the error.decorate(object): Properties to assign to the error.statusCode(number): The HTTP status code to apply.message(string): A custom message to apply.override(boolean): Iffalse, the function will return the original error if it is already a Boom error and no new status/message is provided (defaults totrue).