Skip to main content

jwks_cache/
error.rs

1//! Crate-wide error types and `Result` alias.
2
3/// Library-wide result type.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Unified error type for the JWKS cache crate.
7#[allow(missing_docs)]
8#[derive(Debug, thiserror::Error)]
9pub enum Error {
10	#[error(transparent)]
11	Io(#[from] std::io::Error),
12	#[error(transparent)]
13	SystemTime(#[from] std::time::SystemTimeError),
14
15	#[error(transparent)]
16	Http(#[from] http::Error),
17	#[error(transparent)]
18	Jsonwebtoken(#[from] jsonwebtoken::errors::Error),
19	#[error(transparent)]
20	Reqwest(#[from] reqwest::Error),
21	#[error(transparent)]
22	Serde(#[from] serde_json::Error),
23	#[error(transparent)]
24	Url(#[from] url::ParseError),
25
26	#[cfg(feature = "redis")]
27	#[error(transparent)]
28	Redis(#[from] redis::RedisError),
29
30	#[error("Cache error: {0}")]
31	Cache(String),
32	#[error("Upstream HTTP status {status} from {url}: {body:?}")]
33	HttpStatus { status: http::StatusCode, url: url::Url, body: Option<String> },
34	#[error("Metrics error: {0}")]
35	Metrics(String),
36	#[error("Provider not registered for tenant '{tenant}' and id '{provider}'.")]
37	NotRegistered { tenant: String, provider: String },
38	#[error("Security violation: {0}")]
39	Security(String),
40	#[error("Validation failed for {field}: {reason}")]
41	Validation { field: &'static str, reason: String },
42}
43#[cfg(feature = "metrics")]
44impl<T> From<metrics::SetRecorderError<T>> for Error
45where
46	T: std::fmt::Display,
47{
48	fn from(value: metrics::SetRecorderError<T>) -> Self {
49		Self::Metrics(value.to_string())
50	}
51}