agentmux_srv/server/account_episodes/switch_error.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Why an account switch did not happen: what went wrong, for code, and the words the caller reads.
5
6/// Why an account switch did not happen: what went wrong, and the words the caller reads.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub(crate) struct SwitchError {
9 reason: SwitchRefusal,
10 message: String,
11}
12
13/// What went wrong, for code. The words are for whoever reads them.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub(crate) enum SwitchRefusal {
16 /// The request names no agent.
17 NoAgentNamed,
18 /// No agent has that id or name.
19 UnknownAgent,
20 /// More than one agent has the name: the request must say which.
21 AgentNameShared,
22 /// No account has the name asked for.
23 NoSuchAccount,
24 /// The name asked for fits more than one account: the request must say which.
25 AccountNameShared,
26 /// The request is right, and no account it could mean has quota now.
27 NoQuota,
28 /// The request is right, and the account or the agent cannot take this switch as things are
29 /// (not signed in, a key rejected, an agent on a model route).
30 CantRun,
31 /// An API key for an agent that is not Claude: no such agent takes one.
32 KeyNotForThisAgent,
33 /// AgentMux failed: the store, a restart, anything passed up from below.
34 Failed,
35}
36
37impl SwitchError {
38 pub(crate) fn new(reason: SwitchRefusal, message: impl Into<String>) -> Self {
39 SwitchError { reason, message: message.into() }
40 }
41
42 pub(crate) fn reason(&self) -> SwitchRefusal {
43 self.reason
44 }
45
46 /// The same error under the name of the call that met it: `<call>: <message>`.
47 pub(crate) fn under(self, call: &str) -> Self {
48 SwitchError { reason: self.reason, message: format!("{call}: {}", self.message) }
49 }
50}
51
52impl std::fmt::Display for SwitchError {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 f.write_str(&self.message)
55 }
56}
57
58impl From<String> for SwitchError {
59 /// An error passed up from below as words (a store error): AgentMux failed.
60 fn from(message: String) -> Self {
61 SwitchError::new(SwitchRefusal::Failed, message)
62 }
63}
64
65impl From<SwitchError> for String {
66 fn from(e: SwitchError) -> Self {
67 e.message
68 }
69}