ironshield/error.rs
1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum CliError {
5 /// API-specific errors with status
6 /// code and message from the server.
7 #[error("API error ({status}): {message}")]
8 Api {
9 /// HTTP status code returned by the API.
10 status: u16,
11 /// Error message from the API response.
12 message: String
13 },
14 /// Configuration-related errors
15 /// (invalid settings, missing files, etc.).
16 #[error("Configuration error: {0}")]
17 Config(String),
18 /// Challenge processing errors
19 /// (solving, verification, etc.).
20 #[error("Challenge processing error: {0}")]
21 Challenge(String),
22 /// File system and I/O errors.
23 #[error("IO error: {0}")]
24 Io(#[from] std::io::Error),
25 /// Network communication errors from the HTTP client.
26 #[error("Network error: {0}")]
27 Network(#[from] reqwest::Error),
28 /// JSON parsing and serialization errors.
29 #[error("Parsing error: {0}")]
30 Parse(#[from] serde_json::Error),
31 /// TOML configuration file parsing errors.
32 #[error("TOML parsing error: {0}")]
33 Toml(#[from] toml::de::Error),
34}
35
36impl CliError {
37 /// # Arguments
38 /// * `status`: The HTTP status code from the API
39 /// response.
40 /// * `message`: The error message that corresponds
41 /// to the API error.
42 ///
43 /// # Returns
44 /// * `Self`: A new `CliError::Api` variant.
45 pub fn api_error(
46 status: u16,
47 message: impl Into<String>
48 ) -> Self {
49 Self::Api { status, message: message.into() }
50 }
51
52 /// # Arguments
53 /// * `message`: The error message that corresponds
54 /// to the config error.
55 ///
56 /// # Returns
57 /// * `Self`: A new `CliError::Config` variant.
58 pub fn config_error(
59 message: impl Into<String>
60 ) -> Self {
61 Self::Config(message.into())
62 }
63
64 /// # Arguments
65 /// * `message`: The error message that corresponds
66 /// to the challenge error.
67 ///
68 /// # Returns
69 /// * `Self`: A new `CliError::Challenge` variant.
70 pub fn challenge_error(
71 message: impl Into<String>
72 ) -> Self {
73 Self::Challenge(message.into())
74 }
75}