♻️ Remove nested try-catch
This commit is contained in:
parent
380fd1d91e
commit
7a53bfcfbe
2 changed files with 20 additions and 20 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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,23 +26,14 @@ 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);
|
||||
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}": "${value}". Could not show custom error message: ${String(error)}`);
|
||||
}
|
||||
}
|
||||
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
|
||||
outputArgs[option] = value;
|
||||
|
|
Loading…
Reference in a new issue