-
-
Notifications
You must be signed in to change notification settings - Fork 632
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: add an anyhow
-like Result
type for easier error handling
#1228
Conversation
This can be extended with some additional implications for server function error handling. Before PR
After PR
|
To do: adapt additional server function examples to use the try operator. |
This looks really nice and will simplify user code a lot 👍 Of course, that doesn't give all the full original Display an Debug behavior, but at least you could see the full error stack on the client side. |
So, this is an interesting idea and would certainly be good for developer experience when debugging, but I worry that it might raise some significant security concerns: remember that all of this information would be serialized to JSON and sent from the server to the browser, meaning a malicious actor could have access to a significant amount of information about how your app is structured. |
The
Result<T>
type provided by a library likeanyhow
is extremely useful for converting errors of different types into a single type using the?
operator:However,
anyhow::Result
can't be directly rendered into theview
of a Leptos component, becauseanyhow::Error
doesn't implementstd::error::Error
. (In fact, it can't, because itimpl<T> From<T> for anyhow::Error where T: std::errror::Error // etc.
would mean that anError
implementation forError
conflicts with the defaultFrom<T> for T
...)I initially tried using
anyhow::Result
internally, but this caused issues becauseanyhow::Error
is notClone
, causing lifetime troubles where trying to simply render something like aResource<(), Result<T>>
into the DOM. (Because this forcesResource::with
rather thanResource::read
, which means a reference, which means a lifetime that's not'static
...)However, we can get around this by doing something very similar to what
anyhow
does: providing aleptos::error::Error
type that simply wraps anArc<dyn Error + Send + Sync>
. Now,leptos_dom
can render anything that it could render before (anyResult<T, E> where E: Error + Send + Sync
), because those are allInto<Error>
), but it can also do the "convert any kind of error into a single type" trick which makesanyhow::Result
so convenient.