Skip to main content

rs_identity_model/discovery/
error.rs

1//! Typed discovery failures.
2
3use crate::IdentityError;
4use thiserror::Error;
5
6/// Failures from [`super::DiscoveryClient::discover`].
7#[derive(Debug, Error)]
8#[non_exhaustive]
9pub enum DiscoveryError {
10    /// The discovery endpoint returned a non-success status (DISC-006).
11    #[error("unexpected HTTP status {status} from {endpoint}")]
12    UnexpectedStatus { status: u16, endpoint: String },
13    /// The body is not a JSON provider metadata document (DISC-007).
14    #[error("invalid discovery document from {endpoint}: {reason}")]
15    InvalidJson { endpoint: String, reason: String },
16    /// Required metadata fields are absent or empty, in spec order (DISC-008).
17    #[error("discovery document from {endpoint} is missing required field(s): {}", .fields.join(", "))]
18    MissingFields {
19        fields: Vec<String>,
20        endpoint: String,
21    },
22    /// The document's issuer differs from the requested issuer (DISC-003).
23    #[error("issuer mismatch: requested {requested:?} but document declares {actual:?}")]
24    IssuerMismatch { requested: String, actual: String },
25    /// The issuer scheme violates the client's HTTPS requirement (DISC-010).
26    #[error("issuer {issuer:?} must use https (enable allow_http for development)")]
27    HttpsRequired { issuer: String },
28    /// Transport failures, oversized bodies and an empty issuer URL.
29    #[error(transparent)]
30    Other(#[from] IdentityError),
31}
32
33impl From<DiscoveryError> for IdentityError {
34    fn from(error: DiscoveryError) -> Self {
35        match error {
36            DiscoveryError::Other(error) => error,
37            error @ DiscoveryError::UnexpectedStatus { .. } => Self::Http(error.to_string()),
38            error @ DiscoveryError::InvalidJson { .. } => Self::Deserialization(error.to_string()),
39            error => Self::Validation(error.to_string()),
40        }
41    }
42}