@@ -79,21 +79,21 @@ class WebvpnProxy {
79
79
}
80
80
}
81
81
82
- // Check if we have logged in to WebVPN, returns false if we haven't
83
- static bool checkResponse (Response <dynamic > response) {
82
+ // Check if we should login to WebVPN. Return `true` if we should login now.
83
+ static bool isResponseRequiringLogin (Response <dynamic > response) {
84
84
// When 302 is raised when the method is `POST`, it means that we haven't logged in
85
85
if (response.requestOptions.method == "POST" &&
86
86
response.statusCode == 302 &&
87
87
response.headers['location' ] != null &&
88
88
response.headers['location' ]! .isNotEmpty) {
89
- return false ;
89
+ return true ;
90
90
}
91
91
92
92
if (response.realUri.toString ().startsWith (WEBVPN_LOGIN_URL )) {
93
- return false ;
93
+ return true ;
94
94
}
95
95
96
- return true ;
96
+ return false ;
97
97
}
98
98
99
99
/// Bind WebVPN proxy to a person info so that it updates automatically when [personInfo] changes.
@@ -284,19 +284,37 @@ class WebvpnProxy {
284
284
await loginWebvpn (dio);
285
285
286
286
// First attempt
287
- Response <T > response = await dio.fetch <T >(options);
288
- if (checkResponse (response)) {
289
- return response;
287
+ try {
288
+ Response <T > response = await DioUtils .fetchWithJsonError (dio, options);
289
+ if (! isResponseRequiringLogin (response)) {
290
+ return response;
291
+ }
292
+ } on DioException catch (e) {
293
+ if (e.response == null ) {
294
+ rethrow ;
295
+ }
296
+ if (! isResponseRequiringLogin (e.response! )) {
297
+ rethrow ;
298
+ }
290
299
}
291
300
292
301
// Re-login
293
302
isLoggedIn = false ;
294
303
await loginWebvpn (dio);
295
304
296
305
// Second attempt
297
- response = await dio.fetch <T >(options);
298
- if (checkResponse (response)) {
299
- return response;
306
+ try {
307
+ Response <T > response = await DioUtils .fetchWithJsonError (dio, options);
308
+ if (! isResponseRequiringLogin (response)) {
309
+ return response;
310
+ }
311
+ } on DioException catch (e) {
312
+ if (e.response == null ) {
313
+ rethrow ;
314
+ }
315
+ if (! isResponseRequiringLogin (e.response! )) {
316
+ rethrow ;
317
+ }
300
318
}
301
319
302
320
// All attempts failed
0 commit comments