torn_api/
lib.rs

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