diff --git a/src/utils-functions.ts b/src/utils-functions.ts index 9a8cfdd..40cee64 100644 --- a/src/utils-functions.ts +++ b/src/utils-functions.ts @@ -3,34 +3,46 @@ import { reasonPhraseToStatusCode, } from './utils'; +const getDefaultFallback = (message: string) => (param?: T): any => { + if (!param) { + throw new Error(`${message} ${param}`); + } + + return param; +} + +const defaultReasonPhraseFallback = getDefaultFallback(`Status code does not exist:`); + +const defaultStatusCodeFallback = getDefaultFallback(`Reason phrase does not exist:`); + /** * Returns the reason phrase for the given status code. - * If the given status code does not exist, an error is thrown. + * If the given status code does not exist, a fallback function is called passing the status code provided, + * which by default throws an error indicating that the status code it's missing. * * @param {number|string} statusCode The HTTP status code + * @param {function} [fallback] Optional fallback function that it's called if the status code is missing * @returns {string} The associated reason phrase (e.g. "Bad Request", "OK") * */ -export function getReasonPhrase(statusCode: (number | string)): (string) { +export function getReasonPhrase(statusCode: (number | string), fallback = defaultReasonPhraseFallback): (string) { const result = statusCodeToReasonPhrase[statusCode.toString()]; - if (!result) { - throw new Error(`Status code does not exist: ${statusCode}`); - } - return result; + + return result ? result : fallback(statusCode); } /** * Returns the status code for the given reason phrase. - * If the given reason phrase does not exist, undefined is returned. + * If the given reason phrase does not exist, a fallback function is called passing the reason phrase provided, + * which by default throws an error indicating that the reason code it's missing. * * @param {string} reasonPhrase The HTTP reason phrase (e.g. "Bad Request", "OK") + * @param {function} [fallback] Optional fallback function that it's called if the reason phrase is missing * @returns {string} The associated status code * */ -export function getStatusCode(reasonPhrase: string): (number) { +export function getStatusCode(reasonPhrase: string, fallback = defaultStatusCodeFallback): (number) { const result = reasonPhraseToStatusCode[reasonPhrase]; - if (!result) { - throw new Error(`Reason phrase does not exist: ${reasonPhrase}`); - } - return result; + + return result ? result : fallback(reasonPhrase); } /**