torn_api/
lib.rs

1use thiserror::Error;
2
3pub mod executor;
4#[cfg(feature = "models")]
5pub mod models;
6#[cfg(feature = "requests")]
7pub mod parameters;
8pub mod request;
9#[cfg(feature = "scopes")]
10pub mod scopes;
11
12#[derive(Debug, Error, Clone, PartialEq, Eq)]
13pub enum ApiError {
14    #[error("Unhandled error, should not occur")]
15    Unknown,
16    #[error("Private key is empty in current request")]
17    KeyIsEmpty,
18    #[error("Private key is wrong/incorrect format")]
19    IncorrectKey,
20    #[error("Requesting an incorrect basic type")]
21    WrongType,
22    #[error("Requesting incorect selection fields")]
23    WrongFields,
24    #[error(
25        "Requests are blocked for a small period of time because of too many requests per user"
26    )]
27    TooManyRequest,
28    #[error("Wrong ID value")]
29    IncorrectId,
30    #[error("A requested selection is private")]
31    IncorrectIdEntityRelation,
32    #[error("Current IP is banned for a small period of time because of abuse")]
33    IpBlock,
34    #[error("Api system is currently disabled")]
35    ApiDisabled,
36    #[error("Current key can't be used because owner is in federal jail")]
37    KeyOwnerInFederalJail,
38    #[error("You can only change your API key once every 60 seconds")]
39    KeyChange,
40    #[error("Error reading key from Database")]
41    KeyRead,
42    #[error("The key owner hasn't been online for more than 7 days")]
43    TemporaryInactivity,
44    #[error("Too many records have been pulled today by this user from our cloud services")]
45    DailyReadLimit,
46    #[error("An error code specifically for testing purposes that has no dedicated meaning")]
47    TemporaryError,
48    #[error("A selection is being called of which this key does not have permission to access")]
49    InsufficientAccessLevel,
50    #[error("Backend error occurred, please try again")]
51    Backend,
52    #[error("API key has been paused by the owner")]
53    Paused,
54    #[error("Must be migrated to crimes 2.0")]
55    NotMigratedCrimes,
56    #[error("Race not yet finished")]
57    RaceNotFinished,
58    #[error("Wrong cat value")]
59    IncorrectCategory,
60    #[error("This selection is only available in API v1")]
61    OnlyInV1,
62    #[error("This selection is only available in API v2")]
63    OnlyInV2,
64    #[error("Closed temporarily")]
65    ClosedTemporarily,
66    #[error("Other: {message}")]
67    Other { code: u16, message: String },
68}
69
70impl ApiError {
71    pub fn new(code: u16, message: &str) -> Self {
72        match code {
73            0 => Self::Unknown,
74            1 => Self::KeyIsEmpty,
75            2 => Self::IncorrectKey,
76            3 => Self::WrongType,
77            4 => Self::WrongFields,
78            5 => Self::TooManyRequest,
79            6 => Self::IncorrectId,
80            7 => Self::IncorrectIdEntityRelation,
81            8 => Self::IpBlock,
82            9 => Self::ApiDisabled,
83            10 => Self::KeyOwnerInFederalJail,
84            11 => Self::KeyChange,
85            12 => Self::KeyRead,
86            13 => Self::TemporaryInactivity,
87            14 => Self::DailyReadLimit,
88            15 => Self::TemporaryError,
89            16 => Self::InsufficientAccessLevel,
90            17 => Self::Backend,
91            18 => Self::Paused,
92            19 => Self::NotMigratedCrimes,
93            20 => Self::RaceNotFinished,
94            21 => Self::IncorrectCategory,
95            22 => Self::OnlyInV1,
96            23 => Self::OnlyInV2,
97            24 => Self::ClosedTemporarily,
98            other => Self::Other {
99                code: other,
100                message: message.to_owned(),
101            },
102        }
103    }
104
105    pub fn code(&self) -> u16 {
106        match self {
107            Self::Unknown => 0,
108            Self::KeyIsEmpty => 1,
109            Self::IncorrectKey => 2,
110            Self::WrongType => 3,
111            Self::WrongFields => 4,
112            Self::TooManyRequest => 5,
113            Self::IncorrectId => 6,
114            Self::IncorrectIdEntityRelation => 7,
115            Self::IpBlock => 8,
116            Self::ApiDisabled => 9,
117            Self::KeyOwnerInFederalJail => 10,
118            Self::KeyChange => 11,
119            Self::KeyRead => 12,
120            Self::TemporaryInactivity => 13,
121            Self::DailyReadLimit => 14,
122            Self::TemporaryError => 15,
123            Self::InsufficientAccessLevel => 16,
124            Self::Backend => 17,
125            Self::Paused => 18,
126            Self::NotMigratedCrimes => 19,
127            Self::RaceNotFinished => 20,
128            Self::IncorrectCategory => 21,
129            Self::OnlyInV1 => 22,
130            Self::OnlyInV2 => 23,
131            Self::ClosedTemporarily => 24,
132            Self::Other { code, .. } => *code,
133        }
134    }
135}
136
137#[derive(Debug, Error, PartialEq, Eq)]
138pub enum ParameterError {
139    #[error("value `{value}` is out of range for parameter {name}")]
140    OutOfRange { name: &'static str, value: i32 },
141}
142
143#[derive(Debug, Error)]
144pub enum Error {
145    #[error("Parameter error: {0}")]
146    Parameter(#[from] ParameterError),
147    #[error("Network error: {0}")]
148    Network(#[from] reqwest::Error),
149    #[error("Parsing error: {0}")]
150    Parsing(#[from] serde_json::Error),
151    #[error("Api error: {0}")]
152    Api(#[from] ApiError),
153}