radio_code_calculator/error.rs
1//! Errors returned by the Radio Code Calculator API interface
2//!
3//! Usage:
4//!
5//! ```ignore
6//! if error == RadioErrors::SUCCESS { ... }
7//! ```
8
9use serde_json::Value;
10use thiserror::Error;
11
12/// Errors returned by [`crate::RadioCodeCalculator`] (license, HTTP/JSON, or API error payloads).
13#[derive(Debug, Error)]
14pub enum RadioCodeCalculatorError {
15 /// @var int license key is invalid or expired (also when API key is missing)
16 #[error("invalid license")]
17 InvalidLicense,
18
19 /// API returned `error != SUCCESS` (body includes the `error` field and optional details).
20 #[error("API error: {0:?}")]
21 ApiError(Value),
22
23 /// Network failure, non-JSON response, or unexpected data.
24 #[error("transport error: {0}")]
25 Transport(String),
26}
27
28impl RadioCodeCalculatorError {
29 /// Return the API `error` code when this is [`RadioCodeCalculatorError::ApiError`].
30 pub fn api_error_code(&self) -> Option<i64> {
31 match self {
32 RadioCodeCalculatorError::ApiError(v) => v.get("error").and_then(|e| e.as_i64()),
33 _ => None,
34 }
35 }
36}
37
38/// Errors returned by the Radio Code Calculator API interface
39///
40/// Usage:
41///
42/// if (error === RadioErrors.SUCCESS) { ... }
43pub struct RadioErrors;
44
45impl RadioErrors {
46 /// @var int cannot connect to the Web API interface (network error)
47 pub const ERROR_CONNECTION: i32 = -1;
48
49 /// @var int successful request
50 pub const SUCCESS: i32 = 0;
51
52 /// @var int an error occurred while validating input data (invalid length, format etc.)
53 pub const INVALID_INPUT: i32 = 1;
54
55 /// @var int invalid Web API command (not supported)
56 pub const INVALID_COMMAND: i32 = 2;
57
58 /// @var int radio model is not supported by the calculator
59 pub const INVALID_RADIO_MODEL: i32 = 3;
60
61 /// @var int radio serial number is invalid (invalid format, not matching the expected regex pattern)
62 pub const INVALID_SERIAL_LENGTH: i32 = 4;
63
64 /// @var int radio serial number doesn't match the expected regular expression pattern
65 pub const INVALID_SERIAL_PATTERN: i32 = 5;
66
67 /// @var int radio serial number is not supported by the selected calculator
68 pub const INVALID_SERIAL_NOT_SUPPORTED: i32 = 6;
69
70 /// @var int extra data is invalid (invalid format, not matching the expected regex pattern)
71 pub const INVALID_EXTRA_LENGTH: i32 = 7;
72
73 /// @var int extra data doesn't match the expected regular expression pattern
74 pub const INVALID_EXTRA_PATTERN: i32 = 8;
75
76 /// @var int license key is invalid or expired
77 pub const INVALID_LICENSE: i32 = 100;
78}