1
1
//! Errors
2
2
3
+ use crate :: { AccessToken , RefreshToken } ;
4
+
3
5
/// General errors for talking with twitch, used in [`AppAccessToken::get_app_access_token`](crate::tokens::AppAccessToken::get_app_access_token)
4
6
#[ allow( missing_docs) ]
5
7
#[ derive( thiserror:: Error , Debug , displaydoc:: Display ) ]
@@ -12,7 +14,7 @@ pub enum AppAccessTokenError<RE: std::error::Error + Send + Sync + 'static> {
12
14
RequestParseError ( #[ from] crate :: RequestParseError ) ,
13
15
}
14
16
15
- /// Errors for [AccessToken::validate_token][crate::AccessTokenRef::validate_token] and [UserToken::from_response][crate::tokens::UserToken::from_response]
17
+ /// Errors for [AccessToken::validate_token][crate::AccessTokenRef::validate_token]
16
18
#[ derive( thiserror:: Error , Debug , displaydoc:: Display ) ]
17
19
#[ non_exhaustive]
18
20
pub enum ValidationError < RE : std:: error:: Error + Send + Sync + ' static > {
@@ -38,15 +40,91 @@ impl ValidationError<std::convert::Infallible> {
38
40
}
39
41
}
40
42
43
+ /// Errors for [`UserToken::new`][crate::tokens::UserToken::new], [`UserToken::from_token`][crate::tokens::UserToken::from_token], [`UserToken::from_existing`][crate::tokens::UserToken::from_existing] and [`UserToken::from_response`][crate::tokens::UserToken::from_response]
44
+ #[ derive( thiserror:: Error , Debug ) ]
45
+ #[ error( "creation of token failed" ) ]
46
+ pub struct CreationError < RE : std:: error:: Error + Send + Sync + ' static > {
47
+ /// Access token passed to the function
48
+ pub access_token : AccessToken ,
49
+ /// Refresh token passed to the function
50
+ pub refresh_token : Option < RefreshToken > ,
51
+ /// Error validating the token
52
+ #[ source]
53
+ pub error : ValidationError < RE > ,
54
+ }
55
+
56
+ impl < 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
+
41
74
/// Errors for [UserToken::from_refresh_token][crate::UserToken::from_refresh_token] and [UserToken::UserToken::from_existing_or_refresh_token][crate::UserToken::from_existing_or_refresh_token]
42
75
#[ derive( thiserror:: Error , Debug , displaydoc:: Display ) ]
43
76
#[ non_exhaustive]
44
77
#[ cfg( feature = "client" ) ]
45
78
pub enum RetrieveTokenError < RE : std:: error:: Error + Send + Sync + ' static > {
46
79
/// could not validate token
47
- ValidationError ( #[ from] ValidationError < RE > ) ,
80
+ ValidationError {
81
+ /// Error validating the token
82
+ #[ source]
83
+ error : ValidationError < RE > ,
84
+ /// Access token passed to the function
85
+ access_token : AccessToken ,
86
+ /// Refresh token passed to the function
87
+ refresh_token : Option < RefreshToken > ,
88
+ } ,
48
89
/// could not refresh token
49
- RefreshTokenError ( #[ from] RefreshTokenError < RE > ) ,
90
+ RefreshTokenError {
91
+ /// Error refreshing the token
92
+ #[ source]
93
+ error : RefreshTokenError < RE > ,
94
+ /// Refresh token passed to the function
95
+ refresh_token : RefreshToken ,
96
+ } ,
97
+ }
98
+
99
+ #[ cfg( feature = "client" ) ]
100
+ impl < 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" ) ]
119
+ impl CreationError < std:: convert:: Infallible > {
120
+ /// Convert this error from a infallible to another
121
+ 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
+ }
50
128
}
51
129
52
130
/// Errors for [AccessToken::revoke_token][crate::AccessTokenRef::revoke_token]
@@ -96,6 +174,15 @@ pub enum UserTokenExchangeError<RE: std::error::Error + Send + Sync + 'static> {
96
174
ValidationError ( #[ from] ValidationError < RE > ) ,
97
175
}
98
176
177
+ #[ cfg( feature = "client" ) ]
178
+ impl < 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
+
99
186
/// Errors for [ImplicitUserTokenBuilder::get_user_token][crate::tokens::ImplicitUserTokenBuilder::get_user_token]
100
187
#[ derive( thiserror:: Error , Debug , displaydoc:: Display ) ]
101
188
#[ non_exhaustive]
@@ -114,6 +201,16 @@ pub enum ImplicitUserTokenExchangeError<RE: std::error::Error + Send + Sync + 's
114
201
/// could not get validation for token
115
202
ValidationError ( #[ from] ValidationError < RE > ) ,
116
203
}
204
+
205
+ #[ cfg( feature = "client" ) ]
206
+ impl < 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
+
117
214
/// Errors for [`DeviceUserTokenBuilder`][crate::tokens::DeviceUserTokenBuilder]
118
215
#[ derive( thiserror:: Error , Debug , displaydoc:: Display ) ]
119
216
#[ non_exhaustive]
@@ -147,3 +244,12 @@ impl<RE: std::error::Error + Send + Sync + 'static> DeviceUserTokenExchangeError
147
244
) if message == "authorization_pending" )
148
245
}
149
246
}
247
+
248
+ #[ cfg( feature = "client" ) ]
249
+ impl < 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
+ }
0 commit comments