-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allowing ErrorBoundary to work smoothly with Error #4157
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
Comments
I am more in favor of method 2 as it provide more flexibility, in case of Apps with more well-managed error, we can create an Into trait implementation that automatically use App level error, such as: #[derive(Error, Debug)]
enum AppError {
#[error("Error 1-{0}")]
SomeError(#[from] DisplayError),
}
#[derive(Error, Debug)]
enum DisplayError {
#[error("1")]
Unknown
}
impl Into<CapturedError> for DisplayError {
fn into(self) -> CapturedError {
Err::<(), _>(self)
.show(|x| {
rsx! {
{AppError::from(x).to_string()} // <- Make DisplayError into AppError before returning the display format
}
})
.unwrap_err()
}
} |
Captured error has a default from implementation for any type that implements use dioxus::prelude::*;
use thiserror::Error;
fn main() {
dioxus::launch(|| {
rsx! {
ErrorBoundary {
handle_error: |e: ErrorContext| { rsx! {
for error in e.errors() {
p { "{error}" }
}
} },
Foo {}
}
}
});
}
#[component]
fn Foo() -> Element {
Err(AppError::SomeError)?;
rsx! {}
}
#[derive(Error, Debug)]
enum AppError {
#[error("Some error has occurred")]
SomeError,
} You can also downcast errors like this: use dioxus::prelude::*;
use thiserror::Error;
fn main() {
dioxus::launch(|| {
rsx! {
ErrorBoundary {
handle_error: |e: ErrorContext| { rsx! {
for e in e.errors() {
{
if let Some(my_error) = e.downcast::<AppError>() {
rsx! { div { "App Error {my_error}" } }
} else {
rsx! { div { "{e}" } }
}
}
}
} },
Foo {}
}
}
});
}
#[component]
fn Foo() -> Element {
Err(AppError::SomeError)?;
rsx! {}
}
#[derive(Error, Debug)]
enum AppError {
#[error("Some error has occurred")]
SomeError,
} We could expose a method to get the inner |
Thank you for the quick reply!
I understand the basic implementation, it's just hard to make it "play nicely" with the original errors because everything ends up as CapturedError which limits access to the original error and therefore also limits the formatting of error.
Downcasting does not seem to get into this branch on this code
I guess it will help a great deal if an owned error can be passed as it will help with the handling in the structure Error case mention here |
Feature Request
I gave ErrorBoundary a try according to the guide. However, when I try use it with some errors, I realize that it might be a little hard to get it to play nicely with the errors. So for example:
In order to show the error message in the error, it seems that I have to add
.show(|e| rsx! {{e.to_string()}})
to everyResult
carrying the error. This is a little of a hassle and I thought there should be a better way.Implement Suggestion
Method 1
Maybe, make CaptureError capture information about the original error. (Maybe we can downcast? Didn't seem to work in my case and rather than downcast, it's better to work with the actual error)
Method 2
Allow a way for user to implement a trait that can override Into trait. For example:
The text was updated successfully, but these errors were encountered: