pub struct ApiError {
pub status: StatusCode,
pub error_type: String,
pub message: String,
pub fields: Option<Vec<FieldError>>,
/* private fields */
}Expand description
Standard API error type
Provides structured error responses following a consistent JSON format.
§Example
use rustapi_core::ApiError;
use http::StatusCode;
// Create a custom error
let error = ApiError::new(StatusCode::CONFLICT, "duplicate", "Email already exists");
assert_eq!(error.status, StatusCode::CONFLICT);
assert_eq!(error.error_type, "duplicate");
// Use convenience constructors
let not_found = ApiError::not_found("User not found");
assert_eq!(not_found.status, StatusCode::NOT_FOUND);
let bad_request = ApiError::bad_request("Invalid input");
assert_eq!(bad_request.status, StatusCode::BAD_REQUEST);Fields§
§status: StatusCodeHTTP status code
error_type: StringError type identifier
message: StringHuman-readable error message
fields: Option<Vec<FieldError>>Optional field-level validation errors
Implementations§
Source§impl ApiError
impl ApiError
Sourcepub fn new(
status: StatusCode,
error_type: impl Into<String>,
message: impl Into<String>,
) -> Self
pub fn new( status: StatusCode, error_type: impl Into<String>, message: impl Into<String>, ) -> Self
Create a new API error
Sourcepub fn validation(fields: Vec<FieldError>) -> Self
pub fn validation(fields: Vec<FieldError>) -> Self
Create a validation error with field details
Sourcepub fn bad_request(message: impl Into<String>) -> Self
pub fn bad_request(message: impl Into<String>) -> Self
Create a 400 Bad Request error
Create a 401 Unauthorized error
Sourcepub fn with_internal(self, details: impl Into<String>) -> Self
pub fn with_internal(self, details: impl Into<String>) -> Self
Add internal details (for logging, hidden from response in prod)
Source§impl ApiError
impl ApiError
Sourcepub fn from_validation_error(err: ValidationError) -> Self
pub fn from_validation_error(err: ValidationError) -> Self
Create a validation error from a ValidationError
Create a 503 Service Unavailable error
Trait Implementations§
Source§impl Error for ApiError
impl Error for ApiError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
Source§impl From<ValidationError> for ApiError
impl From<ValidationError> for ApiError
Source§fn from(err: ValidationError) -> Self
fn from(err: ValidationError) -> Self
Converts to this type from the input type.
Source§impl IntoResponse for ApiError
impl IntoResponse for ApiError
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Convert self into a Response
Source§impl ResponseModifier for ApiError
impl ResponseModifier for ApiError
Source§fn update_response(op: &mut Operation)
fn update_response(op: &mut Operation)
Update the operation with response information
Auto Trait Implementations§
impl Freeze for ApiError
impl RefUnwindSafe for ApiError
impl Send for ApiError
impl Sync for ApiError
impl Unpin for ApiError
impl UnwindSafe for ApiError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> ValidateIp for Twhere
T: ToString,
impl<T> ValidateIp for Twhere
T: ToString,
Source§fn validate_ipv4(&self) -> bool
fn validate_ipv4(&self) -> bool
Validates whether the given string is an IP V4
Source§fn validate_ipv6(&self) -> bool
fn validate_ipv6(&self) -> bool
Validates whether the given string is an IP V6
Source§fn validate_ip(&self) -> bool
fn validate_ip(&self) -> bool
Validates whether the given string is an IP