Skip to content

Commit

Permalink
🥅 Throw if cancelled before established (#2251)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored Jun 18, 2024
1 parent 97b4830 commit 83c8a3f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ See the [Migration Guide][] for the complete breaking changes list.**
- Split the Web implementation to `package:dio_web_adapter`.
- Add FusedTransformer for improved performance when decoding JSON.
- Improves `InterceptorState.toString()`.
- If the `CancelToken` got canceled before making requests,
throws the exception directly rather than cut actual HTTP requests afterward.

## 5.4.3+1

Expand Down
6 changes: 5 additions & 1 deletion dio/lib/src/dio_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,11 @@ abstract class DioMixin implements Dio {
Options? options,
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
}) {
}) async {
if (cancelToken != null && cancelToken.isCancelled) {
throw cancelToken.cancelError!;
}

final requestOptions = (options ?? Options()).compose(
this.options,
path,
Expand Down
24 changes: 24 additions & 0 deletions dio/test/cancel_token_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,29 @@ void main() {
verify(request.abort()).called(1);
}
});

test('throws if cancelled before making requests', () async {
final cancelToken = CancelToken();

bool walkThroughHandlers = false;
final interceptor = QueuedInterceptorsWrapper(
onRequest: (options, handler) {
walkThroughHandlers = true;
handler.next(options);
},
);

cancelToken.cancel();
final dio = Dio();
dio.interceptors.add(interceptor);
await expectLater(
() => dio.get('/test', cancelToken: cancelToken),
throwsDioException(
DioExceptionType.cancel,
matcher: isA<DioException>(),
),
);
expect(walkThroughHandlers, isFalse);
});
});
}

0 comments on commit 83c8a3f

Please sign in to comment.