Skip to main content

rpc_agent/
error.rs

1use std::fmt::Display;
2
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6#[derive(Error, Debug)]
7pub enum Error {
8    #[error("provider error: {0}")]
9    ProviderError(String),
10    #[error("authentication error: {0}")]
11    AuthenticationError(String),
12    #[error("client error: {0}")]
13    HttpError(#[from] rig::http_client::Error),
14    #[error("prompt error: {0}")]
15    PromptError(#[from] rig::completion::PromptError),
16    #[error("io error: {0}")]
17    Io(#[from] std::io::Error),
18}
19
20#[derive(Debug, Deserialize, Serialize)]
21pub struct ApiError {
22    pub status: u16,
23    pub message: String,
24}
25
26impl Display for ApiError {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        write!(f, "{}: {}", self.status, self.message)
29    }
30}
31
32impl From<Error> for ApiError {
33    fn from(value: Error) -> Self {
34        match value {
35            Error::ProviderError(e) => ApiError {
36                status: 500,
37                message: e,
38            },
39            Error::HttpError(error) => ApiError {
40                status: 500,
41                message: error.to_string(),
42            },
43            Error::PromptError(prompt_error) => ApiError {
44                status: 500,
45                message: prompt_error.to_string(),
46            },
47            Error::Io(error) => ApiError {
48                status: 500,
49                message: error.to_string(),
50            },
51            Error::AuthenticationError(e) => ApiError {
52                status: 401,
53                message: e,
54            },
55        }
56    }
57}
58
59impl std::error::Error for ApiError {}