use crate::types::UsageEntry;
use rust_decimal::Decimal;
#[derive(Debug, Clone)]
pub struct ActivityError {
pub code: String,
pub message: String,
pub retryable: bool,
pub cost_usd: Option<Decimal>,
pub usage: Vec<UsageEntry>,
}
impl ActivityError {
pub fn retryable(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
code: code.into(),
message: message.into(),
retryable: true,
cost_usd: None,
usage: Vec::new(),
}
}
pub fn terminal(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
code: code.into(),
message: message.into(),
retryable: false,
cost_usd: None,
usage: Vec::new(),
}
}
pub fn with_cost(mut self, cost_usd: Decimal) -> Self {
self.cost_usd = Some(cost_usd);
self
}
pub fn with_usage(mut self, usage: Vec<UsageEntry>) -> Self {
self.usage = usage;
self
}
}
impl std::fmt::Display for ActivityError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.code, self.message)
}
}
impl std::error::Error for ActivityError {}
impl From<anyhow::Error> for ActivityError {
fn from(err: anyhow::Error) -> Self {
Self::retryable("EXECUTION_ERROR", format!("{err:#}"))
}
}
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
#[error("Missing API URL (set KRUXIAFLOW_API_URL)")]
MissingApiUrl,
#[error(
"Partial OAuth credentials: set both KRUXIAFLOW_CLIENT_ID and KRUXIAFLOW_CLIENT_SECRET, \
or neither (server in dev mode)"
)]
PartialCredentials,
#[error(
"No worker name: set KRUXIAFLOW_WORKER, call worker(...) on the builder, \
or register activities that declare one"
)]
MissingWorker,
#[error(
"Ambiguous worker name: registered activities declare multiple workers ({0:?}); \
a worker polls one worker name — set it explicitly"
)]
AmbiguousWorker(Vec<String>),
#[error("Invalid value for {var}: {reason}")]
InvalidValue { var: String, reason: String },
}
#[derive(Debug, thiserror::Error)]
pub enum ClientError {
#[error("HTTP request failed: {0}")]
Http(#[from] reqwest::Error),
#[error(
"Server requires authentication but no credentials are configured \
(set KRUXIAFLOW_CLIENT_ID and KRUXIAFLOW_CLIENT_SECRET)"
)]
AuthRequired,
#[error("Token request failed: {status} - {body}")]
Auth { status: u16, body: String },
#[error("Conflict: {body}")]
Conflict { body: String },
#[error("API error: {status} - {body}")]
Api { status: u16, body: String },
#[error("Wake stream closed: {0}")]
WakeStream(String),
}