Skip to content
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

ServerFnError<T> should impl From<E: std::error::Error> for any T #3153

Closed
nicolas-guichard opened this issue Oct 23, 2024 · 2 comments · May be fixed by #3274
Closed

ServerFnError<T> should impl From<E: std::error::Error> for any T #3153

nicolas-guichard opened this issue Oct 23, 2024 · 2 comments · May be fixed by #3274

Comments

@nicolas-guichard
Copy link

Currently, ServerFnError has a blanket From impl to convert from E, but only with the default NoCustomError type:

impl<E: std::error::Error> From<E> for ServerFnError {
    fn from(value: E) -> Self {
        ServerFnError::ServerError(value.to_string())
    }
}

This means we can't easily use ? to convert other errors into a ServerFnError<MyCustomError>.

Instead I think we should provide a blanket impl for any T:

impl<T, E: std::error::Error> From<E> for ServerFnError<T> {
    fn from(value: E) -> Self {
        ServerFnError::ServerError(value.to_string())
    }
}
@nicolas-guichard nicolas-guichard changed the title ServerFnError<T> should impl From<E> for any T ServerFnError<T> should impl From<E: std::error::Error> for any T Oct 23, 2024
@gbj
Copy link
Collaborator

gbj commented Oct 23, 2024

Feel free to make a PR.

@benwis
Copy link
Contributor

benwis commented Nov 17, 2024

That trait implementation interferes with the one in core, so Rust won't let that happen. I'm also not sure people want to convert their E into a String instead of using our WrappedError variant. Closing this as well.

@benwis benwis closed this as completed Nov 17, 2024
nicolas-guichard added a commit to nicolas-guichard/leptos that referenced this issue Nov 17, 2024
This adds 3 new traits:
- `ConvertServerFnResult` provides easy conversion from
  `Result<T, ServerFnError<SourceCustErr>>` to
  `Result<T, ServerFnError<TargetCustError>>` when `TargetCustError`
  implements `From<SourceCustErr>`

- `ConvertDefaultServerFnResult` provides easy conversion from
  `Result<T, ServerFnError<NoCustomError>` to
  `Result<T, ServerFnError<TargetCustError>>`

- `IntoServerFnResult` provides easy conversion from `Result<T, E>` to
  `Result<T, ServerFnError::ServerError>` when `E` implements
  `std::error::Error`

Fixes leptos-rs#3153 and leptos-rs#3155
nicolas-guichard added a commit to nicolas-guichard/leptos that referenced this issue Nov 17, 2024
This adds 3 new traits:
- `ConvertServerFnResult` provides easy conversion from
  `Result<T, ServerFnError<SourceCustErr>>` to
  `Result<T, ServerFnError<TargetCustError>>` when `TargetCustError`
  implements `From<SourceCustErr>`

- `ConvertDefaultServerFnResult` provides easy conversion from
  `Result<T, ServerFnError<NoCustomError>` to
  `Result<T, ServerFnError<TargetCustError>>`

- `IntoServerFnResult` provides easy conversion from `Result<T, E>` to
  `Result<T, ServerFnError::ServerError>` when `E` implements
  `std::error::Error`

Fixes leptos-rs#3153 and leptos-rs#3155
nicolas-guichard added a commit to nicolas-guichard/leptos that referenced this issue Nov 17, 2024
This adds 3 new traits:
- `ConvertServerFnResult` provides easy conversion from
  `Result<T, ServerFnError<SourceCustErr>>` to
  `Result<T, ServerFnError<TargetCustError>>` when `TargetCustError`
  implements `From<SourceCustErr>`

- `ConvertDefaultServerFnResult` provides easy conversion from
  `Result<T, ServerFnError<NoCustomError>` to
  `Result<T, ServerFnError<TargetCustError>>`

- `IntoServerFnResult` provides easy conversion from `Result<T, E>` to
  `Result<T, ServerFnError::ServerError>` when `E` implements
  `std::error::Error`

Fixes leptos-rs#3153 and leptos-rs#3155
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants