Skip to content

Commit

Permalink
Revert "Catch sync/async exceptions in interceptors' handlers" (#2169)
Browse files Browse the repository at this point in the history
Reverts #2139
Fixes #2167
Reopen #2138

### 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)
- [ ] 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)

Added tests were fake, so they remained effective after the revert.
  • Loading branch information
AlexV525 authored Mar 28, 2024
1 parent 94cd530 commit d256629
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
2 changes: 1 addition & 1 deletion dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ See the [Migration Guide][] for the complete breaking changes list.**

## Unreleased

*None.*
- Revert "Catch sync/async exceptions in interceptors' handlers".

## 5.4.2

Expand Down
34 changes: 12 additions & 22 deletions dio/lib/src/dio_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ abstract class DioMixin implements Dio {
Options? options,
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
}) async {
}) {
final requestOptions = (options ?? Options()).compose(
this.options,
path,
Expand Down Expand Up @@ -377,17 +377,14 @@ abstract class DioMixin implements Dio {
FutureOr Function(dynamic) requestInterceptorWrapper(
InterceptorSendCallback cb,
) {
return (dynamic incomingState) async {
return (dynamic incomingState) {
final state = incomingState as InterceptorState;
if (state.type == InterceptorResultType.next) {
return listenCancelForAsyncTask(
requestOptions.cancelToken,
Future(() async {
final handler = RequestInterceptorHandler();
final callback = cb(state.data as RequestOptions, handler);
if (callback is Future) {
await callback;
}
cb(state.data as RequestOptions, handler);
return handler.future;
}),
);
Expand All @@ -401,24 +398,20 @@ abstract class DioMixin implements Dio {
FutureOr<dynamic> Function(dynamic) responseInterceptorWrapper(
InterceptorSuccessCallback cb,
) {
return (dynamic incomingState) async {
return (dynamic incomingState) {
final state = incomingState as InterceptorState;
if (state.type == InterceptorResultType.next ||
state.type == InterceptorResultType.resolveCallFollowing) {
return listenCancelForAsyncTask(
requestOptions.cancelToken,
Future(() async {
final handler = ResponseInterceptorHandler();
final callback = cb(state.data as Response, handler);
if (callback is Future) {
await callback;
}
cb(state.data as Response, handler);
return handler.future;
}),
);
} else {
return state;
}
return state;
};
}

Expand All @@ -427,16 +420,13 @@ abstract class DioMixin implements Dio {
FutureOr<dynamic> Function(Object) errorInterceptorWrapper(
InterceptorErrorCallback cb,
) {
return (error) {
return (dynamic error) {
final state = error is InterceptorState
? error
: InterceptorState(assureDioException(error, requestOptions));
Future<InterceptorState> handleError() async {
final handler = ErrorInterceptorHandler();
final callback = cb(state.data, handler);
if (callback is Future) {
await callback;
}
cb(state.data, handler);
return handler.future;
}

Expand Down Expand Up @@ -689,10 +679,10 @@ abstract class DioMixin implements Dio {
CancelToken? cancelToken,
Future<T> future,
) {
return Future.any([
if (cancelToken != null) cancelToken.whenCancel.then((e) => throw e),
future,
]);
if (cancelToken == null) {
return future;
}
return Future.any([future, cancelToken.whenCancel.then((e) => throw e)]);
}

@internal
Expand Down
6 changes: 3 additions & 3 deletions dio/lib/src/interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,19 @@ class Interceptor {
}

/// The signature of [Interceptor.onRequest].
typedef InterceptorSendCallback = FutureOr<void> Function(
typedef InterceptorSendCallback = void Function(
RequestOptions options,
RequestInterceptorHandler handler,
);

/// The signature of [Interceptor.onResponse].
typedef InterceptorSuccessCallback = FutureOr<void> Function(
typedef InterceptorSuccessCallback = void Function(
Response<dynamic> response,
ResponseInterceptorHandler handler,
);

/// The signature of [Interceptor.onError].
typedef InterceptorErrorCallback = FutureOr<void> Function(
typedef InterceptorErrorCallback = void Function(
DioException error,
ErrorInterceptorHandler handler,
);
Expand Down

0 comments on commit d256629

Please sign in to comment.