Skip to content

Commit

Permalink
Merge pull request #42 from DmitryGaimaldinov/fix_file_not_found
Browse files Browse the repository at this point in the history
Fix file not found error
  • Loading branch information
rodion-m authored Sep 30, 2024
2 parents 44b3bc5 + 910eeee commit f8f97c2
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 158 deletions.
1 change: 0 additions & 1 deletion lib/dio_smart_retry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ library dio_smart_retry;

export 'src/default_retry_evaluator.dart';
export 'src/http_status_codes.dart';
export 'src/multipart_file_recreatable.dart';
export 'src/retry_interceptor.dart';
67 changes: 0 additions & 67 deletions lib/src/multipart_file_recreatable.dart

This file was deleted.

54 changes: 2 additions & 52 deletions lib/src/retry_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import 'dart:async';
import 'package:dio/dio.dart';
import 'package:dio_smart_retry/src/default_retry_evaluator.dart';
import 'package:dio_smart_retry/src/http_status_codes.dart';
import 'package:dio_smart_retry/src/multipart_file_recreatable.dart';
import 'package:dio_smart_retry/src/retry_not_supported_exception.dart';

typedef RetryEvaluator = FutureOr<bool> Function(
DioException error,
Expand Down Expand Up @@ -46,9 +44,6 @@ class RetryInterceptor extends Interceptor {
}
}

static const _multipartRetryHelpLink =
'https://github.com/rodion-m/dio_smart_retry#retry-requests-with-multipartform-data';

/// The original dio
final Dio dio;

Expand Down Expand Up @@ -101,12 +96,6 @@ class RetryInterceptor extends Interceptor {
return true;
}

@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
_printErrorIfRequestHasMultipartFile(options);
super.onRequest(options, handler);
}

@override
Future<dynamic> onError(
DioException err,
Expand Down Expand Up @@ -137,14 +126,7 @@ class RetryInterceptor extends Interceptor {

var requestOptions = err.requestOptions;
if (requestOptions.data is FormData) {
try {
requestOptions = _recreateOptions(err.requestOptions);
} on RetryNotSupportedException catch (e) {
return super.onError(
DioException(requestOptions: requestOptions, error: e),
handler,
);
}
requestOptions = _recreateOptions(err.requestOptions);
}

if (delay != Duration.zero) {
Expand Down Expand Up @@ -179,41 +161,9 @@ class RetryInterceptor extends Interceptor {
);
}
final formData = options.data as FormData;
final newFormData = FormData();
newFormData.fields.addAll(formData.fields);
for (final pair in formData.files) {
final file = pair.value;
if (file is MultipartFileRecreatable) {
newFormData.files.add(MapEntry(pair.key, file.recreate()));
} else {
throw RetryNotSupportedException(
'Use MultipartFileRecreatable class '
'instead of MultipartFile to make retry available. '
'See: $_multipartRetryHelpLink',
);
}
}
final newFormData = formData.clone();
return options.copyWith(data: newFormData);
}

var _multipartFileChecked = false;

void _printErrorIfRequestHasMultipartFile(RequestOptions options) {
if (_multipartFileChecked) return;
if (options.data is FormData) {
final data = options.data as FormData;
if (data.files.any((pair) => pair.value is! MultipartFileRecreatable)) {
final printer = logPrint ?? print;
printer(
'WARNING: Retry is not supported for MultipartFile class. '
'Use MultipartFileRecreatable class '
'instead of MultipartFile to make retry available. '
'See: $_multipartRetryHelpLink',
);
}
}
_multipartFileChecked = true;
}
}

const _kDisableRetryKey = 'ro_disable_retry';
Expand Down
11 changes: 0 additions & 11 deletions lib/src/retry_not_supported_exception.dart

This file was deleted.

29 changes: 2 additions & 27 deletions test/multipart_retry_tests.dart
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
@TestOn('vm')
import 'package:dio/dio.dart';
import 'package:dio_smart_retry/dio_smart_retry.dart';
import 'package:dio_smart_retry/src/retry_not_supported_exception.dart';
import 'package:test/test.dart';

void main() {
test('Retry for MultipartFile throws RetryNotSupportedException', () async {
final dio = Dio();
dynamic exception;
dio.interceptors.add(
RetryInterceptor(
dio: dio,
logPrint: print,
),
);

final formData =
FormData.fromMap({'file': MultipartFile.fromFileSync('README.md')});
try {
await dio.post<dynamic>(
'https://rodion-m.ru/mock/post500.php',
data: formData,
);
} on DioException catch (error) {
exception = error.error;
}

expect(exception is RetryNotSupportedException, true);
});

test('Retry for MultipartFileRecreatable is retrying', () async {
test('Retry for MultipartFile is retrying', () async {
final dio = Dio();
const retries = 2;
final evaluator = DefaultRetryEvaluator(defaultRetryableStatuses);
Expand All @@ -44,7 +19,7 @@ void main() {
);

final formData = FormData.fromMap({
'file': MultipartFileRecreatable.fromFileSync('README.md'),
'file': MultipartFile.fromFileSync('README.md'),
});
try {
await dio.post<dynamic>(
Expand Down

0 comments on commit f8f97c2

Please sign in to comment.