use adk_core::{AdkError, ErrorCategory, ErrorComponent};
#[derive(Debug, thiserror::Error)]
pub enum VerbError {
#[error("there is no screen action called {verb}; GET /api/surface/verbs lists them")]
Unknown {
verb: String,
},
#[error("nothing in Coming up is called {what}")]
NotFound {
what: String,
},
#[error(
"`{name}` is reserved: a screen action never answers a decision, changes an agent, a \
rule, a mode or Stop all, or resumes work; choose another name"
)]
Reserved {
name: String,
},
#[error("{0}")]
NotHere(String),
#[error("{0}")]
Invalid(String),
#[error("{} things match “{said}” — use the full name: {}", matches.len(), matches.join(", "))]
Ambiguous {
said: String,
matches: Vec<String>,
},
#[error("{0}")]
Refused(String),
#[error(transparent)]
Source(#[from] AdkError),
}
impl VerbError {
pub fn refused(why: impl Into<String>) -> Self {
Self::Refused(why.into())
}
pub fn invalid(why: impl Into<String>) -> Self {
Self::Invalid(why.into())
}
pub fn not_found(what: impl Into<String>) -> Self {
Self::NotFound { what: what.into() }
}
pub fn code(&self) -> String {
match self {
Self::Source(error) => error.code.to_string(),
other => other.parts().1.to_string(),
}
}
fn parts(&self) -> (ErrorCategory, &'static str) {
match self {
Self::Unknown { .. } | Self::NotFound { .. } => {
(ErrorCategory::NotFound, "surface.verb.unknown")
}
Self::Reserved { .. } => (ErrorCategory::InvalidInput, "surface.verb.reserved"),
Self::NotHere(_) => (ErrorCategory::Forbidden, "surface.verb.not_here"),
Self::Invalid(_) => (ErrorCategory::InvalidInput, "surface.verb.invalid"),
Self::Ambiguous { .. } => (ErrorCategory::Conflict, "surface.verb.ambiguous"),
Self::Refused(_) => (ErrorCategory::Conflict, "surface.verb.refused"),
Self::Source(_) => (ErrorCategory::Internal, "surface.verb.source"),
}
}
}
impl From<adk_kernel::KernelError> for VerbError {
fn from(error: adk_kernel::KernelError) -> Self {
Self::Source(AdkError::from(error))
}
}
impl From<VerbError> for AdkError {
fn from(error: VerbError) -> Self {
if let VerbError::Source(inner) = error {
return inner;
}
let (category, code) = error.parts();
AdkError::new(ErrorComponent::Server, category, code, error.to_string())
}
}
impl From<VerbError> for crate::SurfaceError {
fn from(error: VerbError) -> Self {
Self::Source(AdkError::from(error))
}
}