sf_api/
error.rs

1use std::{error::Error, fmt::Display};
2
3#[derive(Debug)]
4#[non_exhaustive]
5#[allow(clippy::module_name_repetitions)]
6/// An error, that occurred during the communication (sending/receiving/parsing)
7/// of requests to the S&F server
8pub enum SFError {
9    /// Whatever you were trying to send was not possible to send. This is
10    /// either our issue when you were doing something normal, or you were
11    /// sending invalid stuff, like a SSO login on a normal logged in character
12    InvalidRequest(&'static str),
13    /// The server replied with an empty response. This could have a range of
14    /// reasons. Could be a bad request, not logged in, or something else
15    EmptyResponse,
16    /// There was some error encountered when sending data to the server. Most
17    /// likely the server, or your connection is down
18    ConnectionError,
19    /// Whatever the server send back was invalid. Could be because of features
20    /// not yet supported, or a bug in the API
21    ParsingError(&'static str, String),
22    /// The server responded with an error. If you are already logged in, this
23    /// is likely recoverable,  i.e you are able to reuse your session. You
24    /// should just not resend the same command, as the server had some error
25    /// with it. Most likely that you were not allowed to do your action (spend
26    /// money you don't have, etc.)
27    ServerError(String),
28    /// The server version is newer, than the limit set in the server
29    /// communication
30    UnsupportedVersion(u32),
31    /// The server responded with a response, that was too short
32    TooShortResponse {
33        /// The name of the item, that was accessed
34        name: &'static str,
35        /// The position at which the array access failed
36        pos: usize,
37        /// The full array in debug print
38        array: String,
39    },
40}
41
42impl Error for SFError {
43    fn source(&self) -> Option<&(dyn Error + 'static)> {
44        None
45    }
46
47    fn description(&self) -> &'static str {
48        "description() is deprecated; use Display"
49    }
50
51    fn cause(&self) -> Option<&dyn Error> {
52        self.source()
53    }
54}
55
56impl Display for SFError {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        match self {
59            SFError::InvalidRequest(msg) => f.write_fmt(format_args!(
60                "Tried to send an invalid request: {msg}"
61            )),
62            SFError::EmptyResponse => {
63                f.write_str("Received an empty response from the server")
64            }
65            SFError::ConnectionError => {
66                f.write_str("Could not communicate with the server")
67            }
68            SFError::ParsingError(name, value) => f.write_fmt(format_args!(
69                "Error parsing the server response because {name} had an \
70                 unexpected value of: {value}"
71            )),
72            SFError::ServerError(e) => {
73                f.write_fmt(format_args!("Server responded with error: {e}"))
74            }
75            SFError::UnsupportedVersion(v) => f.write_fmt(format_args!(
76                "The server version {v} is not supported"
77            )),
78            SFError::TooShortResponse { name, pos, array } => {
79                f.write_fmt(format_args!(
80                    "Tried to access the response for {name} at [{pos}] , but \
81                     the response is too short. The response is: {array}"
82                ))
83            }
84        }
85    }
86}