call_agent/chat/
err.rs

1use std::fmt;
2use std::error::Error;
3
4/// アプリケーションで使うエラー型
5#[derive(Debug)]
6pub enum ClientError {
7    /// ファイルが見つからなかった場合など
8    NotFound(String),
9    /// 入力が不正な場合
10    InvalidInput(String),
11    /// I/O操作中のエラー
12    IoError(std::io::Error),
13    IndexOutOfBounds,
14    ToolNotFound,
15    InvalidEndpoint,
16    InvalidPrompt,
17    NetworkError,
18    InvalidResponse,
19    ModelConfigNotSet,
20    UnknownError,
21}
22
23/// Implements the Display trait for ClientError, providing human-readable error messages
24/// for each variant.
25///
26/// This implementation ensures that all error messages are consistently formatted across
27/// the crate. Depending on the specific variant, a descriptive message or the underlying
28/// error details (e.g., for I/O errors) are displayed.
29///
30/// Error Variants:
31/// - NotFound: Indicates a missing resource or item. The message provides additional context.
32/// - InvalidInput: Denotes that the provided input is not valid. The message explains the issue.
33/// - IoError: Wraps a standard I/O error, relaying the system error message.
34/// - IndexOutOfBounds: Indicates that an index is outside the allowable bounds.
35/// - ToolNotFound: Signals that a required external tool was not found.
36/// - InvalidEndpoint: Denotes that a specified endpoint URL or address is invalid.
37/// - InvalidPrompt: Indicates that a provided prompt does not meet expected criteria.
38/// - NetworkError: Reflects issues with network connectivity or communication.
39/// - InvalidResponse: Indicates that the response received does not match the expected format.
40/// - UnknownError: A catch-all for errors that do not fit any of the other categories.
41///
42/// These messages are intended for crate users and are provided in English to support clarity
43/// and internationalization.
44impl fmt::Display for ClientError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            ClientError::NotFound(ref msg) => write!(f, "NotFound: {}", msg),
48            ClientError::InvalidInput(ref msg) => write!(f, "InvalidInput: {}", msg),
49            ClientError::IoError(ref err) => write!(f, "IoError: {}", err),
50            ClientError::IndexOutOfBounds => write!(f, "Index out of bounds"),
51            ClientError::ToolNotFound => write!(f, "Tool not found"),
52            ClientError::InvalidEndpoint => write!(f, "Invalid endpoint"),
53            ClientError::InvalidPrompt => write!(f, "Invalid prompt"),
54            ClientError::NetworkError => write!(f, "Network error"),
55            ClientError::InvalidResponse => write!(f, "Invalid response"),
56            ClientError::ModelConfigNotSet => write!(f, "Model config not set"),
57            ClientError::UnknownError => write!(f, "Unknown error"),
58        }
59    }
60}
61
62impl Error for ClientError {
63    fn source(&self) -> Option<&(dyn Error + 'static)> {
64        match self {
65            ClientError::IoError(ref err) => Some(err),
66            _ => None,
67        }
68    }
69}
70
71// std::io::ErrorからAppErrorへの変換を可能にする
72impl From<std::io::Error> for ClientError {
73    fn from(err: std::io::Error) -> Self {
74        ClientError::IoError(err)
75    }
76}
77
78impl From<String> for ClientError {
79    fn from(err: String) -> Self {
80        ClientError::InvalidInput(err)
81    }
82}