pub struct ResponseValidationError {
pub errors: Vec<ValidationError>,
pub response_content: Option<Value>,
pub summary: Option<String>,
pub debug_info: Option<DebugInfo>,
}Expand description
Response validation error for internal failures (500 Internal Server Error).
This error type is used when a response fails to serialize or validate
against the expected response model. Unlike ValidationErrors which
indicates client errors (422), this represents a bug in the server code
and returns a 500 status.
§When This Is Used
- Response fails to serialize to JSON
- Response doesn’t match the declared response_model
- Internal data transformation fails
§Security Note
Error details and the original response content are logged server-side but are NOT exposed to clients (unless debug mode is explicitly enabled). This prevents leaking internal implementation details.
§Examples
use fastapi_core::error::{ResponseValidationError, ValidationError, loc};
// Serialization failure
let error = ResponseValidationError::serialization_failed(
"failed to serialize field 'created_at': invalid date format"
);
// Response model validation failure
let error = ResponseValidationError::new()
.with_error(ValidationError::missing(loc::response_field("user_id")))
.with_response_content(serde_json::json!({"name": "Alice"}));Fields§
§errors: Vec<ValidationError>The validation errors that occurred.
response_content: Option<Value>The response content that failed validation (for logging only). This is NOT included in the response to clients.
summary: Option<String>A summary message for logging.
debug_info: Option<DebugInfo>Debug information (only included in response when debug mode is enabled).
Implementations§
Source§impl ResponseValidationError
impl ResponseValidationError
Sourcepub fn serialization_failed(message: impl Into<String>) -> Self
pub fn serialization_failed(message: impl Into<String>) -> Self
Create a serialization failure error.
Use this when response serialization to JSON fails.
Sourcepub fn model_validation_failed(message: impl Into<String>) -> Self
pub fn model_validation_failed(message: impl Into<String>) -> Self
Create a response model validation failure error.
Use this when the response doesn’t match the declared response_model.
Sourcepub fn with_error(self, error: ValidationError) -> Self
pub fn with_error(self, error: ValidationError) -> Self
Add a validation error.
Sourcepub fn with_errors(
self,
errors: impl IntoIterator<Item = ValidationError>,
) -> Self
pub fn with_errors( self, errors: impl IntoIterator<Item = ValidationError>, ) -> Self
Add multiple validation errors.
Sourcepub fn with_response_content(self, content: Value) -> Self
pub fn with_response_content(self, content: Value) -> Self
Set the response content that failed validation.
This is logged server-side but NOT exposed to clients.
Sourcepub fn with_summary(self, summary: impl Into<String>) -> Self
pub fn with_summary(self, summary: impl Into<String>) -> Self
Set a summary message for logging.
Sourcepub fn with_debug_info(self, debug_info: DebugInfo) -> Self
pub fn with_debug_info(self, debug_info: DebugInfo) -> Self
Add debug information.
This information will only be included in the response when debug mode
is enabled (via enable_debug_mode()).
Sourcepub fn iter(&self) -> impl Iterator<Item = &ValidationError>
pub fn iter(&self) -> impl Iterator<Item = &ValidationError>
Get an iterator over the errors.
Sourcepub fn to_log_string(&self) -> String
pub fn to_log_string(&self) -> String
Log the error details (for server-side logging).
This returns a formatted string suitable for logging that includes the error details and response content (if any).
Trait Implementations§
Source§impl Clone for ResponseValidationError
impl Clone for ResponseValidationError
Source§fn clone(&self) -> ResponseValidationError
fn clone(&self) -> ResponseValidationError
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more