use std::time::Duration;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum HeyoError {
#[error("Missing API key. Pass `api_key` or set HEYO_API_KEY.")]
Authentication,
#[error("{0}")]
InvalidArgument(String),
#[error("not found: {0}")]
NotFound(String),
#[error("api error ({status}): {message}")]
Api {
status: u16,
message: String,
body: Option<serde_json::Value>,
},
#[error("timeout after {0:?}: {1}")]
Timeout(Duration, String),
#[error("sandbox {sandbox_id} failed: {reason}")]
SandboxFailed {
sandbox_id: String,
reason: String,
},
#[error("connection error: {0}")]
Connection(String),
#[error("shell session expired{}", .session_id.as_deref().map(|s| format!(" ({})", s)).unwrap_or_default())]
SessionExpired { session_id: Option<String> },
#[error("shell exited with code {0}")]
ShellExit(i32),
#[error("checkin conflict: expected={expected:?} current={current}")]
CheckinConflict {
expected: Option<i64>,
current: i64,
},
}
impl HeyoError {
pub(crate) fn invalid(msg: impl Into<String>) -> Self {
HeyoError::InvalidArgument(msg.into())
}
pub(crate) fn api(status: u16, message: impl Into<String>) -> Self {
HeyoError::Api {
status,
message: message.into(),
body: None,
}
}
pub(crate) fn api_with_body(
status: u16,
message: impl Into<String>,
body: Option<serde_json::Value>,
) -> Self {
HeyoError::Api {
status,
message: message.into(),
body,
}
}
}