Skip to main content

seher/sdk/
errors.rs

1use chrono::{DateTime, Utc};
2
3#[derive(Debug, thiserror::Error)]
4#[error("Provider '{provider}' hit API rate/usage limit")]
5pub struct LimitError {
6    pub provider: String,
7    pub reset_at: Option<DateTime<Utc>>,
8}
9
10#[derive(Debug, thiserror::Error)]
11#[error("seher '{label}' timed out after {ms}ms")]
12pub struct TimeoutError {
13    pub ms: u64,
14    pub label: &'static str,
15}
16
17/// Errors returned by [`crate::sdk::PiRunner::run`]. Each variant carries any
18/// `partial` assistant text accumulated before the failure (may be empty).
19#[derive(Debug, thiserror::Error)]
20pub enum RunError {
21    #[error("{error} (partial output: {} chars)", partial.len())]
22    Limit {
23        #[source]
24        error: LimitError,
25        partial: String,
26    },
27    #[error("{error} (partial output: {} chars)", partial.len())]
28    Timeout {
29        #[source]
30        error: TimeoutError,
31        partial: String,
32    },
33    #[error("{message} (partial output: {} chars)", partial.len())]
34    Other { message: String, partial: String },
35}
36
37impl RunError {
38    /// Return the partial text accumulated before the failure (may be empty).
39    #[must_use]
40    pub fn partial(&self) -> &str {
41        match self {
42            Self::Limit { partial, .. }
43            | Self::Timeout { partial, .. }
44            | Self::Other { partial, .. } => partial,
45        }
46    }
47}