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