pub struct ApiError { /* private fields */ }Expand description
A safe public API error rendered in Baukit’s standard JSON envelope.
The public code and message must be stable and safe for clients. Attach
internal diagnostic errors with ApiError::internal; causes are logged at
the request span and are never serialized.
Implementations§
Source§impl ApiError
impl ApiError
Sourcepub fn new(
status: StatusCode,
code: impl Into<String>,
message: impl Into<String>,
) -> Self
pub fn new( status: StatusCode, code: impl Into<String>, message: impl Into<String>, ) -> Self
Creates a public API error.
code should be a stable snake_case identifier and message must not
contain internal or sensitive information.
Sourcepub fn with_details(self, details: BTreeMap<String, Value>) -> Self
pub fn with_details(self, details: BTreeMap<String, Value>) -> Self
Replaces the structured details object.
Sourcepub fn with_header(self, name: HeaderName, value: HeaderValue) -> Self
pub fn with_header(self, name: HeaderName, value: HeaderValue) -> Self
Adds a header to the error response.
A later call with the same name replaces the previous value. The
request middleware always supplies the final X-Request-Id value.
Sourcepub fn with_retry_after(self, seconds: u64) -> Self
pub fn with_retry_after(self, seconds: u64) -> Self
Adds a Retry-After response header using delta seconds.
Sourcepub fn bad_request(message: impl Into<String>) -> Self
pub fn bad_request(message: impl Into<String>) -> Self
Returns a 400 bad_request error with a safe message.
Sourcepub fn validation(details: BTreeMap<String, Value>) -> Self
pub fn validation(details: BTreeMap<String, Value>) -> Self
Returns a 400 validation_failed error with structured field details.
Sourcepub fn validation_field(
field: impl Into<String>,
message: impl Into<String>,
) -> Self
pub fn validation_field( field: impl Into<String>, message: impl Into<String>, ) -> Self
Returns a validation error for one DTO field.
This is the concise form of ApiError::validation for the common case
where a product has one field-level semantic or bounds error.
Sourcepub fn validation_fields<K, M, I>(fields: I) -> Self
pub fn validation_fields<K, M, I>(fields: I) -> Self
Returns a validation error containing multiple DTO field messages.
Field names are sorted in the serialized details object. If an iterator contains the same field more than once, the last message wins.
Returns a 401 unauthorized error with a safe message.
Sourcepub fn unauthenticated() -> Self
pub fn unauthenticated() -> Self
Returns the standard 401 unauthenticated authentication error.
Sourcepub fn forbidden(message: impl Into<String>) -> Self
pub fn forbidden(message: impl Into<String>) -> Self
Returns a 403 forbidden error with a safe message.
Sourcepub fn permission_denied() -> Self
pub fn permission_denied() -> Self
Returns the standard 403 permission_denied authorization error.
Sourcepub fn not_found(message: impl Into<String>) -> Self
pub fn not_found(message: impl Into<String>) -> Self
Returns a 404 not_found error with a safe message.
Sourcepub fn conflict(message: impl Into<String>) -> Self
pub fn conflict(message: impl Into<String>) -> Self
Returns a 409 conflict error with a safe message.
Sourcepub fn method_not_allowed() -> Self
pub fn method_not_allowed() -> Self
Returns a 405 method_not_allowed error with a stable safe message.
Sourcepub fn rate_limited() -> Self
pub fn rate_limited() -> Self
Returns the standard 429 rate_limited error.
Sourcepub fn internal(error: impl StdError + Send + Sync + 'static) -> Self
pub fn internal(error: impl StdError + Send + Sync + 'static) -> Self
Returns an opaque 500 internal error and retains its cause for logging.
Sourcepub const fn status(&self) -> StatusCode
pub const fn status(&self) -> StatusCode
Returns this error’s HTTP status.
Trait Implementations§
Source§impl Error for ApiError
impl Error for ApiError
Source§fn source(&self) -> Option<&(dyn StdError + 'static)>
fn source(&self) -> Option<&(dyn StdError + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()