attackerkb_api_rs/
error.rs

1//! AttackerKB api error
2
3use serde::{Deserialize, Serialize};
4use std::fmt::{Display, Formatter};
5
6/// All Error enum
7#[derive(Debug, thiserror::Error)]
8pub enum Error {
9  #[error("Invalid AttackerKB API Token: {}", source)]
10  InvalidApiToken {
11    source: reqwest::header::InvalidHeaderValue,
12  },
13
14  #[error("Unable to build reqwest HTTP client: {}", source)]
15  BuildingClient { source: reqwest::Error },
16
17  #[error("Error sending HTTP request: {}", source)]
18  RequestFailed {
19    #[from]
20    source: reqwest::Error,
21  },
22  #[error("Error reading response: {}", source)]
23  ResponseIo { source: reqwest::Error },
24
25  #[error("API Error ({}): {}", .error.status, .error.message)]
26  Api { error: ErrorResponse },
27}
28
29#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Hash)]
30#[serde(untagged)]
31pub enum StatusCode {
32  NonZeroU16(u16),
33  String(String),
34}
35
36impl Display for StatusCode {
37  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
38    let v = match self {
39      StatusCode::NonZeroU16(n) => n.to_string(),
40      StatusCode::String(s) => s.to_string(),
41    };
42    write!(f, "{}", v)
43  }
44}
45
46/// Return [ErrorResponse] when API request encounters an error
47#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
48pub struct ErrorResponse {
49  /// A short description of the error that occurred.
50  /// example: An error occurred while creating the record.
51  pub message: String,
52  /// The HTTP status code of the response.
53  /// example: 500
54  pub status: StatusCode,
55}