How to custom extractors error response? #354
-
I want to customize the content of the response, how can I get the information provided by impl IntoResponse for ServerError {
type Body = Full<Bytes>;
type BodyError = Infallible;
fn into_response(self) -> Response<Self::Body> {
let (status_code, message) = match self {
Self::ValidationError(_) => {
let message = format!("Input validation error: [{}]", self).replace("\n", ", ");
tracing::debug!("{}", message);
(StatusCode::BAD_REQUEST, message)
}
Self::AxumJsonError(err) => {
let message = format!("{:?}", err);
(err.into_response().status(), message)
}
Self::ReInitializedError => (StatusCode::FORBIDDEN, self.to_string()),
_ => {
tracing::error!("Internal Server Error: [{}]", self.to_string());
(
StatusCode::INTERNAL_SERVER_ERROR,
"Internal Server Error".to_string(),
)
}
};
let payload = Json(json!({ "error": message }));
(status_code, payload).into_response()
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Sep 29, 2021
Replies: 1 comment 3 replies
-
I don't quite follow what you mean. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
Oodachi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't quite follow what you mean.
JsonRejection
is an enum so you canmatch
on each variant and handle the case. Similarly to whats shown here.