use tonic::Status; #[derive(Debug, thiserror::Error)] pub enum KenAuthError { #[error("kenoauth is unavailable: {0}")] Unavailable(String), #[error("kenoauth rejected the request: {0}")] Rejected(String), #[error("kenoauth could not find the requested object")] NotFound, #[error("kenoauth rejected the credential")] Unauthenticated, #[error("kenoauth configuration is invalid: {0}")] Config(String), #[error("kenoauth request failed: {0}")] Internal(String), } impl KenAuthError { pub fn from_status(status: Status) -> Self { match status.code() { tonic::Code::Unavailable | tonic::Code::DeadlineExceeded => { Self::Unavailable(status.message().to_string()) } tonic::Code::Unauthenticated => Self::Unauthenticated, tonic::Code::NotFound => Self::NotFound, tonic::Code::PermissionDenied | tonic::Code::InvalidArgument | tonic::Code::FailedPrecondition | tonic::Code::AlreadyExists | tonic::Code::ResourceExhausted | tonic::Code::Aborted => Self::Rejected(status.message().to_string()), _ => Self::Internal(status.message().to_string()), } } pub fn is_not_found(&self) -> bool { matches!(self, Self::NotFound) } pub fn is_unauthenticated(&self) -> bool { matches!(self, Self::Unauthenticated) } }