use crate::{IDENTITY_PATH, K8S_SA_TOKENFILE_PATH, LOCAL_CA_CERT_PATH};
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("private key gen error")]
PrivateKeyGen,
#[error("Authly CA does not exist at {LOCAL_CA_CERT_PATH}")]
AuthlyCAmissingInEtc,
#[error("Authly CA error: {0}")]
AuthlyCA(&'static str),
#[error("identity error: {0}")]
Identity(&'static str),
#[error("tls problem: {0}")]
Tls(&'static str),
#[error(
"environment not inferrable: Neither {IDENTITY_PATH} or {K8S_SA_TOKENFILE_PATH} exists"
)]
EnvironmentNotInferrable,
#[error("invalid X509 common name")]
InvalidCommonName,
#[error("unauthorized: {0}")]
Unauthorized(anyhow::Error),
#[error("network error: {0}")]
Network(anyhow::Error),
#[error("invalid access token: {0}")]
InvalidAccessToken(anyhow::Error),
#[error("encoding error: {0}")]
Codec(anyhow::Error),
#[error("invalid property/attribute label")]
InvalidPropertyAttributeLabel,
#[error("unclassified error: {0}")]
Unclassified(anyhow::Error),
}
pub(crate) fn unclassified(err: impl std::error::Error + Send + Sync + 'static) -> Error {
Error::Unclassified(anyhow::Error::from(err))
}
pub(crate) fn tonic(err: tonic::Status) -> Error {
match err.code() {
tonic::Code::Unauthenticated => Error::Unauthorized(err.into()),
tonic::Code::PermissionDenied => Error::Unauthorized(err.into()),
_ => Error::Network(err.into()),
}
}
pub(crate) fn network(err: impl std::error::Error + Send + Sync + 'static) -> Error {
Error::Unauthorized(anyhow::Error::from(err))
}
pub(crate) fn unauthorized(err: impl std::error::Error + Send + Sync + 'static) -> Error {
Error::Unauthorized(anyhow::Error::from(err))
}