From ae90d7d20d9360a018de44fc64283d72e75feaf4 Mon Sep 17 00:00:00 2001 From: Peter Leibiger Date: Tue, 28 Mar 2023 15:13:29 +0200 Subject: [PATCH] [dio] Revert changes to CancelToken.cancel behavior (#1769) ### New Pull Request Checklist - [x] I have read the [Documentation](https://pub.dev/documentation/dio/latest/) - [x] I have searched for a similar pull request in the [project](https://github.com/cfug/dio/pulls) and found none - [x] I have updated this branch with the latest `main` branch to avoid conflicts (via merge from master or rebase) - [x] I have added the required tests to prove the fix/feature I'm adding - [x] I have updated the documentation (if necessary) - [x] I have run the tests without failures - [x] I have updated the `CHANGELOG.md` in the corresponding package ### Additional context and info (if any) Fixes #1765 --- dio/CHANGELOG.md | 4 +++- dio/lib/src/cancel_token.dart | 7 +------ dio/test/cancel_token_test.dart | 6 ++---- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index 9c54cf280..f954141c5 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -2,7 +2,9 @@ ## Unreleased -*None.* +- Revert changes to `CancelToken.cancel()` behavior, as a result the `DioError` + provided by the `CancelToken.cancelError` does not contain useful information + when the token was not used with a request. ## 5.1.0 diff --git a/dio/lib/src/cancel_token.dart b/dio/lib/src/cancel_token.dart index b4e430917..b049deb4c 100644 --- a/dio/lib/src/cancel_token.dart +++ b/dio/lib/src/cancel_token.dart @@ -31,13 +31,8 @@ class CancelToken { /// Cancel the request with the given [reason]. void cancel([Object? reason]) { - if (requestOptions == null) { - throw StateError( - 'CancelToken was canceled before being used in a request.', - ); - } _cancelError = DioError.requestCancelled( - requestOptions: requestOptions!, + requestOptions: requestOptions ?? RequestOptions(), reason: reason, stackTrace: StackTrace.current, ); diff --git a/dio/test/cancel_token_test.dart b/dio/test/cancel_token_test.dart index 0560945ee..bfbdcb5fc 100644 --- a/dio/test/cancel_token_test.dart +++ b/dio/test/cancel_token_test.dart @@ -16,10 +16,8 @@ void main() { token.cancel(reason); }); - test('cancel without use throws StateError', () async { - final token = CancelToken(); - - expect(() => token.cancel(), throwsStateError); + test('cancel without use does not throw (#1765)', () async { + CancelToken().cancel(); }); }); }