llm_worker/llm_client/
error.rs

1//! LLMクライアントエラー型
2
3use std::fmt;
4
5/// LLMクライアントのエラー
6#[derive(Debug)]
7pub enum ClientError {
8    /// HTTPリクエストエラー
9    Http(reqwest::Error),
10    /// JSONパースエラー
11    Json(serde_json::Error),
12    /// SSEパースエラー
13    Sse(String),
14    /// APIエラー (プロバイダからのエラーレスポンス)
15    Api {
16        status: Option<u16>,
17        code: Option<String>,
18        message: String,
19    },
20    /// 設定エラー
21    Config(String),
22}
23
24impl fmt::Display for ClientError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            ClientError::Http(e) => write!(f, "HTTP error: {}", e),
28            ClientError::Json(e) => write!(f, "JSON parse error: {}", e),
29            ClientError::Sse(msg) => write!(f, "SSE parse error: {}", msg),
30            ClientError::Api {
31                status,
32                code,
33                message,
34            } => {
35                write!(f, "API error")?;
36                if let Some(s) = status {
37                    write!(f, " (status: {})", s)?;
38                }
39                if let Some(c) = code {
40                    write!(f, " [{}]", c)?;
41                }
42                write!(f, ": {}", message)
43            }
44            ClientError::Config(msg) => write!(f, "Config error: {}", msg),
45        }
46    }
47}
48
49impl std::error::Error for ClientError {
50    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
51        match self {
52            ClientError::Http(e) => Some(e),
53            ClientError::Json(e) => Some(e),
54            _ => None,
55        }
56    }
57}
58
59impl From<reqwest::Error> for ClientError {
60    fn from(err: reqwest::Error) -> Self {
61        ClientError::Http(err)
62    }
63}
64
65impl From<serde_json::Error> for ClientError {
66    fn from(err: serde_json::Error) -> Self {
67        ClientError::Json(err)
68    }
69}