pub struct JsonApiError {
pub code: ErrorCode,
/* private fields */
}Expand description
An error on its way to becoming a JSON:API error object.
Fields§
§code: ErrorCodeImplementations§
Source§impl JsonApiError
impl JsonApiError
Sourcepub fn in_request(code: ErrorCode, ctx: &Context) -> Self
pub fn in_request(code: ErrorCode, ctx: &Context) -> Self
An error in a request’s context.
The usual constructor: every error a handler returns should carry the
correlation id, so taking the context up front is one call rather than
a .in_context(ctx) remembered at each site.
Sourcepub fn internal(ctx: &Context) -> Self
pub fn internal(ctx: &Context) -> Self
A 500 that says nothing.
The detail is dropped by ErrorCode::is_sanitized, so the cause has
to be logged separately; the correlation id is what connects the two.
Sourcepub fn invalid(
ctx: &Context,
detail: impl Into<String>,
pointer: impl Into<String>,
) -> Self
pub fn invalid( ctx: &Context, detail: impl Into<String>, pointer: impl Into<String>, ) -> Self
A 422 pointing at one member of the request document.
pub fn new(code: ErrorCode) -> Self
Sourcepub fn detail(self, detail: impl Into<String>) -> Self
pub fn detail(self, detail: impl Into<String>) -> Self
Attach client-facing detail.
Ignored for codes that must be sanitized — an internal error’s message can carry a connection string, so this cannot become a leak by accident at a call site.
Sourcepub fn attribute(self, field: &str) -> Self
pub fn attribute(self, field: &str) -> Self
Point at the offending attribute.
A dotted field name becomes a nested JSON pointer:
address.city → /data/attributes/address/city.
Sourcepub fn pointer(self, pointer: impl Into<String>) -> Self
pub fn pointer(self, pointer: impl Into<String>) -> Self
Point at any member by JSON pointer, e.g.
/data/relationships/owner/data.
Self::attribute covers the common case; this is for members
outside /data/attributes, which relationships and the document
envelope both are.
Sourcepub fn parameter(self, param: impl Into<String>) -> Self
pub fn parameter(self, param: impl Into<String>) -> Self
Point at the offending query parameter, e.g. sort.
Sourcepub fn translate(self, translation: Translation) -> Self
pub fn translate(self, translation: Translation) -> Self
Attach a translation key, so a client can render this failure in its own language.
The detail stays as it is — it is the fallback for a
client with no catalog — and the key travels beside it under the error
object’s meta.i18n. See i18n for why the server
sends a key rather than translated prose.
use ash_jsonapi::i18n::Translation;
use ash_jsonapi::{Context, ErrorCode, JsonApiError};
let err = JsonApiError::in_request(ErrorCode::InvalidAttribute, &ctx)
.detail("must be 80 characters or fewer")
.attribute("name")
.translate(Translation::new("todo.name.too_long").param("max", 80));
let meta = err.to_object().meta.unwrap();
assert_eq!(meta["i18n"]["key"], "todo.name.too_long");Unlike detail, a key is kept on a sanitized code: it
names a message this crate chose, not the underlying cause, so it
cannot leak what is_sanitized exists to withhold.
Sourcepub fn translation(&self) -> Option<&Translation>
pub fn translation(&self) -> Option<&Translation>
The translation key attached to this error, if any.
Sourcepub fn correlation_id(self, id: impl Into<String>) -> Self
pub fn correlation_id(self, id: impl Into<String>) -> Self
Set the correlation id — what makes “here is my error id” resolvable in the audit log.
Sourcepub fn in_context(self, ctx: &Context) -> Self
pub fn in_context(self, ctx: &Context) -> Self
Take the correlation id from the request’s Context.
The normal way to build an error in a handler: the id is already in the context, so it never has to be passed separately or remembered.
Sourcepub fn locale(&self) -> Option<&Locale>
pub fn locale(&self) -> Option<&Locale>
The locale this error was built in, if the request named one.
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
The HTTP status to respond with.
Sourcepub fn to_object(&self) -> ErrorObject
pub fn to_object(&self) -> ErrorObject
Render the JSON:API error object.
Trait Implementations§
Source§impl Clone for JsonApiError
impl Clone for JsonApiError
Source§impl Debug for JsonApiError
impl Debug for JsonApiError
Source§impl Display for JsonApiError
impl Display for JsonApiError
Source§impl Error for JsonApiError
impl Error for JsonApiError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl From<JsonApiError> for Errors
Available on crate feature http only.
impl From<JsonApiError> for Errors
http only.Source§fn from(error: JsonApiError) -> Self
fn from(error: JsonApiError) -> Self
Source§impl IntoResponse for JsonApiError
Available on crate feature http only.An error is a response: return err.into() from any handler.
impl IntoResponse for JsonApiError
http only.An error is a response: return err.into() from any handler.
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Auto Trait Implementations§
impl Freeze for JsonApiError
impl RefUnwindSafe for JsonApiError
impl Send for JsonApiError
impl Sync for JsonApiError
impl Unpin for JsonApiError
impl UnsafeUnpin for JsonApiError
impl UnwindSafe for JsonApiError
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, S> Handler<IntoResponseHandler, S> for T
impl<T, S> Handler<IntoResponseHandler, S> for T
Source§fn call(
self,
_req: Request<Body>,
_state: S,
) -> <T as Handler<IntoResponseHandler, S>>::Future
fn call( self, _req: Request<Body>, _state: S, ) -> <T as Handler<IntoResponseHandler, S>>::Future
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>>,
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>>,
tower::Layer to the handler. Read moreSource§fn with_state(self, state: S) -> HandlerService<Self, T, S>
fn with_state(self, state: S) -> HandlerService<Self, T, S>
Service by providing the stateSource§impl<H, T> HandlerWithoutStateExt<T> for H
impl<H, T> HandlerWithoutStateExt<T> for H
Source§fn into_service(self) -> HandlerService<H, T, ()>
fn into_service(self) -> HandlerService<H, T, ()>
Service and no state.Source§fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
MakeService and no state. Read moreSource§fn into_make_service_with_connect_info<C>(
self,
) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
fn into_make_service_with_connect_info<C>( self, ) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
tokio only.MakeService which stores information
about the incoming connection and has no state. Read more