Introduction
A crate that makes it easy to use the anyhow crate with the tauri framework as a command result.
How to use
Define a function that uses anyhow:
Then proceed to use the anyhow_tauri::TAResult<T> as a return type of your command. And that's it! Now, you can handle your errors as you would outside tauri!
Notice that you can casually use the ? operator. I've had to create a wrapper for the bail!() and ensure!() macros, since I was unable to implement proper traits for them (somebody can submit a PR tho). The TA before each type means TauriAnyhow because I don't want to use type names that could collide with other type names in your codebase. The whole crate is around +-100 lines of code. If you don't want to depend on another package, you should be able to copy-paste it to your codebase without any problems (I don't expect this crate to change that much).
Caveats
- By default, sending errors to the client is disabled in production builds. To enable them, turn on the feature
show_errs_in_release. - You have to use the
anyhow_tauri::TAResult<T>as a return type of your commands. Could be a problem for some people. - Might lack support for some
anyhowfeatures, I didn't find any while dogfooding tho!
Credit
Initially, when I asked about using anyhow with tauri in a Tauri Working Group Office Hours call, a Discord user (I cannot find them for the love of god 😅) suggested I use the following code:
;
pub type Result<T> = Result;
I've taken this code, further extended it, and added some additional features so that it's "smoother" for anyhow to work with tauri. Soooo... credit to that mysterious Discord user :/