Skip to content

Make error handling more convenient #114

Description

@Kixunil

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions