From 7a53bfcfbeba825fcaf89f4e52493dc8ec5f17c2 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Thu, 25 Oct 2018 07:23:36 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Remove=20nested=20try-catc?= =?UTF-8?q?h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/interfaces/IOption.ts | 6 +++--- src/verifyAndStoreOptionValue.ts | 34 ++++++++++++++++---------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/interfaces/IOption.ts b/src/interfaces/IOption.ts index 368b116..f09ada2 100644 --- a/src/interfaces/IOption.ts +++ b/src/interfaces/IOption.ts @@ -8,8 +8,8 @@ export default interface IOption { short?: string; /** A human readable description of this option, for display in the usage message. Not displayed if `undefined` or an empty string. */ description?: string; - /** A function that verifies if the given value is a valid value for this option. If set to `undefined`, any value is accepted for this option. */ + /** A function that verifies if the given value is a valid value for this option. If the function is missing, any value is accepted for this option. */ verify?: (value: string) => boolean; - /** A function returning a custom error message that is shown if the verify function returns false. If the function is `undefined`, or it returns `undefined` or an empty string, a default error message is shown instead. */ - message?: (value: string) => string; + /** A function returning a custom error message that is shown if the verify function returns false. If the function is missing, or it returns `undefined` or an empty string, a default error message is shown instead. */ + message?: (value: string) => (string | undefined); } diff --git a/src/verifyAndStoreOptionValue.ts b/src/verifyAndStoreOptionValue.ts index d009275..b69f127 100644 --- a/src/verifyAndStoreOptionValue.ts +++ b/src/verifyAndStoreOptionValue.ts @@ -1,11 +1,20 @@ import IOption from './interfaces/IOption'; import IState from './interfaces/IState'; +/** Calls the given function, and in case of an error, uses the given function to provide a custom description for the error message. */ +function execWithCustomError(func: (...args: any[]) => any, showError: (error: Error) => string) { + try { + return func(); + } catch (error) { + throw new Error(showError(error)); + } +} + export default function verifyAndStoreOptionValue({ option, value, - verify, - message, + verify = () => true, + message = () => '', outputArgs, requiredOptions, }: { @@ -17,22 +26,13 @@ export default function verifyAndStoreOptionValue({ requiredOptions: IState['requiredOptions'], }) { //Verify value for correctness - try { - if (verify !== undefined && !verify(value)) { - if (message !== undefined) { - try { - const customMessage = message(value); - if (customMessage !== undefined && customMessage !== '') { - throw new Error(customMessage); - } - } catch (error) { - throw new Error(`Invalid value set for option "${option}": "${value}". Could not show custom error message: ${String(error)}`); - } - } - throw new Error(`Invalid value set for option "${option}": "${value}".`); + const verifyValue = execWithCustomError(verify.bind(null, value), (error) => `Invalid value set for option "${option}", the validation of "${value}" failed: ${String(error)}`); + if (verifyValue !== true) { + const customMessage = execWithCustomError(message.bind(null, value), (error) => `Invalid value set for option "${option}": "${value}". Could not create custom error message: ${String(error)}`); + if (customMessage !== undefined && customMessage !== '') { + throw new Error(customMessage); } - } catch (error) { - throw new Error(`Invalid value set for option "${option}", the validation of "${value}" failed: ${String(error)}`); + throw new Error(`Invalid value set for option "${option}": "${value}".`); } //Remember the value, and if it is a required argument, mark that we have encountered it