-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(dio): prevent request hang when async interceptor throws #2499
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
base: main
Are you sure you want to change the base?
Changes from all commits
2f265af
5b49970
7507d5d
a14c0cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -426,6 +426,87 @@ void main() { | |
| ); | ||
| }); | ||
|
|
||
| test('Async interceptor error does not hang the request', () async { | ||
| final dio = Dio() | ||
| ..options.baseUrl = MockAdapter.mockBase | ||
| ..httpClientAdapter = MockAdapter(); | ||
| const errorMsg = 'async interceptor error'; | ||
| dio.interceptors.add( | ||
| InterceptorsWrapper( | ||
| // ignore: void_checks | ||
| onRequest: (options, handler) async { | ||
| await Future<void>.value(); | ||
| throw UnsupportedError(errorMsg); | ||
| }, | ||
|
Comment on lines
+437
to
+440
|
||
| ), | ||
| ); | ||
| await expectLater( | ||
| dio.get('/test'), | ||
| throwsA( | ||
| isA<DioException>().having( | ||
| (e) => e.error, | ||
| 'error', | ||
| isA<UnsupportedError>() | ||
| .having((e) => e.message, 'message', errorMsg), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| test('Async onResponse error does not hang the request', () async { | ||
| final dio = Dio() | ||
| ..options.baseUrl = MockAdapter.mockBase | ||
| ..httpClientAdapter = MockAdapter(); | ||
| const errorMsg = 'async response error'; | ||
| dio.interceptors.add( | ||
| InterceptorsWrapper( | ||
| // ignore: void_checks | ||
| onResponse: (response, handler) async { | ||
| await Future<void>.value(); | ||
| throw UnsupportedError(errorMsg); | ||
| }, | ||
|
Comment on lines
+464
to
+467
|
||
| ), | ||
| ); | ||
| await expectLater( | ||
| dio.get('/test'), | ||
| throwsA( | ||
| isA<DioException>().having( | ||
| (e) => e.error, | ||
| 'error', | ||
| isA<UnsupportedError>() | ||
| .having((e) => e.message, 'message', errorMsg), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| test('Async onError interceptor error does not hang', () async { | ||
| final dio = Dio() | ||
| ..options.baseUrl = MockAdapter.mockBase | ||
| ..httpClientAdapter = MockAdapter(); | ||
| const errorMsg = 'async error interceptor error'; | ||
| dio.interceptors.add( | ||
| InterceptorsWrapper( | ||
| // ignore: void_checks | ||
| onError: (err, handler) async { | ||
| await Future<void>.value(); | ||
| throw UnsupportedError(errorMsg); | ||
| }, | ||
|
Comment on lines
+491
to
+494
|
||
| ), | ||
| ); | ||
| await expectLater( | ||
| dio.get('/test-not-found'), | ||
| throwsA( | ||
| isA<DioException>().having( | ||
| (e) => e.error, | ||
| 'error', | ||
| isA<UnsupportedError>() | ||
| .having((e) => e.message, 'message', errorMsg), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| group(ImplyContentTypeInterceptor, () { | ||
| Dio createDio() { | ||
| final dio = Dio(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handleUncaughtErrorreceives astackTracebut it’s currently ignored, so theDioExceptioncreated viaassureDioExceptionwill userequestOptions.sourceStackTrace/StackTrace.currentinstead of the interceptor’s actual throw site. Please thread the providedstackTraceinto the error you pass to the handler (e.g., by constructing aDioExceptionwithstackTrace: stackTrace, or by extendingassureDioExceptionto accept a stack trace) so users get actionable traces.