Open nicolas-guichard opened 4 hours ago
If/when this is implemented, we could provide an infallible conversion from ServerFnError<NoCustomError>
to ServerFnError<CustErr>
:
impl<CustErr> From<ServerFnError<NoCustomError>> for ServerFnError<CustErr> {
fn from(value: ServerFnError<NoCustomError>) -> Self {
match self {
// not needed since Rust 1.82 if NoCustomError becomes uninhabited:
// ServerFnError::<NoCustomError>::WrappedServerError(_) => unreachable!(),
ServerFnError::<NoCustomError>::Registration(s) => ServerFnError::<CustErr>::Registration(s),
ServerFnError::<NoCustomError>::Request(s) => ServerFnError::<CustErr>::Request(s),
ServerFnError::<NoCustomError>::Response(s) => ServerFnError::<CustErr>::Response(s),
ServerFnError::<NoCustomError>::ServerError(s) => ServerFnError::<CustErr>::ServerError(s),
ServerFnError::<NoCustomError>::Deserialization(s) => ServerFnError::<CustErr>::Deserialization(s),
ServerFnError::<NoCustomError>::Serialization(s) => ServerFnError::<CustErr>::Serialization(s),
ServerFnError::<NoCustomError>::Args(s) => ServerFnError::<CustErr>::Args(s),
ServerFnError::<NoCustomError>::MissingArg(s) => ServerFnError::<CustErr>::MissingArg(s),
}
}
}
Currently to enable the ?
operator, I parameterize the functions that return ServerFnError
to take the CustError
from their caller.
Could you provide an example of the problem you're trying to solve?
For instance let's say I have a helper function like this:
pub fn auth() -> Result<AuthSession, ServerFnError> {
use_context::<AuthSession>().ok_or_else(|| {
ServerFnError::ServerError("Auth session missing.".into())
})
}
I want to write this:
enum GetUsernameError {
NotLoggedIn,
}
#[server]
pub async fn get_username() -> Result<Option<User>, ServerFnError<GetUsernameError>> {
use crate::todo::ssr::auth;
let auth = auth()?; // ← this currently fails, adding the From impl above solves that, and making NoCustomError uninhabited makes it infallible
match auth.current_user {
None => Err(GetUsernameError::NotLoggedIn.into()),
Some(user) => Ok(user.name),
}
}
If you don't have anything against those additions, I'll send PRs your way.
Sure. Can't say I understand this one, but I have never used the custom errors so I am sure I will once I see a PR!
When matching over a
ServerFnError
, we always have to cover theWrappedServerError
case, even though it should not be used whenCustErr = NoCustomError
.This happens because
NoCustomError
is an emptystruct
, which can be constructed.Instead I think we should use an empty
enum
such asstd::convert::Infallible
, which would:This would however be a breaking change.