Skip to main content

horizons_ai/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum HorizonsErrorKind {
5    NotFound,
6    Auth,
7    Validation,
8    Server,
9    Stream,
10    Transport,
11    Serialization,
12}
13
14#[derive(Debug, Error)]
15#[error("{kind:?}: {message}")]
16pub struct HorizonsError {
17    pub kind: HorizonsErrorKind,
18    pub status: Option<u16>,
19    pub message: String,
20}
21
22impl HorizonsError {
23    pub fn new(kind: HorizonsErrorKind, status: Option<u16>, message: impl Into<String>) -> Self {
24        Self {
25            kind,
26            status,
27            message: message.into(),
28        }
29    }
30}
31
32impl From<reqwest::Error> for HorizonsError {
33    fn from(e: reqwest::Error) -> Self {
34        HorizonsError::new(HorizonsErrorKind::Transport, None, e.to_string())
35    }
36}
37
38impl From<serde_json::Error> for HorizonsError {
39    fn from(e: serde_json::Error) -> Self {
40        HorizonsError::new(HorizonsErrorKind::Serialization, None, e.to_string())
41    }
42}
43