-
Notifications
You must be signed in to change notification settings - Fork 4
Proposal for better error handling #61
Comments
2. Third Pary
|
3. Error status codeQuestion: How do we support different Currently, the |
4. Can't get Context data when the
|
Just found this crate (anyhow), let me try to use it for |
this is good reference too |
pub trait IntoErrorResponse: fmt::Debug + fmt::Display {
/// convert Error into error response
fn into_error_response(&self) -> Result<Response<Body>, http::Error> {
let body = Body::from(self.to_string());
Response::builder().status(self.status_code()).body(body)
}
/// status code for this error response
/// this will return Internal Server Error (500) by default
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
}
#[derive(Debug)]
pub struct ObsidianError {
inner: Box<dyn IntoErrorResponse>,
}
impl ObsidianError {
pub fn into_response(&self) -> Result<Response<Body>, http::Error> {
self.into_error_response()
}
}
impl<T: IntoErrorResponse + 'static> From<T> for ObsidianError {
fn from(err: T) -> Self {
ObsidianError {
inner: Box::new(err),
}
}
} |
1. Early return
Error
Question: How do we know what response content-type to be returned by the
Handler
if user using?
to early returnError
?Currently, the
Error
will always return String which might not be what users expecting if they are developing json API.Note: for Obsidian 0.2.x, the user only can handle this by manually write Error response instead of early return using
?
.From
To
Expected response
The suggestion section is a work-in-progress
Suggestion
1. Trait
To allow the user to control error handling globally, we can provide an
ErrorResponse
trait.2. Middleware
We can have a
EarlyReturnErrorMiddleware
to handle it:3. Annotation
The text was updated successfully, but these errors were encountered: