Skip to main content

Error

Struct Error 

Source
pub struct Error { /* private fields */ }
Expand description

The primary error type for the modo framework.

Error carries:

  • an HTTP StatusCode that will be used as the response status,
  • a human-readable message string,
  • an optional structured details payload (arbitrary JSON),
  • an optional boxed source error for causal chaining,
  • an optional static error_code string that survives the response pipeline,
  • a lagged flag used by the SSE broadcaster to signal that a subscriber dropped messages.

§Conversion to HTTP response

Calling into_response() produces a JSON body:

{ "error": { "status": 404, "message": "user not found" } }

If with_details was called, a "details" field is included. A copy of the error (without source) is also stored in response extensions so middleware can inspect it after the fact.

§Clone behaviour

Cloning an Error drops the source field because Box<dyn Error> is not Clone. The error_code, details, and all other fields are preserved.

Implementations§

Source§

impl Error

Source

pub fn new(status: StatusCode, message: impl Into<String>) -> Self

Create a new error with the given HTTP status code and message.

Source

pub fn with_source( status: StatusCode, message: impl Into<String>, source: impl Error + Send + Sync + 'static, ) -> Self

Create a new error with a status code, message, and a boxed source error.

Use chain instead when constructing errors with the builder pattern.

Source

pub fn status(&self) -> StatusCode

Returns the HTTP status code of this error.

Source

pub fn message(&self) -> &str

Returns the human-readable error message.

Source

pub fn details(&self) -> Option<&Value>

Returns the optional structured details payload.

Source

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

Attach a structured JSON details payload (builder-style).

Source

pub fn chain(self, source: impl Error + Send + Sync + 'static) -> Self

Attach a source error (builder-style).

Source

pub fn with_code(self, code: &'static str) -> Self

Attach a static error code to preserve error identity through the response pipeline.

The error code is included in the copy stored in response extensions and can be retrieved after into_response() via Error::error_code.

Source

pub fn error_code(&self) -> Option<&str>

Returns the error code, if one was set.

Source

pub fn source_as<T: Error + 'static>(&self) -> Option<&T>

Downcast the source error to a concrete type.

Returns None if no source is set or if the source is not of type T.

Source

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

Create a 400 Bad Request error.

Source

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

Create a 401 Unauthorized error.

Source

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

Create a 403 Forbidden error.

Source

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

Create a 404 Not Found error.

Source

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

Create a 409 Conflict error.

Source

pub fn payload_too_large(msg: impl Into<String>) -> Self

Create a 413 Payload Too Large error.

Source

pub fn unprocessable_entity(msg: impl Into<String>) -> Self

Create a 422 Unprocessable Entity error.

Source

pub fn too_many_requests(msg: impl Into<String>) -> Self

Create a 429 Too Many Requests error.

Source

pub fn internal(msg: impl Into<String>) -> Self

Create a 500 Internal Server Error.

Source

pub fn bad_gateway(msg: impl Into<String>) -> Self

Create a 502 Bad Gateway error.

Source

pub fn gateway_timeout(msg: impl Into<String>) -> Self

Create a 504 Gateway Timeout error.

Source

pub fn lagged(skipped: u64) -> Self

Create an error indicating a broadcast subscriber lagged behind.

The resulting error has a 500 Internal Server Error status and is_lagged returns true. skipped is the number of messages that were dropped.

Source

pub fn is_lagged(&self) -> bool

Returns true if this error represents a broadcast lag.

Trait Implementations§

Source§

impl Clone for Error

Clones the error, dropping the source field (which is not Clone).

All other fields — status, message, error_code, details, and lagged — are preserved.

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Error

Source§

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

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

impl Display for Error

Source§

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

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

impl Error for Error

Source§

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

👎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<Error> for Error

Converts a std::io::Error into a 500 Internal Server Error.

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Error

Converts a serde_json::Error into a 400 Bad Request.

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Error

Converts a serde_yaml_ng::Error into a 500 Internal Server Error.

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Error

Converts libsql::Error into modo::Error with appropriate HTTP status codes:

  • Unique/primary-key constraint violations become 409 Conflict.
  • Foreign-key violations become 400 Bad Request.
  • QueryReturnedNoRows becomes 404 Not Found.
  • Everything else becomes 500 Internal Server Error.
Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<HttpError> for Error

Converts an HttpError into an Error using its canonical status code and message.

Source§

fn from(http_err: HttpError) -> Self

Converts to this type from the input type.
Source§

impl From<QrError> for Error

Source§

fn from(err: QrError) -> Self

Converts to this type from the input type.
Source§

impl From<ValidationError> for Error

Source§

fn from(ve: ValidationError) -> Self

Converts to this type from the input type.
Source§

impl IntoResponse for Error

Converts Error into an axum Response.

Produces a JSON body of the form:

{ "error": { "status": 422, "message": "validation failed" } }

If with_details was called, a "details" key is added under "error".

A copy of the error (without the source field) is stored in response extensions under the type Error so that downstream middleware can inspect it.

Source§

fn into_response(self) -> Response

Create a response.

Auto Trait Implementations§

§

impl Freeze for Error

§

impl !RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnsafeUnpin for Error

§

impl !UnwindSafe for Error

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, S> Handler<IntoResponseHandler, S> for T
where T: IntoResponse + Clone + Send + Sync + 'static,

Source§

type Future = Ready<Response<Body>>

The type of future calling this handler returns.
Source§

fn call( self, _req: Request<Body>, _state: S, ) -> <T as Handler<IntoResponseHandler, S>>::Future

Call the handler with the given request.
Source§

fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>
where L: Layer<HandlerService<Self, T, S>> + Clone, <L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,

Apply a tower::Layer to the handler. Read more
Source§

fn with_state(self, state: S) -> HandlerService<Self, T, S>

Convert the handler into a Service by providing the state
Source§

impl<H, T> HandlerWithoutStateExt<T> for H
where H: Handler<T, ()>,

Source§

fn into_service(self) -> HandlerService<H, T, ()>

Convert the handler into a Service and no state.
Source§

fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>

Convert the handler into a MakeService and no state. Read more
Source§

fn into_make_service_with_connect_info<C>( self, ) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>

Convert the handler into a MakeService which stores information about the incoming connection and has no state. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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