-
Notifications
You must be signed in to change notification settings - Fork 233
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
Improve error handling #157
base: main
Are you sure you want to change the base?
Conversation
998cf40
to
68b9dab
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like an improvement, but unsure on if all of these changes are necessary. It also might be good if we refactor StringError
and make concrete error types for it's uses, but that's something I need to think over.
worker-macros/src/event.rs
Outdated
quote! { | ||
::worker::Response::error(e.to_string(), 500).unwrap().into() | ||
quote! { | ||
::worker::console_log!("{}", e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably keep the 500 response.
ParseError(#[from] url::ParseError), | ||
StringError(String), | ||
SerdeJsonError(#[from] serde_json::Error), | ||
RustError(#[from] anyhow::Error), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RustError
is never used, it should probably be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not used in this project, but it lets projects building on this use anyhow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, fair. But I think we should add anyhow
as a dependency when we could just use Box<dyn std::error::Error>
.
worker/src/error.rs
Outdated
} | ||
|
||
#[inline] | ||
pub fn unwrap_abort<T, E>(o: Result<T, E>) -> T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why this needs to exist, I think just regular expect
calls would be better. I might not be understanding correctly though, why this instead of an expect
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was pulled from https://rustwasm.github.io/book/reference/code-size.html#optimizing-builds-for-code-size . But yes, I agree that expect makes more sense.
worker/src/error.rs
Outdated
|
||
impl From<Error> for worker_sys::Response { | ||
fn from(e: Error) -> Self { | ||
match e { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like it should just convert the error to a Response
then .into()
it into a sys response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I took so long to address these, notification got buried with work stuff, and I forgot to check back 😅
ParseError(#[from] url::ParseError), | ||
StringError(String), | ||
SerdeJsonError(#[from] serde_json::Error), | ||
RustError(#[from] anyhow::Error), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not used in this project, but it lets projects building on this use anyhow
worker/src/error.rs
Outdated
} | ||
|
||
#[inline] | ||
pub fn unwrap_abort<T, E>(o: Result<T, E>) -> T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was pulled from https://rustwasm.github.io/book/reference/code-size.html#optimizing-builds-for-code-size . But yes, I agree that expect makes more sense.
worker/src/error.rs
Outdated
|
||
impl From<Error> for worker_sys::Response { | ||
fn from(e: Error) -> Self { | ||
match e { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey sorry it took so long to review this, it also got lost in a sea of GitHub notifications. A few small changes and then this should be good to go! There's also a merge conflict and with a PR this small a rebase should be pretty easy.
ParseError(#[from] url::ParseError), | ||
StringError(String), | ||
SerdeJsonError(#[from] serde_json::Error), | ||
RustError(#[from] anyhow::Error), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, fair. But I think we should add anyhow
as a dependency when we could just use Box<dyn std::error::Error>
.
RustError(#[from] anyhow::Error), | ||
} | ||
|
||
impl From<Error> for Response { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should be opinionated on what an error response looks like, I think we should remove these impls. For stuff like this I think it's better for the user to have some way to turn errors into response with some structure they would like.
::worker::Response::error(e.to_string(), 500).unwrap().into() | ||
} | ||
} | ||
false => { | ||
quote! { panic!("{}", e) } | ||
quote! { e.into() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we should remove the From<Error>
for Response
, we should go back to panicking.
Improves error handling by:
anyhow
From<Error> for Response
Note: After making these changes, I get the following error when I run
wrangler dev
, which seems to suggest an issue with the bundler? Because I don't think I touched anything relevant to that error?