adk_surface/verbs/error.rs
1//! Why a screen action was refused or failed (spec 3.3 §7.4, §11).
2
3use adk_core::{AdkError, ErrorCategory, ErrorComponent};
4
5/// Why a screen action was refused or failed (spec 3.3 §2 item 7, §7.4).
6///
7/// Converts into [`AdkError`] with a `surface.verb.*` code; the web router answers with its
8/// problem document and HTTP status. Every message is plain words a surface may show.
9///
10/// | Variant | Code | Status |
11/// |---|---|---|
12/// | [`Unknown`](Self::Unknown), [`NotFound`](Self::NotFound) | `surface.verb.unknown` | 404 |
13/// | [`Reserved`](Self::Reserved) | `surface.verb.reserved` | 400 |
14/// | [`NotHere`](Self::NotHere) | `surface.verb.not_here` | 403 |
15/// | [`Invalid`](Self::Invalid) | `surface.verb.invalid` | 400 |
16/// | [`Ambiguous`](Self::Ambiguous) | `surface.verb.ambiguous` | 409 |
17/// | [`Refused`](Self::Refused) | `surface.verb.refused` | 409 |
18/// | [`Source`](Self::Source) | the source's code | the source's status |
19///
20/// # Example
21///
22/// ```rust
23/// use adk_surface::verbs::VerbError;
24///
25/// let error = VerbError::refused("It moved since you saw this");
26/// assert_eq!(error.code(), "surface.verb.refused");
27/// assert_eq!(adk_core::AdkError::from(error).http_status_code(), 409);
28/// ```
29#[derive(Debug, thiserror::Error)]
30pub enum VerbError {
31 /// No action of that name is offered here.
32 #[error("there is no screen action called {verb}; GET /api/surface/verbs lists them")]
33 Unknown {
34 /// The name asked for.
35 verb: String,
36 },
37 /// Nothing the caller may act on has that id or title. Also the answer for something that
38 /// exists but is someone else's or in another area, so a caller learns nothing about it
39 /// (spec 3.3 §11 S-I8).
40 #[error("nothing in Coming up is called {what}")]
41 NotFound {
42 /// What was looked for.
43 what: String,
44 },
45 /// The name is one of the decision, agent, governance and resume words no screen action
46 /// may take (spec 3.3 §7.4, S-I4, S-I10, S-I11).
47 #[error(
48 "`{name}` is reserved: a screen action never answers a decision, changes an agent, a \
49 rule, a mode or Stop all, or resumes work; choose another name"
50 )]
51 Reserved {
52 /// The name.
53 name: String,
54 },
55 /// The caller may not take this action from here: a chat channel or the tray, an agent
56 /// asking for an action only a person takes, or a remote terminal (spec 3.3 §7.2).
57 #[error("{0}")]
58 NotHere(String),
59 /// The request is malformed: a name outside `^[a-z][a-z0-9_]{0,58}$`, an object kind the
60 /// action does not take, arguments its schema refuses.
61 #[error("{0}")]
62 Invalid(String),
63 /// More than one thing matches what was said or typed.
64 #[error("{} things match “{said}” — use the full name: {}", matches.len(), matches.join(", "))]
65 Ambiguous {
66 /// What was said or typed.
67 said: String,
68 /// The titles that match, in Coming up order.
69 matches: Vec<String>,
70 },
71 /// The action cannot be taken now: the item started, moved since it was shown, ends too
72 /// soon, or the governed path refused it.
73 #[error("{0}")]
74 Refused(String),
75 /// The kernel or a store failed.
76 #[error(transparent)]
77 Source(#[from] AdkError),
78}
79
80impl VerbError {
81 /// A [`Refused`](Self::Refused) saying `why`.
82 pub fn refused(why: impl Into<String>) -> Self {
83 Self::Refused(why.into())
84 }
85
86 /// An [`Invalid`](Self::Invalid) saying `why`.
87 pub fn invalid(why: impl Into<String>) -> Self {
88 Self::Invalid(why.into())
89 }
90
91 /// A [`NotFound`](Self::NotFound) for `what`.
92 pub fn not_found(what: impl Into<String>) -> Self {
93 Self::NotFound { what: what.into() }
94 }
95
96 /// The error code: `surface.verb.unknown`, `surface.verb.reserved`,
97 /// `surface.verb.not_here`, `surface.verb.invalid`, `surface.verb.ambiguous`,
98 /// `surface.verb.refused`, or the source's own code.
99 pub fn code(&self) -> String {
100 match self {
101 Self::Source(error) => error.code.to_string(),
102 other => other.parts().1.to_string(),
103 }
104 }
105
106 fn parts(&self) -> (ErrorCategory, &'static str) {
107 match self {
108 Self::Unknown { .. } | Self::NotFound { .. } => {
109 (ErrorCategory::NotFound, "surface.verb.unknown")
110 }
111 Self::Reserved { .. } => (ErrorCategory::InvalidInput, "surface.verb.reserved"),
112 Self::NotHere(_) => (ErrorCategory::Forbidden, "surface.verb.not_here"),
113 Self::Invalid(_) => (ErrorCategory::InvalidInput, "surface.verb.invalid"),
114 Self::Ambiguous { .. } => (ErrorCategory::Conflict, "surface.verb.ambiguous"),
115 Self::Refused(_) => (ErrorCategory::Conflict, "surface.verb.refused"),
116 Self::Source(_) => (ErrorCategory::Internal, "surface.verb.source"),
117 }
118 }
119}
120
121impl From<adk_kernel::KernelError> for VerbError {
122 fn from(error: adk_kernel::KernelError) -> Self {
123 Self::Source(AdkError::from(error))
124 }
125}
126
127impl From<VerbError> for AdkError {
128 fn from(error: VerbError) -> Self {
129 if let VerbError::Source(inner) = error {
130 return inner;
131 }
132 let (category, code) = error.parts();
133 AdkError::new(ErrorComponent::Server, category, code, error.to_string())
134 }
135}
136
137impl From<VerbError> for crate::SurfaceError {
138 fn from(error: VerbError) -> Self {
139 Self::Source(AdkError::from(error))
140 }
141}