Skip to main content

tt_preview/
error.rs

1//! Preview engine errors. Designed so the HTTP handler can always emit a
2//! valid `PreviewResponse` with `warnings[]`, never a 5xx.
3
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7pub enum PreviewError {
8    #[error("unknown model `{0}` — no pricing table available")]
9    UnknownModel(String),
10    #[error("malformed request: {0}")]
11    Malformed(String),
12    #[error("tokenizer failure: {0}")]
13    Tokenizer(String),
14}
15
16impl PreviewError {
17    pub fn as_warning(&self) -> String {
18        match self {
19            Self::UnknownModel(m) => {
20                format!("model {m} not in pricing table; falling back to heuristic")
21            }
22            Self::Malformed(s) => format!("malformed input: {s}"),
23            Self::Tokenizer(s) => format!("tokenizer failed: {s} — using char-count heuristic"),
24        }
25    }
26}