Skip to content

Add DioException response data to error breadcrumb #3164

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

Merged
merged 4 commits into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
- Tag all spans with thread info on non-web platforms ([#3101](https://github.com/getsentry/sentry-dart/pull/3101), [#3144](https://github.com/getsentry/sentry-dart/pull/3144))
- feat(feedback): Add option to disable keyboard resize ([#3154](https://github.com/getsentry/sentry-dart/pull/3154))

### Enhancements

- Add `DioException` response data to error breadcrumb ([#3164](https://github.com/getsentry/sentry-dart/pull/3164))

## 9.7.0-beta.1

### Features
Expand Down
15 changes: 15 additions & 0 deletions packages/dio/lib/src/breadcrumb_client_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class BreadcrumbClientAdapter implements HttpClientAdapter {
final stopwatch = Stopwatch();
stopwatch.start();

DioException? dioException;
try {
final response =
await _client.fetch(options, requestStream, cancelFuture);
Expand All @@ -46,6 +47,10 @@ class BreadcrumbClientAdapter implements HttpClientAdapter {
responseBodySize = HttpHeaderUtils.getContentLength(response.headers);

return response;
} on DioException catch (e) {
requestHadException = true;
dioException = e;
rethrow;
} catch (_) {
requestHadException = true;
rethrow;
Expand All @@ -57,8 +62,18 @@ class BreadcrumbClientAdapter implements HttpClientAdapter {
HttpSanitizer.sanitizeUrl(options.uri.toString()) ?? UrlDetails();

SentryLevel? level;

if (requestHadException) {
level = SentryLevel.error;
final dioExceptionResponse = dioException?.response;
if (dioExceptionResponse != null) {
statusCode = dioExceptionResponse.statusCode;
reason = dioExceptionResponse.statusMessage;
// ignore: invalid_use_of_internal_member
responseBodySize = HttpHeaderUtils.getContentLength(
dioExceptionResponse.headers.map,
);
}
} else if (statusCode != null) {
// ignore: invalid_use_of_internal_member
level = getBreadcrumbLogLevelFromHttpStatusCode(statusCode);
Expand Down
74 changes: 73 additions & 1 deletion packages/dio/test/breadcrumb_client_adapter_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// ignore_for_file: deprecated_member_use

import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:mockito/mockito.dart';
import 'package:sentry/sentry.dart';
Expand Down Expand Up @@ -148,6 +150,39 @@ void main() {
expect(breadcrumb.data?['duration'], isNotNull);
});

test('breadcrumb gets added when DioException with response is thrown',
() async {
final sut = fixture.getSut(
DioExceptionWithResponseAdapter(
statusCode: 404,
statusMessage: 'Not Found',
headers: {
'content-length': ['123'],
},
),
);

try {
await sut.get<dynamic>('');
fail('Method did not throw');
} on DioException catch (_) {}

expect(fixture.hub.addBreadcrumbCalls.length, 1);

final breadcrumb = fixture.hub.addBreadcrumbCalls.first.crumb;

expect(breadcrumb.type, 'http');
expect(breadcrumb.data?['url'], 'https://example.com');
expect(breadcrumb.data?['method'], 'GET');
expect(breadcrumb.data?['http.query'], 'foo=bar');
expect(breadcrumb.data?['http.fragment'], 'baz');
expect(breadcrumb.level, SentryLevel.error);
expect(breadcrumb.data?['duration'], isNotNull);
expect(breadcrumb.data?['status_code'], 404);
expect(breadcrumb.data?['reason'], 'Not Found');
expect(breadcrumb.data?['response_body_size'], 123);
});

test('close does get called for user defined client', () async {
final mockHub = MockHub();

Expand Down Expand Up @@ -185,8 +220,45 @@ void main() {

class CloseableMockClientAdapter extends Mock implements HttpClientAdapter {}

class DioExceptionWithResponseAdapter implements HttpClientAdapter {
DioExceptionWithResponseAdapter({
required this.statusCode,
required this.statusMessage,
required this.headers,
});

final int statusCode;
final String statusMessage;
final Map<String, List<String>> headers;

@override
Future<ResponseBody> fetch(
RequestOptions options,
Stream<Uint8List>? requestStream,
Future<dynamic>? cancelFuture,
) async {
final response = Response<dynamic>(
requestOptions: options,
statusCode: statusCode,
statusMessage: statusMessage,
headers: Headers.fromMap(headers),
);

throw DioException.badResponse(
requestOptions: options,
response: response,
statusCode: statusCode,
);
}

@override
void close({bool force = false}) {
// No-op for testing
}
}

class Fixture {
Dio getSut([MockHttpClientAdapter? client]) {
Dio getSut([HttpClientAdapter? client]) {
final mc = client ?? getClient();
final dio = Dio(
BaseOptions(baseUrl: 'https://example.com?foo=bar#baz'),
Expand Down
Loading