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

Support catching arbitrary error types #1422

Closed
sebasmagri opened this issue Sep 2, 2020 · 1 comment
Closed

Support catching arbitrary error types #1422

sebasmagri opened this issue Sep 2, 2020 · 1 comment
Labels
duplicate This issue or pull request already exists request Request for new functionality

Comments

@sebasmagri
Copy link

Hi!

It'd be convenient to have a more idiomatic way to define error catchers by a custom error type or enum member.

For example for an application with a couple handlers defined like

fn some_handler() -> Result<Responder, CustomErrorEnum>
...
fn some_other_handler() -> Result<Responder, CustomErrorEnum>

It could be possible to have general catchers for CustomErrorEnum::Variant1 and CustomErrorEnum::Variant2 defined with

#[catch(CustomErrorEnum::Variant1]

It'd fall back to the current Err(_) handling for the error types without a catch definition.

Then, it could be possible to catch the inner error data so that responders can be easily implemented for particular error types:

#[catch(CustomErrorEnum::Variant3(some::crate::Error)]
fn catch_some_crate_error(req: &Request, err: &some::crate::Error) -> Responder {
    ...
}

Additionally, with this in place it could be easy to define some helper macros to enable something like this to easily define catchers:

#[catch_as(400, CustomErrorEnum::ValidationError)]

Finally we could explore ideas even further on error handling to enable, besides the general catchers, route level catchers that might be defined on the handler level:

fn get_foo_serde_error_catcher() -> Responder {
  ...
}

#[get("/foo")
#[route_catch(serde_json::error::Error, get_foo_serde_error_catcher)]
fn get_foo() -> Responder {
    ...
}

I think this would make it easier to separate concerns in handlers and would improve rocket ergonomics in error handling.

Please let me know how feasible this sounds and any thoughts around it.

@jebrosen
Copy link
Collaborator

jebrosen commented Sep 6, 2020

This looks related to #749: in short, we do want some sort of type-based error catching but there are some issues to work out. Therefore, I'm closing this issue in favor of discussing further there.

Please let me know how feasible this sounds and any thoughts around it.

I believe this is the first time I have seen a proposal for catching individual variants of enums instead of entire types. Since we need per-type catchers in order to implement per-variant catchers, I think this particular feature is a bit less likely / farther off.

@jebrosen jebrosen closed this as completed Sep 6, 2020
@jebrosen jebrosen added duplicate This issue or pull request already exists request Request for new functionality labels Sep 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists request Request for new functionality
Projects
None yet
Development

No branches or pull requests

2 participants