♻️ Remove nested try-catch

This commit is contained in:
C-3PO 2018-10-25 07:23:36 +02:00
parent 380fd1d91e
commit 7a53bfcfbe
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
2 changed files with 20 additions and 20 deletions

View file

@ -8,8 +8,8 @@ export default interface IOption {
short?: string; short?: string;
/** A human readable description of this option, for display in the usage message. Not displayed if `undefined` or an empty string. */ /** A human readable description of this option, for display in the usage message. Not displayed if `undefined` or an empty string. */
description?: 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; 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. */ /** 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; message?: (value: string) => (string | undefined);
} }

View file

@ -1,11 +1,20 @@
import IOption from './interfaces/IOption'; import IOption from './interfaces/IOption';
import IState from './interfaces/IState'; 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({ export default function verifyAndStoreOptionValue({
option, option,
value, value,
verify, verify = () => true,
message, message = () => '',
outputArgs, outputArgs,
requiredOptions, requiredOptions,
}: { }: {
@ -17,23 +26,14 @@ export default function verifyAndStoreOptionValue({
requiredOptions: IState['requiredOptions'], requiredOptions: IState['requiredOptions'],
}) { }) {
//Verify value for correctness //Verify value for correctness
try { const verifyValue = execWithCustomError(verify.bind(null, value), (error) => `Invalid value set for option "${option}", the validation of "${value}" failed: ${String(error)}`);
if (verify !== undefined && !verify(value)) { if (verifyValue !== true) {
if (message !== undefined) { const customMessage = execWithCustomError(message.bind(null, value), (error) => `Invalid value set for option "${option}": "${value}". Could not create custom error message: ${String(error)}`);
try {
const customMessage = message(value);
if (customMessage !== undefined && customMessage !== '') { if (customMessage !== undefined && customMessage !== '') {
throw new Error(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}".`); throw new Error(`Invalid value set for option "${option}": "${value}".`);
} }
} catch (error) {
throw new Error(`Invalid value set for option "${option}", the validation of "${value}" failed: ${String(error)}`);
}
//Remember the value, and if it is a required argument, mark that we have encountered it //Remember the value, and if it is a required argument, mark that we have encountered it
outputArgs[option] = value; outputArgs[option] = value;