Skip to main content

ApiResponse

Struct ApiResponse 

Source
pub struct ApiResponse<S> { /* private fields */ }
Expand description

A response that may be returned from an axum handler, with a flexible set of response types.

It implements

  • IntoResponse
  • Optionally aide::OperationOutput when the aide feature is enabled, and all responses implement OperationOutput as well.
  • Optionally [utoipa::ToResponse] when the utoipa feature is enabled, and all responses implement ToResponse as well.

Simply specify the set of response types that this handler may return as a tuple in the ApiResponse type:

  • ApiResponse<(BadRequest<String>,)>
  • ApiResponse<(BadRequest<String>, Internal<String>)>
  • ApiResponse<(BadRequest<String>, Internal<String>, Forbidden<String>)>

ApiResponse<S> implements From<T> for any T contained in the set S, allowing for easy error propagation. See ResultStatusExt for convenient methods to help with error propagation.

Be aware that IntoResponse is called whenever this type is constructed, not when it is returned by the handler. This behaviour differs from standard axum.

Normally, this type is used as Result<T, ApiResponse<S>> (or the type-alias ApiResult). It can also be used itself as a direct return type from an axum handler.

§Examples

// An very basic axum-handler. See the `examples` directory for more complete examples.
async fn handler() -> Result<(), ApiResponse<(NotFound<String>, BadRequest<String>)> { todo!() }

Implementations§

Source§

impl<S> ApiResponse<S>

Source

pub fn new<T>(wrapper: T) -> Self
where S: Contains<T>, T: StatusProvider<Inner: IntoResponse>,

Create a new ApiResponse from a status wrapper.

Source

pub fn new_unchecked(response: impl IntoResponse, code: StatusCode) -> Self

Create a new ApiResponse from a raw response and status code, without checking if it is valid

Source

pub fn into_parts(self) -> (Response, StatusCode)

Decompose the ApiResponse into its raw response and status code.

Source

pub fn into_superset<U>(self) -> ApiResponse<U>
where U: Superset<S>,

Convert this ApiResponse into a new ApiResponse with a superset of the original error types.

Trait Implementations§

Source§

impl<S> Debug for ApiResponse<S>

Source§

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

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

impl<T, S> From<BadGateway<T>> for ApiResponse<S>

Source§

fn from(err: BadGateway<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<BadRequest<T>> for ApiResponse<S>

Source§

fn from(err: BadRequest<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<Conflict<T>> for ApiResponse<S>

Source§

fn from(err: Conflict<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<ExpectationFailed<T>> for ApiResponse<S>

Source§

fn from(err: ExpectationFailed<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<FailedDependency<T>> for ApiResponse<S>

Source§

fn from(err: FailedDependency<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<Forbidden<T>> for ApiResponse<S>

Source§

fn from(err: Forbidden<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<GatewayTimeout<T>> for ApiResponse<S>

Source§

fn from(err: GatewayTimeout<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<Gone<T>> for ApiResponse<S>
where S: Contains<Gone<T>>, Gone<T>: StatusProvider<Inner: IntoResponse>,

Source§

fn from(err: Gone<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<HttpVersionNotSupported<T>> for ApiResponse<S>

Source§

fn from(err: HttpVersionNotSupported<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<ImATeapot<T>> for ApiResponse<S>

Source§

fn from(err: ImATeapot<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<InsufficientStorage<T>> for ApiResponse<S>

Source§

fn from(err: InsufficientStorage<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<Internal<T>> for ApiResponse<S>

Source§

fn from(err: Internal<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<LengthRequired<T>> for ApiResponse<S>

Source§

fn from(err: LengthRequired<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<Locked<T>> for ApiResponse<S>
where S: Contains<Locked<T>>, Locked<T>: StatusProvider<Inner: IntoResponse>,

Source§

fn from(err: Locked<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<LoopDetected<T>> for ApiResponse<S>

Source§

fn from(err: LoopDetected<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<MethodNotAllowed<T>> for ApiResponse<S>

Source§

fn from(err: MethodNotAllowed<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<MisdirectedRequest<T>> for ApiResponse<S>

Source§

fn from(err: MisdirectedRequest<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<NetworkAuthenticationRequired<T>> for ApiResponse<S>

Source§

fn from(err: NetworkAuthenticationRequired<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<NotAcceptable<T>> for ApiResponse<S>

Source§

fn from(err: NotAcceptable<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<NotExtended<T>> for ApiResponse<S>

Source§

fn from(err: NotExtended<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<NotFound<T>> for ApiResponse<S>

Source§

fn from(err: NotFound<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<NotImplemented<T>> for ApiResponse<S>

Source§

fn from(err: NotImplemented<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<PayloadTooLarge<T>> for ApiResponse<S>

Source§

fn from(err: PayloadTooLarge<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<PaymentRequired<T>> for ApiResponse<S>

Source§

fn from(err: PaymentRequired<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<PreconditionFailed<T>> for ApiResponse<S>

Source§

fn from(err: PreconditionFailed<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<PreconditionRequired<T>> for ApiResponse<S>

Source§

fn from(err: PreconditionRequired<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<ProxyAuthenticationRequired<T>> for ApiResponse<S>

Source§

fn from(err: ProxyAuthenticationRequired<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<RangeNotSatisfiable<T>> for ApiResponse<S>

Source§

fn from(err: RangeNotSatisfiable<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<RequestHeaderFieldsTooLarge<T>> for ApiResponse<S>

Source§

fn from(err: RequestHeaderFieldsTooLarge<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<RequestTimeout<T>> for ApiResponse<S>

Source§

fn from(err: RequestTimeout<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<ServiceUnavailable<T>> for ApiResponse<S>

Source§

fn from(err: ServiceUnavailable<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<TooEarly<T>> for ApiResponse<S>

Source§

fn from(err: TooEarly<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<TooManyRequests<T>> for ApiResponse<S>

Source§

fn from(err: TooManyRequests<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<Unauthorized<T>> for ApiResponse<S>

Source§

fn from(err: Unauthorized<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<UnavailableForLegalReasons<T>> for ApiResponse<S>

Source§

fn from(err: UnavailableForLegalReasons<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<UnprocessableEntity<T>> for ApiResponse<S>

Source§

fn from(err: UnprocessableEntity<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<UnsupportedMediaType<T>> for ApiResponse<S>

Source§

fn from(err: UnsupportedMediaType<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<UpgradeRequired<T>> for ApiResponse<S>

Source§

fn from(err: UpgradeRequired<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<UriTooLong<T>> for ApiResponse<S>

Source§

fn from(err: UriTooLong<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, S> From<VariantAlsoNegotiates<T>> for ApiResponse<S>

Source§

fn from(err: VariantAlsoNegotiates<T>) -> Self

Converts to this type from the input type.
Source§

impl<S> IntoResponse for ApiResponse<S>

Source§

fn into_response(self) -> Response

Create a response.
Source§

impl OperationOutput for ApiResponse<()>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1> OperationOutput for ApiResponse<(E1,)>
where E1: StatusProvider<Inner: OperationOutput>,

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2> OperationOutput for ApiResponse<(E1, E2)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3> OperationOutput for ApiResponse<(E1, E2, E3)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4> OperationOutput for ApiResponse<(E1, E2, E3, E4)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4, E5> OperationOutput for ApiResponse<(E1, E2, E3, E4, E5)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4, E5, E6> OperationOutput for ApiResponse<(E1, E2, E3, E4, E5, E6)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4, E5, E6, E7> OperationOutput for ApiResponse<(E1, E2, E3, E4, E5, E6, E7)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4, E5, E6, E7, E8> OperationOutput for ApiResponse<(E1, E2, E3, E4, E5, E6, E7, E8)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4, E5, E6, E7, E8, E9> OperationOutput for ApiResponse<(E1, E2, E3, E4, E5, E6, E7, E8, E9)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10> OperationOutput for ApiResponse<(E1, E2, E3, E4, E5, E6, E7, E8, E9, E10)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11> OperationOutput for ApiResponse<(E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12> OperationOutput for ApiResponse<(E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13> OperationOutput for ApiResponse<(E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14> OperationOutput for ApiResponse<(E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15> OperationOutput for ApiResponse<(E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more
Source§

impl<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16> OperationOutput for ApiResponse<(E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16)>

Available on crate feature aide only.
Source§

type Inner = (StatusCode, Response<Body>)

The type that is used in examples. Read more
Source§

fn operation_response( _ctx: &mut GenContext, _operation: &mut Operation, ) -> Option<Response>

Return a response documentation for this type, alternatively modify the operation if required. Read more
Source§

fn inferred_responses( ctx: &mut GenContext, operation: &mut Operation, ) -> Vec<(Option<u16>, Response)>

Inferred responses are used when the type is used as a request handler return type. Read more

Auto Trait Implementations§

§

impl<S> !RefUnwindSafe for ApiResponse<S>

§

impl<S> !Sync for ApiResponse<S>

§

impl<S> !UnwindSafe for ApiResponse<S>

§

impl<S> Freeze for ApiResponse<S>
where PhantomData<fn() -> S>: Freeze,

§

impl<S> Send for ApiResponse<S>
where PhantomData<fn() -> S>: Send,

§

impl<S> Unpin for ApiResponse<S>
where PhantomData<fn() -> S>: Unpin,

§

impl<S> UnsafeUnpin for ApiResponse<S>
where PhantomData<fn() -> S>: UnsafeUnpin,

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> Extend<()> for T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoApi for T

Source§

fn into_api<A>(self) -> UseApi<T, A>
where T: Sized,

into UseApi
Source§

impl<S1, S2> Superset<S2> for S1
where S2: Subset<S1> + ?Sized, S1: ?Sized,

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