handleNetworkError function

Future<NetworkResponse> handleNetworkError({
  1. required Future<NetworkResponse> proccess(),
  2. dynamic builder(
    1. DioException
    )?,
})

Implementation

Future<NetworkResponse> handleNetworkError({
  required Future<NetworkResponse> Function() proccess,
  Function(DioException)? builder,
}) async {
  bool isDisconnect = await WifiService.isDisconnectWhenCallApi();
  if (isDisconnect) {
    if (networkOptions.errorDisconnectCallback != null) {
      networkOptions.errorDisconnectCallback!();
    }
    return NetworkResponse.withDisconnect();
  }
  try {
    return await proccess();
  } on DioException catch (e, stackTrace) {
    if (builder != null) {
      var result = builder.call(e);
      if (result is NetworkResponse) return result;
    }
    if (e.type == DioExceptionType.connectionTimeout ||
        e.type == DioExceptionType.connectionError) {
      return NetworkResponse.withDisconnect();
    }
    return NetworkResponse.withErrorRequest(e, stackTrace: stackTrace);
  } on BearerTokenRequiredException catch (e) {
    return NetworkResponse.bearerTokenRequired(e);
  } catch (e, stackTrace) {
    return NetworkResponse.withErrorConvert(e, stackTrace: stackTrace);
  }
}