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

Revert "Catch sync/async exceptions in interceptors' handlers" #2169

Merged
merged 3 commits into from
Mar 28, 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: 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