fix(apps): improve ApiInterceptor retry and error handling
This commit is contained in:
@@ -19,6 +19,7 @@ class ApiClient {
|
|||||||
_dio.interceptors.add(
|
_dio.interceptors.add(
|
||||||
ApiInterceptor(
|
ApiInterceptor(
|
||||||
tokenStorage: _tokenStorage,
|
tokenStorage: _tokenStorage,
|
||||||
|
dio: _dio,
|
||||||
onTokenRefresh: _handleTokenRefresh,
|
onTokenRefresh: _handleTokenRefresh,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -40,7 +41,7 @@ class ApiClient {
|
|||||||
Future<Response<T>> get<T>(String path, {Options? options}) async {
|
Future<Response<T>> get<T>(String path, {Options? options}) async {
|
||||||
try {
|
try {
|
||||||
return await _dio.get<T>(path, options: options);
|
return await _dio.get<T>(path, options: options);
|
||||||
} catch (e) {
|
} on DioException catch (e) {
|
||||||
throw ApiException.fromDioError(e);
|
throw ApiException.fromDioError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,7 +53,7 @@ class ApiClient {
|
|||||||
}) async {
|
}) async {
|
||||||
try {
|
try {
|
||||||
return await _dio.post<T>(path, data: data, options: options);
|
return await _dio.post<T>(path, data: data, options: options);
|
||||||
} catch (e) {
|
} on DioException catch (e) {
|
||||||
throw ApiException.fromDioError(e);
|
throw ApiException.fromDioError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,13 @@ import '../storage/token_storage.dart';
|
|||||||
class ApiInterceptor extends Interceptor {
|
class ApiInterceptor extends Interceptor {
|
||||||
final TokenStorage tokenStorage;
|
final TokenStorage tokenStorage;
|
||||||
final Future<bool> Function()? onTokenRefresh;
|
final Future<bool> Function()? onTokenRefresh;
|
||||||
|
final Dio dio;
|
||||||
|
|
||||||
ApiInterceptor({required this.tokenStorage, this.onTokenRefresh});
|
ApiInterceptor({
|
||||||
|
required this.tokenStorage,
|
||||||
|
required this.dio,
|
||||||
|
this.onTokenRefresh,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onRequest(
|
void onRequest(
|
||||||
@@ -28,10 +33,12 @@ class ApiInterceptor extends Interceptor {
|
|||||||
if (token != null) {
|
if (token != null) {
|
||||||
err.requestOptions.headers['Authorization'] = 'Bearer $token';
|
err.requestOptions.headers['Authorization'] = 'Bearer $token';
|
||||||
try {
|
try {
|
||||||
final response = await Dio().fetch(err.requestOptions);
|
final response = await dio.fetch(err.requestOptions);
|
||||||
handler.resolve(response);
|
handler.resolve(response);
|
||||||
return;
|
return;
|
||||||
} catch (_) {}
|
} on DioException {
|
||||||
|
// Retry failed, proceed with original error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user