Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling #462

Merged
merged 9 commits into from
Dec 4, 2024
Merged

Improve error handling #462

merged 9 commits into from
Dec 4, 2024

Conversation

ecooper
Copy link

@ecooper ecooper commented Dec 4, 2024

Problem

All errors are thrown and treated the same. This means validation errors, known error states, and unexpected errors get the same treatment.

Solution

This PR updates our error handling for adding capabilities for three classes of errors:

  • Validation errors (ours and yargs): Arguments or options are invalid. Print help, display the validation error.
  • Command errors: Errors we intentionally throw in commands or middleware. Print the error only.
  • Unknown errors: Errors that are not validation, yargs, or command errors. So things we haven't thrown. Tell the user it's unexpected, print the error, and display a message for bug reports.

Additionally, for debugging purposes, if cause is available on an error that will be outputted as well.

Result

Validation error/unknown argument (ignore the red background, that's my shell):
Screenshot 2024-12-04 at 10 59 59 AM

Command error/not logged in
Screenshot 2024-12-04 at 11 00 33 AM

Command error/bad query syntax
Screenshot 2024-12-04 at 11 02 16 AM

Unknown error/throw
Screenshot 2024-12-04 at 11 22 08 AM

With verbosity
Screenshot 2024-12-04 at 11 56 43 AM

Testing

The tests have been updated to reflect this new behavior.

@ecooper ecooper requested a review from a team as a code owner December 4, 2024 19:24
@ecooper
Copy link
Author

ecooper commented Dec 4, 2024

I'll update the general-cli tests shortly.

@ecooper ecooper requested a review from ptpaterson December 4, 2024 19:55
@@ -42,15 +51,44 @@ export async function run(_argvInput, _container) {
let subMessage = chalk.reset(
"Use 'fauna <command> --help' for more information about a command.",
);
let epilogue = "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts on extracting this to a helper function and typing the error object? might make it easier to recall/find how to get certain error behaviors.

edit: found the types. still think this might be worth extracting just because our runner/entrypoint is now like 90% error handler.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll file it as a follow-up. My thoughts as well. It would make testing easier to since we could test error handling/printing separate from the individual messages we actually expect.

@@ -93,7 +131,7 @@ function buildYargs(argvInput) {
})
.command("warn", false, {
handler: async () => {
process.emitWarning("this is a warning emited on the node process");
process.emitWarning("this is a warning emitted on the node process");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my poor spelling getting all beaten up between you and cleve 😔

@@ -54,12 +128,15 @@ const COMMON_QUERY_OPTIONS = {
* @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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should really have a ticket for typing argv correctly...

@@ -73,7 +77,7 @@ export const runQuery = async ({
}) => {
// Check for required arguments.
if (!query) {
throw new Error("A query is required.");
throw new CommandError("A query is required.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this not validation? the line's fuzzy, but it seems to me that validation errors are sort of all errors we can determine strictly from argv without touching fs, network, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, because hideHelp is false on ValidationError

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can switch it. The problem is that this is library code and we should never really get this far without a query. You can't write a helpful validation message (i.e. use [fql] or --input) in this context.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trying a few things like leaving the query arg missing or providing an empty string catch the error before this point. So this may be moot? If the other code is working query should always be truthy by this point.

Copy link
Contributor

@ptpaterson ptpaterson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how yet, but I would very much like to print out more info for Fauna Service errors, including error code and other information like the actual abort object for aborts and contraints for constraint errors. At least at debug level. But this looks good and I think tweaks to handling specific errors can be done in a follow up.

Co-authored-by: echo-bravo-yahoo <[email protected]>
@ecooper
Copy link
Author

ecooper commented Dec 4, 2024

I don't know how yet, but I would very much like to print out more info for Fauna Service errors, including error code and other information like the actual abort object for aborts and contraints for constraint errors. At least at debug level. But this looks good and I think tweaks to handling specific errors can be done in a follow up.

We do in query via --extra.

Comment on lines +83 to +85
if (e.cause) {
logger.fatal(e.cause?.stack, "error");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need null coalesce here

Suggested change
if (e.cause) {
logger.fatal(e.cause?.stack, "error");
}
if (e.cause) {
logger.fatal(e.cause.stack, "error");
}

or can cause.stack be undefined?

Suggested change
if (e.cause) {
logger.fatal(e.cause?.stack, "error");
}
if (e.cause?.stack) {
logger.fatal(e.cause.stack, "error");
}

@ecooper ecooper merged commit 4030666 into v3 Dec 4, 2024
4 checks passed
@ecooper ecooper deleted the v3-error-handling branch December 4, 2024 20:59
@ptpaterson
Copy link
Contributor

We do in query via --extra.
🤦Okay... I'm catching up.

@cleve-fauna cleve-fauna mentioned this pull request Dec 5, 2024
This was referenced Dec 6, 2024
@cleve-fauna cleve-fauna mentioned this pull request Dec 13, 2024
@wildemat wildemat mentioned this pull request Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants