1use std::{error::Error as StdError, fmt};
2
3#[non_exhaustive]
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
6pub enum ErrorCategory {
7 Configuration,
8 Protocol,
9 Login,
10 Handoff,
11 Authentication,
12 Session,
13 Refresh,
14 RateLimited,
15 Transport,
16 Timeout,
17 Cancelled,
18 Indeterminate,
19}
20
21#[non_exhaustive]
23#[derive(Clone, Copy, Debug, Eq, PartialEq)]
24pub enum RetryPolicy {
25 Never,
26 SafeAfterDelay,
27 ApplicationDecision,
28}
29
30#[non_exhaustive]
32#[derive(Clone, Copy, Debug, Eq, PartialEq)]
33pub enum LocalAction {
34 None,
35 DiscardPendingLogin,
36 QuarantinePendingLogin,
37 ClearCredentials,
38 QuarantineCredentials,
39 Reauthenticate,
40}
41
42#[derive(Clone, Eq, PartialEq)]
44pub struct Error {
45 category: ErrorCategory,
46 code: String,
47 message: String,
48 request_id: Option<String>,
49 retry: RetryPolicy,
50 action: LocalAction,
51 operation: &'static str,
52 status: Option<u16>,
53 retry_after_seconds: Option<u64>,
54}
55
56impl Error {
57 pub(crate) fn new(
58 category: ErrorCategory,
59 code: impl Into<String>,
60 message: impl Into<String>,
61 retry: RetryPolicy,
62 action: LocalAction,
63 operation: &'static str,
64 ) -> Self {
65 Self {
66 category,
67 code: code.into(),
68 message: message.into(),
69 request_id: None,
70 retry,
71 action,
72 operation,
73 status: None,
74 retry_after_seconds: None,
75 }
76 }
77
78 pub(crate) fn with_runtime(mut self, status: u16, request_id: Option<String>) -> Self {
79 self.status = Some(status);
80 self.request_id = request_id.filter(|value| value.len() <= 128);
81 self
82 }
83
84 pub(crate) const fn with_retry_after(mut self, retry_after_seconds: u64) -> Self {
85 self.retry_after_seconds = Some(retry_after_seconds);
86 self
87 }
88
89 #[must_use]
90 pub const fn category(&self) -> ErrorCategory {
91 self.category
92 }
93
94 #[must_use]
95 pub fn code(&self) -> &str {
96 &self.code
97 }
98
99 #[must_use]
100 pub fn message(&self) -> &str {
101 &self.message
102 }
103
104 #[must_use]
105 pub fn request_id(&self) -> Option<&str> {
106 self.request_id.as_deref()
107 }
108
109 #[must_use]
110 pub const fn retry_policy(&self) -> RetryPolicy {
111 self.retry
112 }
113
114 #[must_use]
115 pub const fn local_action(&self) -> LocalAction {
116 self.action
117 }
118
119 #[must_use]
120 pub const fn operation(&self) -> &'static str {
121 self.operation
122 }
123
124 #[must_use]
125 pub const fn status(&self) -> Option<u16> {
126 self.status
127 }
128
129 #[must_use]
130 pub const fn retry_after_seconds(&self) -> Option<u64> {
131 self.retry_after_seconds
132 }
133}
134
135impl fmt::Display for Error {
136 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
137 write!(formatter, "{}: {}", self.code, self.message)
138 }
139}
140
141impl fmt::Debug for Error {
142 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
143 formatter
144 .debug_struct("Error")
145 .field("category", &self.category)
146 .field("code", &self.code)
147 .field("message", &self.message)
148 .field("request_id", &self.request_id)
149 .field("retry", &self.retry)
150 .field("action", &self.action)
151 .field("operation", &self.operation)
152 .field("status", &self.status)
153 .field("retry_after_seconds", &self.retry_after_seconds)
154 .finish()
155 }
156}
157
158impl StdError for Error {}
159
160pub(crate) fn configuration(code: &str, message: &str) -> Error {
161 Error::new(
162 ErrorCategory::Configuration,
163 code,
164 message,
165 RetryPolicy::Never,
166 LocalAction::None,
167 "configuration",
168 )
169}
170
171pub(crate) fn protocol(operation: &'static str, code: &str) -> Error {
172 Error::new(
173 ErrorCategory::Protocol,
174 code,
175 "Runtime returned an invalid or incompatible response.",
176 RetryPolicy::Never,
177 LocalAction::None,
178 operation,
179 )
180}