#[non_exhaustive]pub struct ApiError {
pub status: u16,
pub kind: ApiErrorKind,
pub error_type: Option<String>,
pub message: String,
pub body: Option<Value>,
pub request_id: Option<String>,
pub retry_after: Option<Duration>,
}Expand description
A non-success response from the API.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.status: u16The HTTP status code.
kind: ApiErrorKindThe category of the status.
error_type: Option<String>The server’s detail.error_type, for example authentication_error.
message: StringA human-readable description taken from the response body.
body: Option<Value>The response body, when it was JSON.
request_id: Option<String>The x-typesafe-request-id response header.
retry_after: Option<Duration>How long the server asked to wait, from retry-after-ms or Retry-After.
Implementations§
Source§impl ApiError
impl ApiError
Sourcepub fn new(status: u16, message: impl Into<String>) -> Self
pub fn new(status: u16, message: impl Into<String>) -> Self
An error with status and message, for example to script a failure in a fake.
use std::time::Duration;
use typesafe_client::{ApiError, ApiErrorKind};
let error = ApiError::new(429, "Too many requests").with_retry_after(Duration::from_secs(1));
assert_eq!(error.kind, ApiErrorKind::RateLimited);Sourcepub fn with_error_type(self, error_type: impl Into<String>) -> Self
pub fn with_error_type(self, error_type: impl Into<String>) -> Self
Sets the server’s error type, such as authentication_error.
Sourcepub fn with_request_id(self, request_id: impl Into<String>) -> Self
pub fn with_request_id(self, request_id: impl Into<String>) -> Self
Sets the request id.
Sourcepub fn with_retry_after(self, retry_after: Duration) -> Self
pub fn with_retry_after(self, retry_after: Duration) -> Self
Sets how long the server asked to wait.
Sourcepub fn from_response(
status: u16,
body: &[u8],
request_id: Option<String>,
retry_after: Option<Duration>,
) -> Self
pub fn from_response( status: u16, body: &[u8], request_id: Option<String>, retry_after: Option<Duration>, ) -> Self
Builds an error from a raw HTTP response, for transports other than the built-in
client. Reads {"detail": {"error_type", "message"}}, {"detail": "..."} and
validation lists ({"detail": [{"loc", "msg", "type"}]}).
Sourcepub fn field_errors(&self) -> Vec<FieldError>
pub fn field_errors(&self) -> Vec<FieldError>
The fields a validation (422) response rejected. Empty for other errors.
Trait Implementations§
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()