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 31b80ef
Show file tree
Hide file tree
Showing 2 changed files with 31 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> & ({ onlyError: true } | { onlyError?: false; stringAsMessage?: boolean })): Boom<Data> & Decoration;


// 4xx Errors

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


exports.boomifyAny = function (value, options) {

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

options = options || {};

if (options.onlyError) {
const err = internals.serverError('Non-error used as error', value, 500, exports.boomifyAny);
err.isDeveloperError = true;
return err;
}

const message = (options.stringAsMessage && typeof value === 'string' && value) || options.message || 'Unknown error';

return new exports.Boom(message, { statusCode: options.statusCode || 500, data: value, ctor: exports.boomifyAny });
};


// 4xx Client Errors

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

0 comments on commit 31b80ef

Please sign in to comment.