kruxiaflow_worker/
error.rs1use crate::types::UsageEntry;
4use rust_decimal::Decimal;
5
6#[derive(Debug, Clone)]
23pub struct ActivityError {
24 pub code: String,
26 pub message: String,
28 pub retryable: bool,
30 pub cost_usd: Option<Decimal>,
32 pub usage: Vec<UsageEntry>,
34}
35
36impl ActivityError {
37 pub fn retryable(code: impl Into<String>, message: impl Into<String>) -> Self {
40 Self {
41 code: code.into(),
42 message: message.into(),
43 retryable: true,
44 cost_usd: None,
45 usage: Vec::new(),
46 }
47 }
48
49 pub fn terminal(code: impl Into<String>, message: impl Into<String>) -> Self {
52 Self {
53 code: code.into(),
54 message: message.into(),
55 retryable: false,
56 cost_usd: None,
57 usage: Vec::new(),
58 }
59 }
60
61 pub fn with_cost(mut self, cost_usd: Decimal) -> Self {
64 self.cost_usd = Some(cost_usd);
65 self
66 }
67
68 pub fn with_usage(mut self, usage: Vec<UsageEntry>) -> Self {
70 self.usage = usage;
71 self
72 }
73}
74
75impl std::fmt::Display for ActivityError {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 write!(f, "{}: {}", self.code, self.message)
78 }
79}
80
81impl std::error::Error for ActivityError {}
82
83impl From<anyhow::Error> for ActivityError {
84 fn from(err: anyhow::Error) -> Self {
85 Self::retryable("EXECUTION_ERROR", format!("{err:#}"))
86 }
87}
88
89#[derive(Debug, thiserror::Error)]
91pub enum ConfigError {
92 #[error("Missing API URL (set KRUXIAFLOW_API_URL)")]
93 MissingApiUrl,
94
95 #[error(
96 "Partial OAuth credentials: set both KRUXIAFLOW_CLIENT_ID and KRUXIAFLOW_CLIENT_SECRET, \
97 or neither (server in dev mode)"
98 )]
99 PartialCredentials,
100
101 #[error(
102 "No worker name: set KRUXIAFLOW_WORKER, call worker(...) on the builder, \
103 or register activities that declare one"
104 )]
105 MissingWorker,
106
107 #[error(
108 "Ambiguous worker name: registered activities declare multiple workers ({0:?}); \
109 a worker polls one worker name — set it explicitly"
110 )]
111 AmbiguousWorker(Vec<String>),
112
113 #[error("Invalid value for {var}: {reason}")]
114 InvalidValue { var: String, reason: String },
115}
116
117#[derive(Debug, thiserror::Error)]
119pub enum ClientError {
120 #[error("HTTP request failed: {0}")]
121 Http(#[from] reqwest::Error),
122
123 #[error(
127 "Server requires authentication but no credentials are configured \
128 (set KRUXIAFLOW_CLIENT_ID and KRUXIAFLOW_CLIENT_SECRET)"
129 )]
130 AuthRequired,
131
132 #[error("Token request failed: {status} - {body}")]
133 Auth { status: u16, body: String },
134
135 #[error("Conflict: {body}")]
139 Conflict { body: String },
140
141 #[error("API error: {status} - {body}")]
142 Api { status: u16, body: String },
143
144 #[error("Wake stream closed: {0}")]
147 WakeStream(String),
148}