Skip to main content

ApiError

Struct ApiError 

Source
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

Source

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.

Source

pub fn with_details(self, details: BTreeMap<String, Value>) -> Self

Replaces the structured details object.

Source

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.

Source

pub fn with_retry_after(self, seconds: u64) -> Self

Adds a Retry-After response header using delta seconds.

Source

pub fn bad_request(message: impl Into<String>) -> Self

Returns a 400 bad_request error with a safe message.

Source

pub fn validation(details: BTreeMap<String, Value>) -> Self

Returns a 400 validation_failed error with structured field details.

Source

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.

Source

pub fn validation_fields<K, M, I>(fields: I) -> Self
where K: Into<String>, M: Into<String>, I: IntoIterator<Item = (K, M)>,

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.

Source

pub fn unauthorized(message: impl Into<String>) -> Self

Returns a 401 unauthorized error with a safe message.

Source

pub fn unauthenticated() -> Self

Returns the standard 401 unauthenticated authentication error.

Source

pub fn forbidden(message: impl Into<String>) -> Self

Returns a 403 forbidden error with a safe message.

Source

pub fn permission_denied() -> Self

Returns the standard 403 permission_denied authorization error.

Source

pub fn not_found(message: impl Into<String>) -> Self

Returns a 404 not_found error with a safe message.

Source

pub fn conflict(message: impl Into<String>) -> Self

Returns a 409 conflict error with a safe message.

Source

pub fn method_not_allowed() -> Self

Returns a 405 method_not_allowed error with a stable safe message.

Source

pub fn rate_limited() -> Self

Returns the standard 429 rate_limited error.

Source

pub fn internal(error: impl StdError + Send + Sync + 'static) -> Self

Returns an opaque 500 internal error and retains its cause for logging.

Source

pub const fn status(&self) -> StatusCode

Returns this error’s HTTP status.

Source

pub fn code(&self) -> &str

Returns this error’s stable machine-readable code.

Trait Implementations§

Source§

impl Debug for ApiError

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ApiError

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for ApiError

Source§

fn source(&self) -> Option<&(dyn StdError + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<IdempotencyError> for ApiError

Source§

fn from(error: IdempotencyError) -> Self

Converts to this type from the input type.
Source§

impl From<JsonRejection> for ApiError

Source§

fn from(_rejection: JsonRejection) -> Self

Converts to this type from the input type.
Source§

impl From<PaginationError> for ApiError

Source§

fn from(error: PaginationError) -> Self

Converts to this type from the input type.
Source§

impl From<PathRejection> for ApiError

Source§

fn from(_rejection: PathRejection) -> Self

Converts to this type from the input type.
Source§

impl From<PreconditionError> for ApiError

Source§

fn from(error: PreconditionError) -> Self

Converts to this type from the input type.
Source§

impl From<QueryRejection> for ApiError

Source§

fn from(_rejection: QueryRejection) -> Self

Converts to this type from the input type.
Source§

impl From<RevisionOutOfRange> for ApiError

Source§

fn from(error: RevisionOutOfRange) -> Self

Converts to this type from the input type.
Source§

impl IntoResponse for ApiError

Source§

fn into_response(self) -> Response

Create a response.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more