akinator_rs/
error.rs

1use thiserror::Error as ErrorBase;
2
3use std::time::SystemTimeError;
4use serde_json::error::Error as SerdeJsonError;
5use reqwest::Error as ReqwestError;
6use std::num::{
7    ParseFloatError,
8    ParseIntError,
9};
10
11
12/// returned in the update info methods
13#[derive(Debug, ErrorBase)]
14#[allow(clippy::module_name_repetitions)]
15pub enum UpdateInfoError {
16    /// from propogating [`ParseFloatError`] when parsing json struct fields into [`f32`] fails
17    #[error("Failed to parse data: {0}")]
18    ParseFloatError(#[from] ParseFloatError),
19
20    /// from propogating [`ParseIntError`] when parsing json struct fields into [`usize`] fails
21    #[error("Faield to parse data: {0}")]
22    ParseIntError(#[from] ParseIntError),
23
24    /// Deserialized struct is missing a needed field, probably `parameters`
25    #[error("Missing an expected json field")]
26    MissingData,
27}
28
29
30/// the main Error enum for errors returned from akinator functions
31#[derive(Debug, ErrorBase)]
32pub enum Error {
33    /// from propogating [`SystemTimeError`] when retrieving current timestamp fails when starting the game
34    #[error("Failed to get current time: {0}")]
35    TimeError(#[from] SystemTimeError),
36
37    /// from propogating [`ReqwestError`] when making HTTP requests fails with reqwests across many akinator functions
38    #[error("RequestError: {0}")]
39    RequestError(#[from] ReqwestError),
40
41    /// from propogating [`SerdeJsonError`] when deserializing json from [`str`] into a struct fails
42    #[error("Failed to parse JSON: {0}")]
43    JsonParseError(#[from] SerdeJsonError),
44
45    /// conversion from [`UpdateInfoError`]
46    #[error("Failed to update data fields: {0}")]
47    UpdateInfoError(#[from] UpdateInfoError),
48
49    /// from when searching for information such as the WS url and session info etc.
50    #[error("Failed to find the required information needed to start the game")]
51    NoDataFound,
52
53    /// from when the akinator servers in the specified region are down
54    #[error("The akinator servers in that region are currently down")]
55    ServersDown,
56
57    /// from when there is a technical internal error with the akinator servers
58    #[error("There is a technical error with the akinator servers")]
59    TechnicalError,
60
61    /// from when the akinator session timed out waiting for a response
62    #[error("Akinator session timed out waiting for a response")]
63    TimeoutError,
64
65    /// from when there are no more available questions the akinator has to offer
66    #[error("There are no more available questions")]
67    NoMoreQuestions,
68
69    /// from any other form of connection or server error
70    #[error("Failed to connect to akinator servers")]
71    ConnectionError,
72
73    /// from when calling `back`, fails often when we are already on the first questions so we can't go back any more
74    #[error("Cannot go back any further, you are already on the first question")]
75    CantGoBackAnyFurther,
76
77    /// Simply an invalid answer to respond to the question when parsing from string
78    #[error("Invalid Answer")]
79    InvalidAnswer,
80
81    /// from when an invalid or not supported language is passed when parsing from string
82    #[error("Invalid Language")]
83    InvalidLanguage,
84}
85
86/// result typealias with `E` that defaults to [`Error`]
87pub type Result<T, E = Error> = std::result::Result<T, E>;