-
-
Notifications
You must be signed in to change notification settings - Fork 691
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
User defined errors from server function #1657
Comments
To start where you ended:
From the docs:
This functions in the same way as As far as I can tell, making It should be fairly simple for you to implement an extension trait that serializes your custom error type to a string to be used in Alternately, if you have a proposal for your second approach (allowing server functions to return a Result with any error type) I'd be open to a PR. |
I likely won't have the time to write up a PR for it but if somebody else wants to pick it up, here's the way I had imagined the second approach being used: use thiserror::Error;
use serde::{Serialize, Deserialize};
#[derive(Error, Serialize, Deserialize)]
pub enum MyCustomError {
// It implements `From<ServerFnError>` so it's able to represent framework errors
#[from]
ServerError(ServerFnError),
// .. and some custom variants
}
#[server(MyServerFn, "/api")]
fn my_server_fn(arg: i32) -> Result<i32, MyCustomError> {
arg + 1
} In the pub trait ServerFn {
// ...
type Error: From<ServerFnError> + Serialize + Deserialize;
// ...
fn call_fn(
self,
cx: T
) -> Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>>>>;
// ...
} |
I was also reminded recently that this is a perfectly reasonable type: #[server]
pub async fn my_fallible_server_fn() -> Result<Result<T, MyError>, ServerFnError> in which the other result is "did the server function work" and the inner result is "what value am I returning." So |
Custom error types in server functions added in #2158. |
Is your feature request related to a problem? Please describe.
Currently, according to the documentation, server functions must return a
ServerFnError
, which contains a couple of enum variants for framework errors and then allows for user defined errors inServerFnError::ServerError
. However, that's just a string and that doesn't give much flexibility. I would rather use my own error types, e.g. ones created with thiserror.Describe the solution you'd like
I can see two possible solutions:
ServerFnError::ServerError
variant store an arbitrary (serializable) error type instead of just aString
. This, however, would requireServerFnError
to become generic.ServerFnError
generic, we could consider an alternative approach where server functions can return any user defined error type. That type would require a bound onFrom<ServerFnError>
so that framework errors can be converted into it.Additional context
I've seen
ServerFnErrorErr
but haven't fully understood what it is for. It looks pretty much the same asServerFnError
just with additional traits implemented? Why aren't those traits just implemented onServerFnError
?The text was updated successfully, but these errors were encountered: