diff --git a/example/pubspec.lock b/example/pubspec.lock index 76be80c..9f19e07 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -395,7 +395,7 @@ packages: path: ".." relative: true source: path - version: "4.1.0" + version: "5.0.1" vm_service: dependency: transitive description: diff --git a/lib/src/mixin/network_manager_core_operation.dart b/lib/src/mixin/network_manager_core_operation.dart index 77af5b7..fed2d91 100644 --- a/lib/src/mixin/network_manager_core_operation.dart +++ b/lib/src/mixin/network_manager_core_operation.dart @@ -33,8 +33,7 @@ mixin NetworkManagerCoreOperation> { CancelToken? cancelToken, bool? forceUpdateDecode, }) async { - if (!isErrorDialog || - _noNetworkTryCount == NetworkManagerParameters.maxRetryCount) { + if (!isErrorDialog || _noNetworkTryCount == parameters.maxRetryCount) { return onError.call(error); } @@ -89,8 +88,7 @@ mixin NetworkManagerCoreOperation> { CancelToken? cancelToken, bool? forceUpdateDecode, }) async { - if (!isErrorDialog || - _noNetworkTryCount == NetworkManagerParameters.maxRetryCount) { + if (!isErrorDialog || _noNetworkTryCount == parameters.maxRetryCount) { return onError.call(error); } diff --git a/lib/src/mixin/network_manager_error_interceptor.dart b/lib/src/mixin/network_manager_error_interceptor.dart index 4a06b08..11fe21b 100644 --- a/lib/src/mixin/network_manager_error_interceptor.dart +++ b/lib/src/mixin/network_manager_error_interceptor.dart @@ -44,7 +44,7 @@ mixin NetworkManagerErrorInterceptor { () => _createNewRequest(error), onRetry: (_) async => error = await _createError(parameters, exception), - maxAttempts: NetworkManagerParameters.maxRetryCount, + maxAttempts: parameters.maxRetryCount, retryIf: _retryIf, ); // onResponseParse is null, then return response @@ -53,10 +53,17 @@ mixin NetworkManagerErrorInterceptor { } // Call onResponseParse callback and return response return handler.resolve(parameters.onResponseParse!(response)); - } catch (_) { + } catch (err) { /// cancel request & call onRefreshFail callback and unlock error.requestOptions.cancelToken?.cancel(); parameters.onRefreshFail?.call(); + + /// If error is DioException, then return error + if (err is DioException) { + return handler.next(err); + } + + /// If error is not DioException, then return error return handler.next(exception); } }, @@ -78,6 +85,7 @@ mixin NetworkManagerErrorInterceptor { isEnableLogger: params.isEnableLogger, isEnableTest: params.isEnableTest, options: parameters.baseOptions, + maxRetryCount: params.maxRetryCount, ), ); } diff --git a/lib/src/mixin/network_manager_parameters.dart b/lib/src/mixin/network_manager_parameters.dart index 3727f79..9cb2765 100644 --- a/lib/src/mixin/network_manager_parameters.dart +++ b/lib/src/mixin/network_manager_parameters.dart @@ -16,8 +16,6 @@ typedef OnReply = Response Function( class NetworkManagerParameters extends Equatable { final VoidCallback? onRefreshFail; - static const int maxRetryCount = 3; - final IFileManager? fileManager; final bool? isEnableTest; @@ -38,6 +36,8 @@ class NetworkManagerParameters extends Equatable { final OnReply? onResponseParse; + final int maxRetryCount; + const NetworkManagerParameters({ required BaseOptions options, this.onRefreshFail, @@ -50,6 +50,7 @@ class NetworkManagerParameters extends Equatable { this.interceptor, this.onRefreshToken, this.onResponseParse, + this.maxRetryCount = 3, }) : baseOptions = options; @override diff --git a/lib/src/network_manager.dart b/lib/src/network_manager.dart index c3f68e1..c0d1aec 100644 --- a/lib/src/network_manager.dart +++ b/lib/src/network_manager.dart @@ -42,6 +42,7 @@ class NetworkManager> extends dio.DioMixin IFileManager? fileManager, Interceptor? interceptor, OnReply? onReply, + int maxRetryCount = 3, }) { parameters = NetworkManagerParameters( options: options, @@ -54,6 +55,7 @@ class NetworkManager> extends dio.DioMixin onRefreshToken: onRefreshToken, onRefreshFail: onRefreshFail, onResponseParse: onReply, + maxRetryCount: maxRetryCount, ); _setup(); } diff --git a/test/unit/common/mock_network_manager.dart b/test/unit/common/mock_network_manager.dart index 7ff5026..77cd9c6 100644 --- a/test/unit/common/mock_network_manager.dart +++ b/test/unit/common/mock_network_manager.dart @@ -18,6 +18,7 @@ final class MockErrorCustomNetworkManager extends NetworkManager { : super( options: BaseOptions(baseUrl: baseUrl), isEnableTest: true, + maxRetryCount: 5, onRefreshFail: () => print('onRefreshFail Triggered'), onRefreshToken: (e, networkManager) async { await onRefresh.call(); diff --git a/test/unit/network_manager_error_test.dart b/test/unit/network_manager_error_test.dart index db20c4a..cfcf3c5 100644 --- a/test/unit/network_manager_error_test.dart +++ b/test/unit/network_manager_error_test.dart @@ -22,7 +22,7 @@ void main() { method: RequestType.GET, ); stopServer(); - expect(retryCount, 3); + expect(retryCount, 5); expect(response.error?.statusCode, 401); }); @@ -57,7 +57,7 @@ void main() { ), ]); stopServer(); - expect(retryCount, 3); + expect(retryCount, 5); expect(cancelToken.isCancelled, true); });