In the previous section, you created a basic counter app that responds to the user
pressing the Left and Right arrow keys to control the value of a counter. This tutorial will
start with that code and add error and panic handling.
A quick reminder of where we left off in the basic app:
The problem
The app you built in the previous section has an intentional error in that causes the app to panic
when the user presses the Left arrow key when the Counter is already at 0. When this happens,
the main function does not have a chance to restore the terminal state before it exits.
The application’s default panic handler runs and displays the details messed up. This is because raw
mode stops the terminal from interpreting newlines in the usual way. The shell prompt is also
rendered at the wrong place.
To recover from this, on a macOS or Linux console, run the reset command. On a Windows console you
may need to restart the console.
Setup Hooks
There are two ways that a rust application can fail. The rust book chapter on error handling explains
this in better detail.
Rust groups errors into two major categories: recoverable and unrecoverable errors. For a
recoverable error, such as a file not found error, we most likely just want to report the
problem to the user and retry the operation. Unrecoverable errors are always symptoms of bugs,
like trying to access a location beyond the end of an array, and so we want to immediately stop
the program. — https://doc.rust-lang.org/book/ch09-00-error-handling.html
One approach that makes it easy to show unhandled errors is to use the color-eyre crate to augment
the error reporting hooks. In a ratatui application that’s running on the alternate screen in raw
mode,
it’s important to restore the terminal before displaying these errors to the user.
Add the color-eyre crate
Update the main function’s return value to color_eyre::Result<()> and call the the
color_eyre::install function. We can also add an error message that helps your app user
understand what to do if restoring the terminal does fail.
Next, update the tui::init() function to replace the panic hook with one that first restores the
terminal before printing the panic information. This will ensure that both panics and unhandled
errors (i.e. any Result::Errs that bubble up to the top level of the main function) are both
displayed on the terminal correctly when the application exits.
Using color_eyre
Color eyre works by adding extra information to Results. You can add context to the errors by
calling wrap_err (defined on the color_eyre::eyre::WrapErr trait).
Update the App::run function to add some information about the update function failing and change
the return value.
Creating a recoverable error
The tutorial needs a synthetic error to show how we can handle recoverable errors. Change
handle_key_event to return a color_eyre::Result and make sure the calls to increment and
decrement calls have the ? operator to propagate the error to the caller.
Let’s add an error that occurs when the counter is above 2. Also change both methods’ return types.
Add the new error to the increment_counter method. You can use the bail! macro for this:
In the handle_events method, add some extra information about which key caused the failure and
update the return value.
Update the tests for this method to unwrap the calls to handle_key_events. This will cause the test
to fail if an error is returned.
Add tests for the panic and overflow conditions
Run the tests:
The Finished App
Putting this altogether, you should now have the following files.
Handling Panics
Experiment to see what happens when the application panics. The application has an intentional bug
where it uses u8 for the counter field, but doesn’t guard against decrementing this below 0. Run
the app and press the Left arrow key.
To get more information about where the error occurred, add RUST_BACKTRACE=full before the
command.
Handling Errors
Experiment to see what happens when the application returns an unhandled error as a result. The app
will cause this to happen when the counter increases past 2. Run the app and press the Right arrow 3
times.
To get more information about where the error occurred, add RUST_BACKTRACE=full before the
command.