hyperdb_api_salesforce/
error.rs1use std::fmt;
7
8pub type SalesforceAuthResult<T> = Result<T, SalesforceAuthError>;
10
11#[derive(Debug)]
14pub enum SalesforceAuthError {
15 Config(String),
17
18 PrivateKey(String),
20
21 Jwt(String),
23
24 Http(String),
26
27 Authorization {
29 error_code: String,
31 error_description: String,
33 },
34
35 TokenExchange(String),
37
38 TokenParse(String),
40
41 TokenExpired,
43
44 Io(String),
46}
47
48impl fmt::Display for SalesforceAuthError {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 match self {
51 SalesforceAuthError::Config(msg) => write!(f, "configuration error: {msg}"),
52 SalesforceAuthError::PrivateKey(msg) => write!(f, "private key error: {msg}"),
53 SalesforceAuthError::Jwt(msg) => write!(f, "JWT assertion error: {msg}"),
54 SalesforceAuthError::Http(msg) => write!(f, "HTTP error: {msg}"),
55 SalesforceAuthError::Authorization {
56 error_code,
57 error_description,
58 } => write!(
59 f,
60 "authorization failed: {error_code} - {error_description}"
61 ),
62 SalesforceAuthError::TokenExchange(msg) => {
63 write!(f, "DC JWT exchange failed: {msg}")
64 }
65 SalesforceAuthError::TokenParse(msg) => write!(f, "token parse error: {msg}"),
66 SalesforceAuthError::TokenExpired => write!(f, "DC JWT has expired"),
67 SalesforceAuthError::Io(msg) => write!(f, "I/O error: {msg}"),
68 }
69 }
70}
71
72impl std::error::Error for SalesforceAuthError {}
73
74impl From<reqwest::Error> for SalesforceAuthError {
75 fn from(err: reqwest::Error) -> Self {
76 SalesforceAuthError::Http(err.to_string())
77 }
78}
79
80impl From<jsonwebtoken::errors::Error> for SalesforceAuthError {
81 fn from(err: jsonwebtoken::errors::Error) -> Self {
82 SalesforceAuthError::Jwt(err.to_string())
83 }
84}
85
86impl From<rsa::pkcs8::Error> for SalesforceAuthError {
87 fn from(err: rsa::pkcs8::Error) -> Self {
88 SalesforceAuthError::PrivateKey(err.to_string())
89 }
90}
91
92impl From<url::ParseError> for SalesforceAuthError {
93 fn from(err: url::ParseError) -> Self {
94 SalesforceAuthError::Config(format!("invalid URL: {err}"))
95 }
96}
97
98impl From<serde_json::Error> for SalesforceAuthError {
99 fn from(err: serde_json::Error) -> Self {
100 SalesforceAuthError::TokenParse(err.to_string())
101 }
102}