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

Make error handling more convenient #114

Open
Kixunil opened this issue Apr 18, 2018 · 0 comments
Open

Make error handling more convenient #114

Kixunil opened this issue Apr 18, 2018 · 0 comments

Comments

@Kixunil
Copy link

Kixunil commented Apr 18, 2018

It is often needed for application to perform a fallible operation in its handler. Currently one needs to manually handle failures. Maybe there's a way to make it convenient. Ideally, one would just ? on results, but we need to distinguish parsing errors from internal errors. Two approaches that come to my mind:

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum HandlingErrorKind {
    Request,
    Internal,
    // Maybe more?
}

trait HandlingError: std::fmt::Display {
    fn error_kind(&self) -> HandlingErrorKind;
}

The user would then implement HandlingError for their error type and could just ? errors.

Another approach is to use just enum instead of a trait:

enum HandlingError<R: fmt::Display, I: fmt::Display> {
    Request(R),
    Internal(I),
}

This could be used easily like this:

let user_input = serde_json::from_slice::<UserInput>(request.body()).map_err(HandlingError::Request)?;
user_input.save_to_db().map_err(HandlingError::Internal)?;

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant