add into_json feature

This commit is contained in:
gengteng
2023-08-01 12:38:14 +08:00
parent b7fdcb15ff
commit 91ce221dd3
6 changed files with 52 additions and 4 deletions

View File

@@ -39,8 +39,17 @@ impl<E: IntoResponse> IntoResponse for ValidRejection<E> {
fn into_response(self) -> Response {
match self {
ValidRejection::Valid(validate_error) => {
(StatusCode::BAD_REQUEST, validate_error.to_string()).into_response()
}
#[cfg(feature = "into_json")]
match serde_json::to_string(&validate_error) {
Ok(json) => (StatusCode::BAD_REQUEST, json),
Err(error) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to serialize validation error into JSON ({validate_error}): {error}"),
),
}
#[cfg(not(feature = "into_json"))]
(StatusCode::BAD_REQUEST, validate_error.to_string())
}.into_response(),
ValidRejection::Inner(json_error) => json_error.into_response(),
}
}