-
Notifications
You must be signed in to change notification settings - Fork 39
Open
Description
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?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels