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 management #396

Merged
merged 6 commits into from
May 6, 2023
Merged

Improve error management #396

merged 6 commits into from
May 6, 2023

Conversation

matei-radu
Copy link
Collaborator

This PR is a proposal for improving error management and how such errors are displayed to the user. It addresses #17.

There are two major steps involved, plus some small UX changes for consistency.

1. Streamline how errors are surfaced and managed

Necessary changes are made so that all unrecoverable errors eventually bubble up src/index.ts because it's easier to handle errors in one place. This means that all existing process.exit calls in other files, like in src/api.ts and src/commands/team.ts, are removed.

In other words, all commands can throw errors and they are captured in src/index.ts.

All errors are handled with the newly added handleCommandError function. This function will interpret the errors to pick an appropriate error message to display and an error code to use with process.exit. To make this easier, ApplicationError is created (next step).

2. Unified error class ApplicationError

ApplicationError is an extension of Error and is designed to be used for unrecoverable errors. It is used to convey the nature of the error and have extra contextual info (for certain types of errors).

Small UX changes

Don't use cfonts until all data is available

Seeing a giant piece of text is noisy when in fact the application encounters an error, it is not useful. It might even initially give the illusion that "it's working". So, cfonts is now used only if all the API data has been fetched correctly.

Before
Screenshot from 2023-05-06 11-04-43

After
Screenshot from 2023-05-06 11-41-50

Display an error spinner even when no API key is available.

Previously, the spinner would not be displayed at all if there is no API key available, even if though the application is in the data fetching phase.

Things that need improving before merging

Most of the error messages of formatErrorForPrinting probably need refining, rephrasing, and spellchecking as for now they are sort of placeholders. Feedback is much appreciated.

This commit refactors how errors are handled and communicated to the
user.

First, it makes the necessary changes so that all unrecoverable errors
eventually bubble up `src/index.ts`. It's easier to handle errors in one
place.

Second, the commit adds the `handleCommandError` and `ApplicationError`
utilities. `handleCommandError` is what is used in `src/index.ts` to
capture, interpret and display errors.
In case of errors, displaying a huge block of text is not useful. So,
this commit moves the various `cfont.say` statements after all required
API calls, so that fonts will be displayed only if the app is sure to
have all the data it needs.
Previously, the spinner would not be displayed at all if there is no API
key available, even if the application is in the data fetching phase.

This commit moves the spinner creation at the top of `callApi` so that
it can be set to display error if the API key is missing.
@matei-radu matei-radu requested a review from acifani May 6, 2023 09:48
Copy link
Owner

@acifani acifani left a comment

Choose a reason for hiding this comment

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

Great stuff ❤️

src/api.ts Show resolved Hide resolved
src/cache/index.ts Show resolved Hide resolved
src/utils/errors.ts Outdated Show resolved Hide resolved
src/utils/errors.ts Outdated Show resolved Hide resolved
Copy link
Owner

@acifani acifani left a comment

Choose a reason for hiding this comment

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

Great stuff ❤️

matei-radu and others added 2 commits May 6, 2023 14:45
Co-authored-by: Alessandro Cifani <alessandro.cifani@gmail.com>
The application can work without caching, so there is no need to have an
`ErrorCode.CACHE_WRITE` case.
@acifani acifani merged commit dfd4346 into main May 6, 2023
6 checks passed
@acifani acifani deleted the error-management branch May 6, 2023 14:35
@acifani acifani mentioned this pull request May 6, 2023
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.

None yet

2 participants