Skip to main content

adk_surface/
error.rs

1//! Errors of the engine and its sources (spec 3.3 §6).
2
3use adk_core::{AdkError, ErrorCategory, ErrorComponent};
4
5use crate::rules::RuleViolation;
6
7/// The engine's result.
8pub type Result<T> = std::result::Result<T, SurfaceError>;
9
10/// Why the engine refused or failed a read or a command.
11///
12/// Converts into [`AdkError`] with a `surface.*` code; the web router answers with its problem
13/// document and HTTP status.
14#[derive(Debug, thiserror::Error)]
15pub enum SurfaceError {
16    /// Nothing of that name belongs to the caller. Also the answer for something that exists
17    /// but belongs to someone else, so a surface learns nothing about other people's sessions
18    /// (spec 3.3 §11 S-I8).
19    #[error("{what} was not found among your sessions and decisions")]
20    NotFound {
21        /// What was looked for.
22        what: String,
23    },
24    /// The answer breaks a rule of spec 3.3 §7.2 or §7.4.
25    #[error(transparent)]
26    Rule(#[from] RuleViolation),
27    /// The caller may not do this from here (spec 3.3 §7.4, §11 S-I10).
28    #[error("{0}")]
29    NotHere(String),
30    /// Stop all or Resume all from a caller outside the group the engine keeps them for
31    /// (spec 3.3 §11 S-I10).
32    #[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        /// The group they need.
38        group: String,
39    },
40    /// The source cannot do this.
41    #[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 was asked.
46        what: String,
47    },
48    /// The request is malformed.
49    #[error("{0}")]
50    Invalid(String),
51    /// The source failed.
52    #[error(transparent)]
53    Source(#[from] AdkError),
54}
55
56impl SurfaceError {
57    /// A [`NotFound`](Self::NotFound) for `what`.
58    pub fn not_found(what: impl Into<String>) -> Self {
59        Self::NotFound { what: what.into() }
60    }
61
62    /// An [`Unsupported`](Self::Unsupported) for `what`.
63    pub fn unsupported(what: impl Into<String>) -> Self {
64        Self::Unsupported { what: what.into() }
65    }
66
67    /// The error code: `surface.not_found`, a rule's code, `surface.not_here`,
68    /// `surface.operator_only`, `surface.unsupported`, `surface.invalid`, or the source's own
69    /// code.
70    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}