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 9df4be2
Show file tree
Hide file tree
Showing 2 changed files with 34 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
23 changes: 23 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,29 @@ exports.boomify = function (err, options) {
};


exports.boomifyAny = function (value, options) {

// Because message & statusCode are typically used for non-errors, override should not
// default to "true" when using boomifyAny with an error.
options = Object.assign({ override: false }, options);

if (value instanceof Error) {
return exports.boomify(value, options);
}

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

err.isDeveloperError = true;

if (options.decorate) {
Object.assign(err, options.decorate);
}

return err;

};


// 4xx Client Errors

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

0 comments on commit 9df4be2

Please sign in to comment.