1use adk_core::{AdkError, ErrorCategory, ErrorComponent};
4
5use crate::rules::RuleViolation;
6
7pub type Result<T> = std::result::Result<T, SurfaceError>;
9
10#[derive(Debug, thiserror::Error)]
15pub enum SurfaceError {
16 #[error("{what} was not found among your sessions and decisions")]
20 NotFound {
21 what: String,
23 },
24 #[error(transparent)]
26 Rule(#[from] RuleViolation),
27 #[error("{0}")]
29 NotHere(String),
30 #[error(
33 "stopping and resuming every agent needs the `{group}` group: they freeze and unfreeze \
34 every run this server makes"
35 )]
36 OperatorOnly {
37 group: String,
39 },
40 #[error(
42 "{what} is not available from this source; run the surface on a kernel or harness that provides it"
43 )]
44 Unsupported {
45 what: String,
47 },
48 #[error("{0}")]
50 Invalid(String),
51 #[error(transparent)]
53 Source(#[from] AdkError),
54}
55
56impl SurfaceError {
57 pub fn not_found(what: impl Into<String>) -> Self {
59 Self::NotFound { what: what.into() }
60 }
61
62 pub fn unsupported(what: impl Into<String>) -> Self {
64 Self::Unsupported { what: what.into() }
65 }
66
67 pub fn code(&self) -> String {
71 match self {
72 Self::NotFound { .. } => "surface.not_found".to_string(),
73 Self::Rule(rule) => rule.code().to_string(),
74 Self::NotHere(_) => "surface.not_here".to_string(),
75 Self::OperatorOnly { .. } => "surface.operator_only".to_string(),
76 Self::Unsupported { .. } => "surface.unsupported".to_string(),
77 Self::Invalid(_) => "surface.invalid".to_string(),
78 Self::Source(error) => error.code.to_string(),
79 }
80 }
81}
82
83impl From<adk_kernel::KernelError> for SurfaceError {
84 fn from(error: adk_kernel::KernelError) -> Self {
85 Self::Source(AdkError::from(error))
86 }
87}
88
89impl From<SurfaceError> for AdkError {
90 fn from(error: SurfaceError) -> Self {
91 let error = match error {
92 SurfaceError::Source(inner) => return inner,
93 other => other,
94 };
95 let (category, code) = match &error {
96 SurfaceError::NotFound { .. } => (ErrorCategory::NotFound, "surface.not_found"),
97 SurfaceError::Rule(rule) => (ErrorCategory::Forbidden, rule.code()),
98 SurfaceError::NotHere(_) => (ErrorCategory::Forbidden, "surface.not_here"),
99 SurfaceError::OperatorOnly { .. } => {
100 (ErrorCategory::Forbidden, "surface.operator_only")
101 }
102 SurfaceError::Unsupported { .. } => (ErrorCategory::Unsupported, "surface.unsupported"),
103 SurfaceError::Invalid(_) => (ErrorCategory::InvalidInput, "surface.invalid"),
104 SurfaceError::Source(_) => (ErrorCategory::Internal, "surface.source"),
105 };
106 AdkError::new(ErrorComponent::Server, category, code, error.to_string())
107 }
108}