Skip to content

Commit

Permalink
feat: allow boomifying any value (hapijs#291)
Browse files Browse the repository at this point in the history
Creation of a new boomifyAny that allows creating a boom error from an
"unknown" value.

Refs hapijs#291
  • Loading branch information
matthieusieben committed Dec 21, 2021
1 parent 12e025c commit dcb1e26
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ export function isBoom(obj: unknown, statusCode?: number): obj is Boom;
export function boomify<Data, Decoration>(err: Error, options?: Options<Data> & Decorate<Decoration>): Boom<Data> & Decoration;


/**
* Specifies if an error object is a valid boom object
*
* @param err - The error object to decorate
* @param options - Options object
*
* @returns A decorated boom object
*/
export function boomifyAny<Data, Decoration>(err: unknown, options?: Options<Data> & Decorate<Decoration>): Boom<Data> & Decoration;


// 4xx Errors

/**
Expand Down
12 changes: 12 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ exports.boomify = function (err, options) {
};


exports.boomifyAny = function (value, options) {

options = options || {};

const err = value instanceof Error
? value
: new exports.Boom(options.message || 'Unknown error', { statusCode: options.statusCode || 500, data: value, ctor: exports.boomifyAny });

return exports.boomify(err, options);
};


// 4xx Client Errors

exports.badRequest = function (message, data) {
Expand Down

0 comments on commit dcb1e26

Please sign in to comment.