gw2api/
error.rs

1use serde::de;
2use std::fmt::{self, Display};
3
4/// This error is raised whenever an error occurs when calling the Guild Wars 2 API, for example by
5/// trying to access a resource that requires authentication without a valid API key, or trying to
6/// access a non-existent item id.
7///
8/// The most relevant field is the text field describing the error, rest are probably for internal
9/// use by ArenaNet.
10#[derive(Debug, Clone, Deserialize, PartialEq)]
11pub struct ApiError {
12    ///// Error code.
13    //code: Option<u32>,
14    ///// Product where the error concerns.
15    //product: Option<u32>,
16    ///// Which module the error occurred in.
17    //module: Option<u32>,
18    ///// At what line the error occurred.
19    //line: Option<u32>,
20    /// Text describing the error retrieved from the API.
21    text: String,
22}
23
24impl de::Error for ApiError {
25    fn custom<T: std::string::ToString>(msg: T) -> Self {
26        ApiError {
27            text: msg.to_string(),
28        }
29    }
30}
31
32impl Display for ApiError {
33    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
34        formatter.write_str(&self.to_string())
35    }
36}
37
38impl std::error::Error for ApiError {
39    fn description(&self) -> &str {
40        &self.text
41    }
42}
43
44impl ApiError {
45    /// Create a new ApiError from any type T that implements the Display trait.
46    pub fn new(text: impl Display) -> ApiError {
47        ApiError {
48            text: text.to_string(),
49        }
50    }
51
52    /// Returns the description of the error.
53    pub fn description(&self) -> &str {
54        &self.text
55    }
56}