Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🥅 Throw if cancelled before established #2251

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
}