minecraft_uuid/
error.rs

1use std::{error::Error, fmt::Display};
2
3use crate::models::ErrorResponse;
4
5/// Error constructed from an erroneous API
6/// response.
7#[derive(Debug, Clone)]
8pub struct APIError {
9    status_code: u16,
10    status: String,
11    message: String,
12}
13
14impl APIError {
15    /// The HTTP status code of the response.
16    pub fn status_code(&self) -> u16 {
17        self.status_code
18    }
19
20    /// The error status message.
21    pub fn status(&self) -> &str {
22        self.status.as_ref()
23    }
24
25    /// The actual error message with more concise
26    /// error details.
27    pub fn message(&self) -> &str {
28        self.message.as_ref()
29    }
30
31    pub(crate) fn new(status_code: u16, status: &str, message: &str) -> Self {
32        Self {
33            status_code,
34            status: status.to_owned(),
35            message: message.to_owned(),
36        }
37    }
38
39    pub(crate) fn set_status_code(&mut self, status_code: u16) {
40        self.status_code = status_code;
41    }
42}
43
44impl Error for APIError {}
45
46impl Display for APIError {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(
49            f,
50            "{} ({}): {}",
51            self.status_code, self.status, self.message
52        )
53    }
54}
55
56impl From<ErrorResponse> for APIError {
57    fn from(resp: ErrorResponse) -> Self {
58        Self {
59            status_code: 0,
60            message: resp.error_message,
61            status: resp.error,
62        }
63    }
64}