use serde_json::Value;
use thiserror::Error;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Operation {
Validate,
Ingest,
}
impl std::fmt::Display for Operation {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(match self {
Self::Validate => "validate",
Self::Ingest => "ingest",
})
}
}
#[derive(Clone, Debug, Error)]
#[non_exhaustive]
pub enum Error {
#[error("invalid ReqKey configuration: {0}")]
Configuration(String),
#[error("ReqKey {operation} request timed out")]
Timeout {
operation: Operation,
},
#[error("could not reach ReqKey during {operation}: {message}")]
Transport {
operation: Operation,
message: String,
},
#[error("ReqKey rejected the project credential: {message}")]
Authentication {
status: u16,
message: String,
body: Option<Value>,
},
#[error("ReqKey API returned HTTP {status}: {message}")]
Api {
status: u16,
message: String,
body: Option<Value>,
},
}
impl Error {
pub fn status_code(&self) -> Option<u16> {
match self {
Self::Authentication { status, .. } | Self::Api { status, .. } => Some(*status),
_ => None,
}
}
pub fn is_unavailable(&self) -> bool {
matches!(
self,
Self::Timeout { .. } | Self::Transport { .. } | Self::Api { .. }
)
}
}
pub type Result<T> = std::result::Result<T, Error>;