Skip to content

Commit

Permalink
Catch all yargs validation errors in isYargsError (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecooper authored Dec 11, 2024
1 parent a520215 commit e25ef6d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 46 deletions.
82 changes: 41 additions & 41 deletions src/lib/command-helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,47 +79,6 @@ const COMMON_QUERY_OPTIONS = {
},
};

export const resolveFormat = (argv) => {
const logger = container.resolve("logger");

if (argv.json) {
logger.debug(
"--json has taken precedence over other formatting options, using JSON output",
"argv",
);
return Format.JSON;
}

if (argv.raw) {
logger.debug(
"--raw has taken precedence over other formatting options, using JSON output",
"argv",
);
return Format.JSON;
}

return argv.format;
};

/**
* Validate that the user has specified either a database or a secret.
* This check is not required for commands that can operate at a
* "root" level.
*
* @param {object} argv
* @param {string} argv.database - The database to use
* @param {string} argv.secret - The secret to use
* @param {boolean} argv.local - Whether to use a local Fauna container
*/
export const validateDatabaseOrSecret = (argv) => {
if (!argv.database && !argv.secret && !argv.local) {
throw new ValidationError(
"No database or secret specified. Please use either --database, --secret, or --local to connect to your desired Fauna database.",
);
}
return true;
};

// used for queries customers can configure
const COMMON_CONFIGURABLE_QUERY_OPTIONS = {
...COMMON_QUERY_OPTIONS,
Expand Down Expand Up @@ -182,3 +141,44 @@ export function yargsWithCommonConfigurableQueryOptions(yargs) {
export function yargsWithCommonOptions(yargs, options) {
return yargs.options({ ...options, ...COMMON_OPTIONS });
}

export const resolveFormat = (argv) => {
const logger = container.resolve("logger");

if (argv.json) {
logger.debug(
"--json has taken precedence over other formatting options, using JSON output",
"argv",
);
return Format.JSON;
}

if (argv.raw) {
logger.debug(
"--raw has taken precedence over other formatting options, using JSON output",
"argv",
);
return Format.JSON;
}

return argv.format;
};

/**
* Validate that the user has specified either a database or a secret.
* This check is not required for commands that can operate at a
* "root" level.
*
* @param {object} argv
* @param {string} argv.database - The database to use
* @param {string} argv.secret - The secret to use
* @param {boolean} argv.local - Whether to use a local Fauna container
*/
export const validateDatabaseOrSecret = (argv) => {
if (!argv.database && !argv.secret && !argv.local) {
throw new ValidationError(
"No database or secret specified. Please use either --database, --secret, or --local to connect to your desired Fauna database.",
);
}
return true;
};
27 changes: 22 additions & 5 deletions src/lib/errors.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ import { container } from "../cli.mjs";

const BUG_REPORT_MESSAGE = `If you believe this is a bug, please report this issue on GitHub: https://github.com/fauna/fauna-shell/issues`;

/*
* These are the error message prefixes that yargs throws during
* validation. To detect these errors, you can either parse the stack
* or the message. We've decided to parse the messages.
*
* Compiled from https://github.com/yargs/yargs/blob/main/lib/validation.ts
*/
const YARGS_STATIC_PREFIXES = [
"Unknown argument:",
"Unknown arguments:",
"Missing required argument:",
"Missing required arguments:",
"Unknown command:",
"Unknown commands:",
"Invalid values:",
"Not enough non-option arguments:",
"Too many non-option arguments:",
"Implications failed:",
];

/**
* An error that is thrown by commands that is not a validation error, but
* a known error state that should be communicated to the user.
Expand Down Expand Up @@ -54,13 +74,10 @@ function isYargsError(error) {
return true;
}

// Usage errors from yargs are thrown as plain old Error. The best
// you can do is check for the message.
// Does the message look like a yargs error?
if (
error.message &&
(error.message.startsWith("Unknown argument") ||
error.message.startsWith("Missing required argument") ||
error.message.startsWith("Unknown command"))
YARGS_STATIC_PREFIXES.some((prefix) => error.message.startsWith(prefix))
) {
return true;
}
Expand Down

0 comments on commit e25ef6d

Please sign in to comment.