Skip to main content

sdp_request_client/
error.rs

1use thiserror::Error;
2
3/// SDP API error codes
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum SdpErrorCode {
6    Success = 2000,
7    InvalidValue = 4001,
8    Forbidden = 4002,
9    ClosureRuleViolation = 4003,
10    Internal = 4004,
11    ReferenceExists = 4005,
12    NotFound = 4007,
13    NotUnique = 4008,
14    NonEditableField = 4009,
15    InternalField = 4010,
16    NoSuchField = 4011,
17    MissingMandatoryField = 4012,
18    UnsupportedContentType = 4013,
19    ReadOnlyField = 4014,
20    RateLimitExceeded = 4015,
21    AlreadyInTrash = 4016,
22    NotInTrash = 4017,
23    LicenseRestriction = 7001,
24    Unknown = 0,
25}
26
27impl From<u32> for SdpErrorCode {
28    fn from(code: u32) -> Self {
29        match code {
30            2000 => SdpErrorCode::Success,
31            4001 => SdpErrorCode::InvalidValue,
32            4002 => SdpErrorCode::Forbidden,
33            4003 => SdpErrorCode::ClosureRuleViolation,
34            4004 => SdpErrorCode::Internal,
35            4005 => SdpErrorCode::ReferenceExists,
36            4007 => SdpErrorCode::NotFound,
37            4008 => SdpErrorCode::NotUnique,
38            4009 => SdpErrorCode::NonEditableField,
39            4010 => SdpErrorCode::InternalField,
40            4011 => SdpErrorCode::NoSuchField,
41            4012 => SdpErrorCode::MissingMandatoryField,
42            4013 => SdpErrorCode::UnsupportedContentType,
43            4014 => SdpErrorCode::ReadOnlyField,
44            4015 => SdpErrorCode::RateLimitExceeded,
45            4016 => SdpErrorCode::AlreadyInTrash,
46            4017 => SdpErrorCode::NotInTrash,
47            7001 => SdpErrorCode::LicenseRestriction,
48            _ => SdpErrorCode::Unknown,
49        }
50    }
51}
52
53#[derive(Debug, Error)]
54pub enum Error {
55    #[error("HTTP error: {0}")]
56    Http(#[from] reqwest::Error),
57    #[error("Authentication failed: invalid or expired token")]
58    Unauthorized,
59    #[error("Permission denied: {0}")]
60    Forbidden(String),
61    #[error("Resource not found: {0}")]
62    NotFound(String),
63    #[error("Invalid value: {0}")]
64    InvalidValue(String),
65    #[error("Resource already exists (not unique): {0}")]
66    NotUnique(String),
67    #[error("Cannot delete: resource is referenced elsewhere")]
68    ReferenceExists,
69    #[error("Missing mandatory field: {0}")]
70    MissingField(String),
71    #[error("Field is not editable: {0}")]
72    NotEditable(String),
73    #[error("Field does not exist: {0}")]
74    NoSuchField(String),
75    #[error("Closure rule violation: {0}")]
76    ClosureRuleViolation(String),
77    #[error("Rate limit exceeded")]
78    RateLimited,
79    #[error("License restriction: operation not allowed")]
80    LicenseRestricted,
81    #[error("SDP internal error")]
82    Internal,
83    #[error("Serialization error: {0}")]
84    Serialization(#[from] serde_json::Error),
85    #[error("URL parsing error: {0}")]
86    UrlParse(#[from] url::ParseError),
87    #[error("Form encoding error: {0}")]
88    FormEncoding(#[from] serde_urlencoded::ser::Error),
89    #[error("SDP error (code {code}): {message}")]
90    Sdp { code: u32, message: String },
91    #[error("{0}")]
92    Other(String),
93}
94
95impl Error {
96    /// Create an error from SDP response status code and message
97    pub fn from_sdp(code: u32, message: String, field: Option<String>) -> Self {
98        let field_info = field.clone().unwrap_or_else(|| message.clone());
99
100        match SdpErrorCode::from(code) {
101            SdpErrorCode::InvalidValue => Error::InvalidValue(field_info),
102            SdpErrorCode::Forbidden => Error::Forbidden(field_info),
103            SdpErrorCode::ClosureRuleViolation => Error::ClosureRuleViolation(field_info),
104            SdpErrorCode::Internal => Error::Internal,
105            SdpErrorCode::ReferenceExists => Error::ReferenceExists,
106            SdpErrorCode::NotFound => Error::NotFound(field_info),
107            SdpErrorCode::NotUnique => Error::NotUnique(field_info),
108            SdpErrorCode::NonEditableField | SdpErrorCode::ReadOnlyField => {
109                Error::NotEditable(field_info)
110            }
111            SdpErrorCode::InternalField => {
112                Error::NotEditable(format!("internal field: {}", field_info))
113            }
114            SdpErrorCode::NoSuchField => Error::NoSuchField(field_info),
115            SdpErrorCode::MissingMandatoryField => Error::MissingField(field_info),
116            SdpErrorCode::RateLimitExceeded => Error::RateLimited,
117            SdpErrorCode::LicenseRestriction => Error::LicenseRestricted,
118            SdpErrorCode::AlreadyInTrash | SdpErrorCode::NotInTrash => Error::Sdp { code, message },
119            SdpErrorCode::UnsupportedContentType => Error::Sdp { code, message },
120            SdpErrorCode::Success => Error::Other("Unexpected success code in error path".into()),
121            SdpErrorCode::Unknown => Error::Sdp { code, message },
122        }
123    }
124}