pub struct ApiError { /* private fields */ }Expand description
An unsuccessful HTTP response, with the body and metadata it came with.
Reach it through ErrorKind::Api.
Implementations§
Source§impl ApiError
impl ApiError
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
The response status.
Sourcepub fn kind(&self) -> ApiErrorKind
pub fn kind(&self) -> ApiErrorKind
Which class of failure the status puts this response in.
Sourcepub fn request_id(&self) -> Option<&str>
pub fn request_id(&self) -> Option<&str>
The server’s identifier for this request, from x-typesafe-request-id.
None when the header is absent or is not text. This is the header’s
text exactly as it arrived; Display and Debug show it escaped and
cut at 128 characters instead.
Sourcepub fn endpoint(&self) -> Option<&str>
pub fn endpoint(&self) -> Option<&str>
The method and URL the request went to, without credentials, query or
fragment, as GET https://api.typesafe.ai/v1/models.
None when the failure was built without a request to name.
Sourcepub fn message(&self) -> &str
pub fn message(&self) -> &str
The sentence the server gave for this failure.
It is read from the body at the first of these that holds a string:
error, error.message, message, detail, detail.message, or a
list under detail whose entries are joined with ; as
<loc>: <msg>. Failing all of those, it is the body itself, compacted
when it is JSON; an empty body, or a body that is the JSON null,
gives status code (no body).
Whichever part of the body it came from, the text is the server’s, so
it is made safe to print: a control character, or a format character
that reorders or hides the text around it, is written as a Rust escape
(\n, \u{1b}), and the text is cut at 200 characters, counted after
escaping, and marked with U+2026. The Python SDK keeps a member’s text
as the server sent it and cuts only a body standing in for a message;
here every path is cut, so a server cannot break, recolour or flood
the log line of a caller that prints the error.
body_text still returns every byte.
It can be empty, which is how a caller-supplied empty message survives
to Display, where the status then stands alone.
Sourcepub fn error_type(&self) -> Option<&str>
pub fn error_type(&self) -> Option<&str>
The server’s machine-readable name for this failure, from
detail.error_type.
The live API answers a request with no key with 403 and
authentication_error here, which is the only way to tell that case
apart from a key that exists and lacks a permission.
This is the server’s text as it arrived, for a caller to compare. It is
never part of Display; Debug shows it escaped and cut at 128
characters, as it does the request id.
Sourcepub fn body_text(&self) -> Cow<'_, str>
pub fn body_text(&self) -> Cow<'_, str>
The response body as text, with anything that is not UTF-8 replaced by
U+FFFD.
Sourcepub fn body_json<'de, T>(&'de self) -> Result<T, DecodeError>where
T: Deserialize<'de>,
pub fn body_json<'de, T>(&'de self) -> Result<T, DecodeError>where
T: Deserialize<'de>,
The response body, decoded as JSON.
§Errors
Returns a DecodeError when the body is not JSON, is nested deeper
than this crate’s limit, or does not fit T.
Sourcepub fn retry_after(&self) -> Option<Duration>
pub fn retry_after(&self) -> Option<Duration>
How long the server asked the caller to wait, from retry-after-ms or
Retry-After.
Read for any status, not only 429: a 503 may carry the same headers.
Reads the system clock, because Retry-After may be an HTTP date.
Trait Implementations§
Source§impl Debug for ApiError
impl Debug for ApiError
Source§fn fmt(&self, formatter: &mut Formatter<'_>) -> Result
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result
Prints what identifies the failure, and nothing that could be a secret.
The headers are reduced to their count and the body to its length: a
response carries back whatever the request sent under Authorization
or a cookie in some proxy configurations, and a Debug that is printed
into a log must not be the thing that puts it there.
Source§impl Error for ApiError
impl Error for ApiError
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()
Auto Trait Implementations§
impl !Freeze for ApiError
impl RefUnwindSafe for ApiError
impl Send for ApiError
impl Sync for ApiError
impl Unpin for ApiError
impl UnsafeUnpin for ApiError
impl UnwindSafe for ApiError
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more