twitch_oauth2/tokens/
errors.rs1use crate::{AccessToken, RefreshToken};
4
5#[allow(missing_docs)]
7#[derive(thiserror::Error, Debug, displaydoc::Display)]
8#[cfg(feature = "client")]
9#[non_exhaustive]
10pub enum AppAccessTokenError<RE: std::error::Error + Send + Sync + 'static> {
11 Request(#[source] RE),
13 RequestParseError(#[from] crate::RequestParseError),
15}
16
17#[derive(thiserror::Error, Debug, displaydoc::Display)]
19#[non_exhaustive]
20pub enum ValidationError<RE: std::error::Error + Send + Sync + 'static> {
21 NotAuthorized,
23 RequestParseError(#[from] crate::RequestParseError),
25 Request(#[source] RE),
27 InvalidToken(&'static str),
29}
30
31impl ValidationError<std::convert::Infallible> {
32 pub fn into_other<RE: std::error::Error + Send + Sync + 'static>(self) -> ValidationError<RE> {
34 match self {
35 ValidationError::NotAuthorized => ValidationError::NotAuthorized,
36 ValidationError::RequestParseError(e) => ValidationError::RequestParseError(e),
37 ValidationError::InvalidToken(s) => ValidationError::InvalidToken(s),
38 ValidationError::Request(_) => unreachable!(),
39 }
40 }
41}
42
43#[derive(thiserror::Error, Debug)]
45#[error("creation of token failed")]
46pub struct CreationError<RE: std::error::Error + Send + Sync + 'static> {
47 pub access_token: AccessToken,
49 pub refresh_token: Option<RefreshToken>,
51 #[source]
53 pub error: ValidationError<RE>,
54}
55
56impl<RE: std::error::Error + Send + Sync + 'static>
57 From<(AccessToken, Option<RefreshToken>, ValidationError<RE>)> for CreationError<RE>
58{
59 fn from(
60 (access_token, refresh_token, error): (
61 AccessToken,
62 Option<RefreshToken>,
63 ValidationError<RE>,
64 ),
65 ) -> Self {
66 Self {
67 access_token,
68 refresh_token,
69 error,
70 }
71 }
72}
73
74#[derive(thiserror::Error, Debug, displaydoc::Display)]
76#[non_exhaustive]
77#[cfg(feature = "client")]
78pub enum RetrieveTokenError<RE: std::error::Error + Send + Sync + 'static> {
79 ValidationError {
81 #[source]
83 error: ValidationError<RE>,
84 access_token: AccessToken,
86 refresh_token: Option<RefreshToken>,
88 },
89 RefreshTokenError {
91 #[source]
93 error: RefreshTokenError<RE>,
94 refresh_token: RefreshToken,
96 },
97}
98
99#[cfg(feature = "client")]
100impl<RE: std::error::Error + Send + Sync + 'static> From<CreationError<RE>>
101 for RetrieveTokenError<RE>
102{
103 fn from(
104 CreationError {
105 error,
106 access_token,
107 refresh_token,
108 }: CreationError<RE>,
109 ) -> Self {
110 RetrieveTokenError::ValidationError {
111 error,
112 access_token,
113 refresh_token,
114 }
115 }
116}
117
118#[cfg(feature = "client")]
119impl CreationError<std::convert::Infallible> {
120 pub fn into_other<RE: std::error::Error + Send + Sync + 'static>(self) -> CreationError<RE> {
122 CreationError {
123 access_token: self.access_token,
124 refresh_token: self.refresh_token,
125 error: self.error.into_other(),
126 }
127 }
128}
129
130#[allow(missing_docs)]
132#[derive(thiserror::Error, Debug, displaydoc::Display)]
133#[non_exhaustive]
134#[cfg(feature = "client")]
135pub enum RevokeTokenError<RE: std::error::Error + Send + Sync + 'static> {
136 RequestParseError(#[from] crate::RequestParseError),
138 RequestError(#[source] RE),
140}
141
142#[allow(missing_docs)]
144#[derive(thiserror::Error, Debug, displaydoc::Display)]
145#[non_exhaustive]
146#[cfg(feature = "client")]
147pub enum RefreshTokenError<RE: std::error::Error + Send + Sync + 'static> {
148 RequestError(#[source] RE),
150 RequestParseError(#[from] crate::RequestParseError),
152 NoClientSecretFound,
156 NoRefreshToken,
158 NoExpiration,
160}
161
162#[derive(thiserror::Error, Debug, displaydoc::Display)]
164#[non_exhaustive]
165#[cfg(feature = "client")]
166pub enum UserTokenExchangeError<RE: std::error::Error + Send + Sync + 'static> {
167 RequestError(#[source] RE),
169 RequestParseError(#[from] crate::RequestParseError),
171 StateMismatch,
173 ValidationError(#[from] ValidationError<RE>),
175}
176
177#[cfg(feature = "client")]
178impl<RE: std::error::Error + Send + Sync + 'static> From<CreationError<RE>>
179 for UserTokenExchangeError<RE>
180{
181 fn from(value: CreationError<RE>) -> Self {
182 UserTokenExchangeError::ValidationError(value.error)
183 }
184}
185
186#[derive(thiserror::Error, Debug, displaydoc::Display)]
188#[non_exhaustive]
189#[cfg(feature = "client")]
190pub enum ImplicitUserTokenExchangeError<RE: std::error::Error + Send + Sync + 'static> {
191 TwitchError {
194 error: Option<String>,
196 description: Option<String>,
198 },
199 StateMismatch,
201 ValidationError(#[from] ValidationError<RE>),
203}
204
205#[cfg(feature = "client")]
206impl<RE: std::error::Error + Send + Sync + 'static> From<CreationError<RE>>
207 for ImplicitUserTokenExchangeError<RE>
208{
209 fn from(value: CreationError<RE>) -> Self {
210 ImplicitUserTokenExchangeError::ValidationError(value.error)
211 }
212}
213
214#[derive(thiserror::Error, Debug, displaydoc::Display)]
216#[non_exhaustive]
217#[cfg(feature = "client")]
218pub enum DeviceUserTokenExchangeError<RE: std::error::Error + Send + Sync + 'static> {
219 DeviceExchangeRequestError(#[source] RE),
221 DeviceExchangeParseError(#[source] crate::RequestParseError),
223 TokenRequestError(#[source] RE),
225 TokenParseError(#[source] crate::RequestParseError),
227 ValidationError(#[from] ValidationError<RE>),
229 NoDeviceCode,
231 Expired,
233}
234
235#[cfg(feature = "client")]
236impl<RE: std::error::Error + Send + Sync + 'static> DeviceUserTokenExchangeError<RE> {
237 pub fn is_pending(&self) -> bool {
239 matches!(self, DeviceUserTokenExchangeError::TokenParseError(
240 crate::RequestParseError::TwitchError(crate::id::TwitchTokenErrorResponse {
241 message,
242 ..
243 }),
244 ) if message == "authorization_pending")
245 }
246}
247
248#[cfg(feature = "client")]
249impl<RE: std::error::Error + Send + Sync + 'static> From<CreationError<RE>>
250 for DeviceUserTokenExchangeError<RE>
251{
252 fn from(value: CreationError<RE>) -> Self {
253 DeviceUserTokenExchangeError::ValidationError(value.error)
254 }
255}