-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[feat] Make AsyncCommandMustReturnResult trait public so it be implemented on custom "result" types. #9797
Comments
What does your custom Result type look like? why do you need a custom Result type in the first place? it is more like you need a custom Error type which you can do. |
Thanks for the reply. My custom Result type is this:
I am far more of a rust person than a JS / TS person so I may ask another team member to comment on the need of |
As a follow up, here is an article referencing some of the issues the team wanted to avoid. https://kentcdodds.com/blog/get-a-catch-block-error-message-with-typescript I am going to experiment some today with using good ole |
Hi @amrbashir, to illustrate the issue, see below: When using
vs
So it seems specta is throwing away the E part of |
I understand that the core issue is you're trying to avoid throwing errors on JS which happens when use serde::Serialize;
#[derive(Debug, Serialize)]
enum CustomError {}
impl std::fmt::Display for CustomError {}
impl std::error::Error for CustomError {}
async fn greet_inner(name: &str) -> Result<String, CustomError> {
// here you can propagate errors normally using ?
Ok(format!("Hello, {}! You've been greeted from Rust!", name))
}
#[tauri::command]
async fn greet(name: &str) -> Result<Result<String, CustomError>, ()> {
// wrap the returned `Result` into an `Ok`, making the invoke
// call to only throw on internal tauri errors and not our errors
// so we can handle errors gracefully in JS without try/catch
Ok(greet_inner(name).await)
} I know this is a bit ugly but it is way better than exposing If you want to make this a bit better you could try writing a macro that generates these wrapper/inner functions. |
Describe the problem
We use a custom Result type for my endpoints, so that we have full control of the handling behavior and type information of the Err variant on the typescript side.
We have not been using the tauri
Manager
functionality to manage state and I wanted to change that and give it a try (instead we have been using OnceCells). As soon as I injected thetauri::State<'_, MyState>
annotation in a command signature, I started getting compiler errors that I must return aResult
from async commands. This makes sense, but I don't want to return vanillaResult
, but rather my ownCommandResult
due to the FE handling of Result types.Here is where the private trait is implemented.
tauri/core/tauri-macros/src/command/wrapper.rs
Line 192 in be95d8d
Describe the solution you'd like
I would like to be able to impl
AsyncCommandMustReturnResult
trait myself for my custom result type. At least by having this trait restriction you know that folks will be opting in for good reason and can be trusted to understand why this is the case.Alternatives considered
I discussed returning result to the FE folks and it seems there is some type erasure happening in that case and we have less control over the error handling on the TS side. It would also be a lot of work in this case as the FE is quite large.
Additional context
No response
The text was updated successfully, but these errors were encountered: