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

Interceptors now gets the StackTrace in the DioError objects #1503

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 3 additions & 2 deletions dio/lib/src/dio_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ abstract class DioMixin implements Dio {
// we can handle the return value of interceptor callback.
FutureOr<dynamic> Function(dynamic, StackTrace) _errorInterceptorWrapper(
InterceptorErrorCallback interceptor) {
return (err, stackTrace) {
return (err, _) {
if (err is! InterceptorState) {
err = InterceptorState(
assureDioError(
Expand All @@ -571,7 +571,8 @@ abstract class DioMixin implements Dio {
Future(() {
return checkIfNeedEnqueue(interceptors.errorLock, () {
var errorHandler = ErrorInterceptorHandler();
interceptor(err.data as DioError, errorHandler);
var dioError = (err.data as DioError)..stackTrace = stackTrace;
interceptor(dioError, errorHandler);
return errorHandler.future;
});
}),
Expand Down
62 changes: 62 additions & 0 deletions dio/test/interceptor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,37 @@ void main() {
response = await dio.get('/test?tag=1');
expect(response.data['errCode'], 0);
});

test("Interceptor gets stacktrace in onError", () async {
var dio = Dio();
dio.options.baseUrl = EchoAdapter.mockBase;
dio.httpClientAdapter = EchoAdapter();

late final StackTrace caughtStackTrace;
dio.interceptors.addAll([
InterceptorsWrapper(
onError: (err, handler) {
caughtStackTrace = err.stackTrace!;
handler.next(err);
},
),
InterceptorsWrapper(onRequest: (options, handler) {
final error = DioError(
error: Error(),
requestOptions: options,
);
handler.reject(error, true);
}),
]);

await expectLater(
dio.get('/error'),
throwsA(predicate(
(error) => error is DioError && caughtStackTrace == error.stackTrace,
)),
reason: "Stacktrace should be available in onError",
);
});
});

group('#test response interceptor', () {
Expand Down Expand Up @@ -543,6 +574,37 @@ void main() {
expect(tokenRequestCounts, 1);
expect(result, 3);
});

test("QueuedInterceptor gets stacktrace in onError", () async {
var dio = Dio();
dio.options.baseUrl = EchoAdapter.mockBase;
dio.httpClientAdapter = EchoAdapter();

late final StackTrace caughtStackTrace;
dio.interceptors.addAll([
QueuedInterceptorsWrapper(
onError: (err, handler) {
caughtStackTrace = err.stackTrace!;
handler.next(err);
},
),
QueuedInterceptorsWrapper(onRequest: (options, handler) {
final error = DioError(
error: Error(),
requestOptions: options,
);
handler.reject(error, true);
}),
]);

await expectLater(
dio.get('/error'),
throwsA(predicate(
(error) => error is DioError && caughtStackTrace == error.stackTrace,
)),
reason: "Stacktrace should be available in onError",
);
});
});

// group('test queued interceptors for error', () {
Expand Down