use auths_core::error::AuthsErrorInfo;
use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum AgentError {
#[error("identity not found: {did}")]
IdentityNotFound {
did: String,
},
#[error("an agent key already exists under alias '{alias}'")]
AlreadyDelegated {
alias: String,
},
#[error("agent not found: {did}")]
AgentNotFound {
did: String,
},
#[error("agent {did} is revoked")]
Revoked {
did: String,
},
#[error("requested capability '{capability}' exceeds the delegator's scope")]
OutsideDelegatorScope {
capability: String,
},
#[error("crypto error: {0}")]
CryptoError(#[source] auths_core::AgentError),
#[error("agent delegation failed: {0}")]
DelegationError(#[source] auths_id::error::InitError),
}
impl From<auths_core::AgentError> for AgentError {
fn from(err: auths_core::AgentError) -> Self {
AgentError::CryptoError(err)
}
}
impl AuthsErrorInfo for AgentError {
fn error_code(&self) -> &'static str {
match self {
Self::IdentityNotFound { .. } => "AUTHS-E5311",
Self::AlreadyDelegated { .. } => "AUTHS-E5312",
Self::CryptoError(e) => e.error_code(),
Self::DelegationError(_) => "AUTHS-E5313",
Self::AgentNotFound { .. } => "AUTHS-E5314",
Self::Revoked { .. } => "AUTHS-E5315",
Self::OutsideDelegatorScope { .. } => "AUTHS-E5316",
}
}
fn suggestion(&self) -> Option<&'static str> {
match self {
Self::IdentityNotFound { .. } => {
Some("Run `auths init` to create a root identity first")
}
Self::AlreadyDelegated { .. } => {
Some("Choose a different --label; run `auths id agent list` to see existing agents")
}
Self::CryptoError(e) => e.suggestion(),
Self::DelegationError(_) => Some(
"The agent delegation could not be authored or anchored; check the root identity",
),
Self::AgentNotFound { .. } => {
Some("Run `auths id agent list` to see the agents this identity has delegated")
}
Self::Revoked { .. } => {
Some("This agent was revoked and cannot be rotated; delegate a new agent instead")
}
Self::OutsideDelegatorScope { .. } => {
Some("Narrow the agent's --scope to a subset of the delegator's own capabilities")
}
}
}
}