artifactize_core/agent/
error.rs1use std::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
7#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
8pub enum Code {
9 Authentication,
11 Quota,
13 RateLimit,
15 Transient,
18 Timeout,
20 Cancelled,
21 ProviderBudgetExceeded,
23 InvalidResult,
25 ProviderError,
28 AgentError,
30}
31
32impl From<Code> for crate::types::FailureCode {
34 fn from(code: Code) -> Self {
35 match code {
36 Code::Authentication => Self::Authentication,
37 Code::Quota => Self::Quota,
38 Code::RateLimit => Self::RateLimit,
39 Code::Transient => Self::Transient,
40 Code::Timeout => Self::Timeout,
41 Code::Cancelled => Self::Cancelled,
42 Code::ProviderBudgetExceeded => Self::ProviderBudgetExceeded,
43 Code::InvalidResult => Self::InvalidResult,
44 Code::ProviderError => Self::ProviderError,
45 Code::AgentError => Self::AgentError,
46 }
47 }
48}
49
50impl Code {
51 pub fn as_str(self) -> &'static str {
52 match self {
53 Self::Authentication => "AUTHENTICATION",
54 Self::Quota => "QUOTA",
55 Self::RateLimit => "RATE_LIMIT",
56 Self::Transient => "TRANSIENT",
57 Self::Timeout => "TIMEOUT",
58 Self::Cancelled => "CANCELLED",
59 Self::ProviderBudgetExceeded => "PROVIDER_BUDGET_EXCEEDED",
60 Self::InvalidResult => "INVALID_RESULT",
61 Self::ProviderError => "PROVIDER_ERROR",
62 Self::AgentError => "AGENT_ERROR",
63 }
64 }
65
66 pub fn retryable(self) -> bool {
68 matches!(self, Self::RateLimit | Self::Transient)
69 }
70
71 pub fn stops_backend(code: &str) -> bool {
74 code == Self::Authentication.as_str() || code == Self::Quota.as_str()
75 }
76}
77
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub struct Failure {
80 pub code: Code,
81 pub message: String,
82}
83
84impl Failure {
85 pub fn new(code: Code, message: impl Into<String>) -> Self {
86 Self {
87 code,
88 message: message.into(),
89 }
90 }
91
92 pub fn cancelled() -> Self {
93 Self::new(Code::Cancelled, "Agent review was cancelled.")
94 }
95
96 pub fn timeout() -> Self {
97 Self::new(Code::Timeout, "Agent review timed out.")
98 }
99}
100
101impl From<String> for Failure {
103 fn from(message: String) -> Self {
104 Self::new(Code::AgentError, message)
105 }
106}
107
108impl fmt::Display for Failure {
109 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
110 formatter.write_str(&self.message)
111 }
112}