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