Skip to main content

tripo_api/
error.rs

1//! Error types returned by the client.
2
3use std::path::PathBuf;
4
5use crate::types::{TaskId, TaskStatus};
6
7/// Result alias using [`Error`].
8pub type Result<T, E = Error> = std::result::Result<T, E>;
9
10/// Errors returned by the client.
11#[derive(thiserror::Error, Debug)]
12#[non_exhaustive]
13pub enum Error {
14    /// HTTP layer error with no structured API envelope.
15    #[error("HTTP {status}: {message}")]
16    Http {
17        /// HTTP status code.
18        status: u16,
19        /// Response body or derived message.
20        message: String,
21    },
22
23    /// Structured API error envelope (Tripo returns `{code, message, suggestion}`).
24    #[error("API [{code}] {message}{}", suggestion.as_deref().map(|s| format!(" — {s}")).unwrap_or_default())]
25    Api {
26        /// API-specific error code.
27        code: i32,
28        /// Human-readable error message.
29        message: String,
30        /// Optional suggestion for how to fix the error.
31        suggestion: Option<String>,
32    },
33
34    /// A task polling loop observed a non-success terminal status.
35    #[error("task {0} ended with status {1:?}")]
36    TaskFailed(TaskId, TaskStatus),
37
38    /// `wait_for_task` exceeded its timeout.
39    #[error("timed out waiting for task {0}")]
40    WaitTimeout(TaskId),
41
42    /// `TRIPO_API_KEY` not set and no key passed programmatically.
43    #[error("missing API key (set TRIPO_API_KEY or pass --api-key)")]
44    MissingApiKey,
45
46    /// API key does not begin with `tsk_`.
47    #[error("invalid API key (must start with `tsk_`)")]
48    InvalidApiKey,
49
50    /// Download target exists and `overwrite` was not set.
51    #[error("file already exists: {0} (use --force to overwrite)")]
52    FileExists(PathBuf),
53
54    /// Client-side request validation failed before the request was sent.
55    #[error("invalid request: {0}")]
56    InvalidRequest(String),
57
58    /// I/O error.
59    #[error(transparent)]
60    Io(#[from] std::io::Error),
61
62    /// HTTP transport error.
63    #[error(transparent)]
64    Reqwest(#[from] reqwest::Error),
65
66    /// JSON (de)serialization error.
67    #[error(transparent)]
68    Json(#[from] serde_json::Error),
69}