mtjp9_rs_auth0_client/
error.rs1pub type Result<T, E = Auth0Error> = std::result::Result<T, E>;
2
3#[derive(Debug, thiserror::Error)]
5pub enum Auth0Error {
6 #[error("network/json error: {0}")]
8 Transport(#[from] reqwest::Error),
9
10 #[error("invalid request: {0}")]
12 InvalidRequest(String),
13
14 #[error("unauthorized: {0}")]
16 Unauthorized(String),
17
18 #[error("forbidden / insufficient scope: {0}")]
20 Forbidden(String),
21
22 #[error("conflict status {status}: {body}")]
23 Conflict { status: u16, body: String },
24
25 #[error("rate limited: {0}")]
27 TooManyRequests(String),
28
29 #[error("unexpected status {status}: {body}")]
31 UnexpectedResponse { status: u16, body: String },
32}
33
34impl Auth0Error {
35 pub async fn from_response(resp: reqwest::Response) -> Self {
37 let status = resp.status();
38 let body = resp.text().await.unwrap_or_default();
39 match status.as_u16() {
40 400 => Self::InvalidRequest(body),
41 401 => Self::Unauthorized(body),
42 403 => Self::Forbidden(body),
43 409 => Self::Conflict {
44 status: status.as_u16(),
45 body,
46 },
47 429 => Self::TooManyRequests(body),
48 code => Self::UnexpectedResponse { status: code, body },
49 }
50 }
51}