pub struct HttpError {
pub status: StatusCode,
pub detail: Option<String>,
pub headers: Vec<(String, Vec<u8>)>,
pub debug_info: Option<DebugInfo>,
}Expand description
HTTP error that produces a response.
When debug mode is enabled, errors can include additional diagnostic
information via the debug_info field. This information is conditionally
serialized into the response when is_debug_mode_enabled() returns true.
§Production Mode (default)
{"detail": "Not Found"}§Debug Mode (when enabled)
{
"detail": "Not Found",
"debug": {
"source_file": "src/handlers/user.rs",
"source_line": 42,
"function_name": "get_user",
"route_pattern": "/users/{id}"
}
}Fields§
§status: StatusCodeStatus code.
detail: Option<String>Error detail message.
headers: Vec<(String, Vec<u8>)>Additional headers.
debug_info: Option<DebugInfo>Debug information (only included in response when debug mode is enabled).
Implementations§
Source§impl HttpError
impl HttpError
Sourcepub fn new(status: StatusCode) -> Self
pub fn new(status: StatusCode) -> Self
Create a new HTTP error.
Sourcepub fn with_detail(self, detail: impl Into<String>) -> Self
pub fn with_detail(self, detail: impl Into<String>) -> Self
Add a detail message.
Sourcepub fn with_header(
self,
name: impl Into<String>,
value: impl Into<Vec<u8>>,
) -> Self
pub fn with_header( self, name: impl Into<String>, value: impl Into<Vec<u8>>, ) -> Self
Add a header.
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()).
§Example
use fastapi_core::error::{HttpError, DebugInfo};
let error = HttpError::not_found()
.with_detail("User not found")
.with_debug_info(DebugInfo::new()
.with_source_location("src/handlers/user.rs", 42, "get_user")
.with_route_pattern("/users/{id}"));Sourcepub fn with_debug_location(self, function_name: impl Into<String>) -> Self
pub fn with_debug_location(self, function_name: impl Into<String>) -> Self
Add debug information at the current source location.
This is a convenience method that captures the current file and line.
Note: This captures the location where this method is called, not
where the error originated. For accurate source tracking, use
with_debug_info with a DebugInfo created via the debug_location! macro.
Sourcepub fn bad_request() -> Self
pub fn bad_request() -> Self
Create a 400 Bad Request error.
Create a 401 Unauthorized error.
Sourcepub fn payload_too_large() -> Self
pub fn payload_too_large() -> Self
Create a 413 Payload Too Large error.
Sourcepub fn unsupported_media_type() -> Self
pub fn unsupported_media_type() -> Self
Create a 415 Unsupported Media Type error.