From 2dc595fa99bf102132d8b89b0d7d1a78b59e079f Mon Sep 17 00:00:00 2001 From: David Alfonso Date: Fri, 17 May 2024 12:26:34 +0200 Subject: [PATCH 1/2] Optional error throw in utils functions --- src/utils-functions.ts | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/utils-functions.ts b/src/utils-functions.ts index 9a8cfdd..2c542b4 100644 --- a/src/utils-functions.ts +++ b/src/utils-functions.ts @@ -3,34 +3,42 @@ import { reasonPhraseToStatusCode, } from './utils'; +const checkResult = (result: T, message: string): T => { + if (!result) { + throw new Error(message); + } + + return result; +} + /** * 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, an error is thrown if the flag throwWhenMissing is set up to true, + * otherwise return undefined. * * @param {number|string} statusCode The HTTP status code + * @param {boolean} [throwWhenMissing=true] Flag to indicate that an error should be thrown if the status code provided 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), throwWhenMissing = true): (string) { const result = statusCodeToReasonPhrase[statusCode.toString()]; - if (!result) { - throw new Error(`Status code does not exist: ${statusCode}`); - } - return result; + + return throwWhenMissing ? checkResult(result, `Status code does not exist: ${statusCode}`) : result; } /** * 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, an error is thrown if the flag throwWhenMissing is set up to true, + * otherwise return undefined. * * @param {string} reasonPhrase The HTTP reason phrase (e.g. "Bad Request", "OK") + * @param {boolean} [throwWhenMissing=true] Flag to indicate that an error should be thrown if the reason phrase is missing * @returns {string} The associated status code * */ -export function getStatusCode(reasonPhrase: string): (number) { +export function getStatusCode(reasonPhrase: string, throwWhenMissing = true): (number) { const result = reasonPhraseToStatusCode[reasonPhrase]; - if (!result) { - throw new Error(`Reason phrase does not exist: ${reasonPhrase}`); - } - return result; + + return throwWhenMissing ? checkResult(result, `Reason phrase does not exist: ${reasonPhrase}`) : result; } /** From b92017c65ed31efde5aa27f474b686b1bbb4d305 Mon Sep 17 00:00:00 2001 From: David Alfonso Date: Fri, 17 May 2024 12:58:37 +0200 Subject: [PATCH 2/2] Change optional param to accept a fallback function --- src/utils-functions.ts | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/utils-functions.ts b/src/utils-functions.ts index 2c542b4..40cee64 100644 --- a/src/utils-functions.ts +++ b/src/utils-functions.ts @@ -3,42 +3,46 @@ import { reasonPhraseToStatusCode, } from './utils'; -const checkResult = (result: T, message: string): T => { - if (!result) { - throw new Error(message); +const getDefaultFallback = (message: string) => (param?: T): any => { + if (!param) { + throw new Error(`${message} ${param}`); } - return result; + 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 flag throwWhenMissing is set up to true, - * otherwise return undefined. + * 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 {boolean} [throwWhenMissing=true] Flag to indicate that an error should be thrown if the status code provided is missing + * @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), throwWhenMissing = true): (string) { +export function getReasonPhrase(statusCode: (number | string), fallback = defaultReasonPhraseFallback): (string) { const result = statusCodeToReasonPhrase[statusCode.toString()]; - return throwWhenMissing ? checkResult(result, `Status code does not exist: ${statusCode}`) : result; + return result ? result : fallback(statusCode); } /** * Returns the status code for the given reason phrase. - * If the given reason phrase does not exist, an error is thrown if the flag throwWhenMissing is set up to true, - * otherwise return undefined. + * 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 {boolean} [throwWhenMissing=true] Flag to indicate that an error should be thrown if the reason phrase is missing + * @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, throwWhenMissing = true): (number) { +export function getStatusCode(reasonPhrase: string, fallback = defaultStatusCodeFallback): (number) { const result = reasonPhraseToStatusCode[reasonPhrase]; - return throwWhenMissing ? checkResult(result, `Reason phrase does not exist: ${reasonPhrase}`) : result; + return result ? result : fallback(reasonPhrase); } /**