Skip to content

Commit d433b49

Browse files
committed
feat: remove SentryResponse fields: url, body, redirected, status
1 parent 3f8512b commit d433b49

File tree

6 files changed

+31
-131
lines changed

6 files changed

+31
-131
lines changed

dart/lib/src/http_client/failed_request_client.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,8 @@ class FailedRequestClient extends BaseClient {
190190
if (response != null) {
191191
event.contexts.response = SentryResponse(
192192
headers: sendDefaultPii ? response.headers : null,
193-
redirected: response.isRedirect,
194-
// Body not captured yet.
195-
// See https://github.com/getsentry/sentry-dart/pull/1093#issuecomment-1293056244
196-
body: null,
193+
bodySize: 0, // TODO
197194
statusCode: response.statusCode,
198-
status: response.reasonPhrase,
199195
);
200196
}
201197

dart/lib/src/protocol/sentry_response.dart

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,13 @@ class SentryResponse {
99
/// The tpye of this class in the [Contexts] field
1010
static const String type = 'response';
1111

12-
/// The URL of the response if available.
13-
/// This might be the redirected URL
14-
final String? url;
15-
16-
/// Indicates whether or not the response is the result of a redirect
17-
/// (that is, its URL list has more than one entry).
18-
final bool? redirected;
19-
20-
/// The body of the response
21-
final Object? body;
12+
/// The size of the response body.
13+
final int? bodySize;
2214

2315
/// The HTTP status code of the response.
2416
/// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
2517
final int? statusCode;
2618

27-
/// The status message for the corresponding [statusCode]
28-
final String? status;
29-
3019
/// An immutable dictionary of submitted headers.
3120
/// If a header appears multiple times it,
3221
/// needs to be merged according to the HTTP standard for header merging.
@@ -35,76 +24,43 @@ class SentryResponse {
3524

3625
final Map<String, String>? _headers;
3726

38-
Map<String, String> get other => Map.unmodifiable(_other ?? const {});
39-
40-
final Map<String, String>? _other;
41-
4227
SentryResponse({
43-
this.url,
44-
this.body,
45-
this.redirected,
28+
this.bodySize,
4629
this.statusCode,
47-
this.status,
4830
Map<String, String>? headers,
49-
Map<String, String>? other,
50-
}) : _headers = headers != null ? Map.from(headers) : null,
51-
_other = other != null ? Map.from(other) : null;
31+
}) : _headers = headers != null ? Map.from(headers) : null;
5232

5333
/// Deserializes a [SentryResponse] from JSON [Map].
5434
factory SentryResponse.fromJson(Map<String, dynamic> json) {
5535
return SentryResponse(
56-
url: json['url'],
57-
headers: json.containsKey('headers')
58-
? (json['headers'] as Map<String, dynamic>)
59-
.map((key, value) => MapEntry(key, value as String))
60-
: null,
61-
other: json['other'],
62-
body: json['body'],
63-
statusCode: json['status_code'],
64-
status: json['status'],
65-
redirected: json['redirected'],
66-
);
36+
headers: json.containsKey('headers') ? Map.from(json['headers']) : null,
37+
bodySize: json['body_size'],
38+
statusCode: json['status_code']);
6739
}
6840

6941
/// Produces a [Map] that can be serialized to JSON.
7042
Map<String, dynamic> toJson() {
7143
return <String, dynamic>{
72-
if (url != null) 'url': url,
7344
if (headers.isNotEmpty) 'headers': headers,
74-
if (other.isNotEmpty) 'other': other,
75-
if (redirected != null) 'redirected': redirected,
76-
if (body != null) 'body': body,
77-
if (status != null) 'status': status,
45+
if (bodySize != null) 'body_size': bodySize,
7846
if (statusCode != null) 'status_code': statusCode,
7947
};
8048
}
8149

8250
SentryResponse copyWith({
83-
String? url,
84-
bool? redirected,
8551
int? statusCode,
86-
String? status,
87-
Object? body,
52+
int? bodySize,
8853
Map<String, String>? headers,
89-
Map<String, String>? other,
9054
}) =>
9155
SentryResponse(
92-
url: url ?? this.url,
9356
headers: headers ?? _headers,
94-
redirected: redirected ?? this.redirected,
95-
other: other ?? _other,
96-
body: body ?? this.body,
97-
status: status ?? this.status,
57+
bodySize: bodySize ?? this.bodySize,
9858
statusCode: statusCode ?? this.statusCode,
9959
);
10060

10161
SentryResponse clone() => SentryResponse(
102-
body: body,
62+
bodySize: bodySize,
10363
headers: headers,
104-
other: other,
105-
redirected: redirected,
106-
status: status,
10764
statusCode: statusCode,
108-
url: url,
10965
);
11066
}

dart/test/http_client/failed_request_client_test.dart

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void main() {
2121

2222
test('no captured events when everything goes well', () async {
2323
final sut = fixture.getSut(
24-
client: fixture.getClient(statusCode: 200, reason: 'OK'),
24+
client: fixture.getClient(statusCode: 200),
2525
);
2626

2727
final response = await sut.get(requestUri);
@@ -83,7 +83,7 @@ void main() {
8383
test('event reported if bad status code occurs', () async {
8484
final sut = fixture.getSut(
8585
client: fixture.getClient(
86-
statusCode: 404, reason: 'Not Found', headers: {'lorem': 'ipsum'}),
86+
statusCode: 404, body: 'foo', headers: {'lorem': 'ipsum'}),
8787
badStatusCodes: [SentryStatusCode(404)],
8888
);
8989

@@ -120,20 +120,16 @@ void main() {
120120
expect(request?.other.keys.contains('content_length'), true);
121121

122122
final response = eventCall.contexts.response!;
123-
expect(response.body, isNull);
123+
expect(response.bodySize, 0); // TODO 3
124124
expect(response.statusCode, 404);
125-
expect(response.status, 'Not Found');
126125
expect(response.headers, equals({'lorem': 'ipsum'}));
127-
expect(response.other, isEmpty);
128-
expect(response.redirected, false);
129-
expect(response.url, isNull);
130126
});
131127

132128
test(
133129
'just one report on status code reporting with failing requests enabled',
134130
() async {
135131
final sut = fixture.getSut(
136-
client: fixture.getClient(statusCode: 404, reason: 'Not Found'),
132+
client: fixture.getClient(statusCode: 404),
137133
badStatusCodes: [SentryStatusCode(404)],
138134
captureFailedRequests: true,
139135
);
@@ -172,13 +168,12 @@ void main() {
172168
expect(fixture.transport.calls, 1);
173169
expect(event.request?.headers.isEmpty, true);
174170
expect(event.request?.cookies, isNull);
175-
expect(event.request?.data, isNull);
176-
expect(event.contexts.response?.headers.isEmpty, true);
171+
expect(event.contexts.response, isNull);
177172
});
178173

179174
test('pii is not send on invalid status code', () async {
180175
final sut = fixture.getSut(
181-
client: fixture.getClient(statusCode: 404, reason: 'Not Found'),
176+
client: fixture.getClient(statusCode: 404),
182177
badStatusCodes: [SentryStatusCode(404)],
183178
captureFailedRequests: false,
184179
sendDefaultPii: false,
@@ -190,7 +185,7 @@ void main() {
190185
expect(fixture.transport.calls, 1);
191186
expect(event.request?.headers.isEmpty, true);
192187
expect(event.request?.cookies, isNull);
193-
expect(event.request?.data, isNull);
188+
expect(event.contexts.response, isNotNull);
194189
expect(event.contexts.response?.headers.isEmpty, true);
195190
});
196191

@@ -284,11 +279,11 @@ class Fixture {
284279

285280
MockClient getClient(
286281
{int statusCode = 200,
287-
String? reason,
282+
String body = '',
288283
Map<String, String> headers = const {}}) {
289284
return MockClient((request) async {
290285
expect(request.url, requestUri);
291-
return Response('', statusCode, reasonPhrase: reason, headers: headers);
286+
return Response(body, statusCode, headers: headers);
292287
});
293288
}
294289
}

dart/test/protocol/sentry_response_test.dart

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,15 @@ import 'package:test/test.dart';
44

55
void main() {
66
final sentryResponse = SentryResponse(
7-
url: 'url',
8-
body: 'foobar',
9-
redirected: true,
10-
status: 'OK',
7+
bodySize: 42,
118
statusCode: 200,
129
headers: {'header_key': 'header_value'},
13-
other: {'other_key': 'other_value'},
1410
);
1511

1612
final sentryResponseJson = <String, dynamic>{
17-
'url': 'url',
18-
'body': 'foobar',
19-
'redirected': true,
20-
'status': 'OK',
13+
'body_size': 42,
2114
'status_code': 200,
2215
'headers': {'header_key': 'header_value'},
23-
'other': {'other_key': 'other_value'},
2416
};
2517

2618
group('json', () {
@@ -59,20 +51,14 @@ void main() {
5951
final data = sentryResponse;
6052

6153
final copy = data.copyWith(
62-
url: 'url1',
63-
body: 'barfoo',
54+
bodySize: 11,
6455
headers: {'key1': 'value1'},
65-
redirected: false,
6656
statusCode: 301,
67-
status: 'REDIRECT',
6857
);
6958

70-
expect('url1', copy.url);
71-
expect('barfoo', copy.body);
59+
expect(11, copy.bodySize);
7260
expect({'key1': 'value1'}, copy.headers);
73-
expect(false, copy.redirected);
7461
expect(301, copy.statusCode);
75-
expect('REDIRECT', copy.status);
7662
});
7763
});
7864
}

dio/lib/src/dio_event_processor.dart

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class DioEventProcessor implements EventProcessor {
2121

2222
final SentryOptions _options;
2323
final MaxRequestBodySize _maxRequestBodySize;
24+
// Will be used again, see https://github.com/getsentry/sentry-dart/issues/624
25+
// ignore: unused_field
2426
final MaxResponseBodySize _maxResponseBodySize;
2527

2628
SentryExceptionFactory get _sentryExceptionFactory =>
@@ -162,29 +164,8 @@ class DioEventProcessor implements EventProcessor {
162164

163165
return SentryResponse(
164166
headers: _options.sendDefaultPii ? headers : null,
165-
url: response?.realUri.toString(),
166-
redirected: response?.isRedirect,
167-
body: _getResponseData(dioError.response?.data),
167+
bodySize: dioError.response?.data?.length as int?,
168168
statusCode: response?.statusCode,
169-
status: response?.statusMessage,
170169
);
171170
}
172-
173-
/// Returns the request data, if possible according to the users settings.
174-
/// Type checks are based on DIOs [ResponseType].
175-
Object? _getResponseData(dynamic data) {
176-
if (!_options.sendDefaultPii) {
177-
return null;
178-
}
179-
if (data is String) {
180-
if (_maxResponseBodySize.shouldAddBody(data.codeUnits.length)) {
181-
return data;
182-
}
183-
} else if (data is List<int>) {
184-
if (_maxResponseBodySize.shouldAddBody(data.length)) {
185-
return data;
186-
}
187-
}
188-
return null;
189-
}
190171
}

dio/test/dio_event_processor_test.dart

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,9 @@ void main() {
115115

116116
expect(processedEvent.throwable, event.throwable);
117117
expect(processedEvent.contexts.response, isNotNull);
118-
expect(processedEvent.contexts.response?.body, 'foobar');
119-
expect(processedEvent.contexts.response?.redirected, true);
120-
expect(processedEvent.contexts.response?.status, 'OK');
118+
expect(processedEvent.contexts.response?.bodySize, 6);
121119
expect(processedEvent.contexts.response?.statusCode, 200);
122-
expect(
123-
processedEvent.contexts.response?.url,
124-
'https://example.org/foo/bar?foo=bar',
125-
);
126-
expect(processedEvent.contexts.response?.headers, <String, String>{
127-
'foo': 'bar',
128-
});
120+
expect(processedEvent.contexts.response?.headers, {'foo': 'bar'});
129121
});
130122

131123
test('$DioEventProcessor adds response without PII', () {
@@ -153,14 +145,8 @@ void main() {
153145

154146
expect(processedEvent.throwable, event.throwable);
155147
expect(processedEvent.contexts.response, isNotNull);
156-
expect(processedEvent.contexts.response?.body, isNull);
157-
expect(processedEvent.contexts.response?.redirected, true);
158-
expect(processedEvent.contexts.response?.status, 'OK');
148+
expect(processedEvent.contexts.response?.bodySize, 6);
159149
expect(processedEvent.contexts.response?.statusCode, 200);
160-
expect(
161-
processedEvent.contexts.response?.url,
162-
'https://example.org/foo/bar?foo=bar',
163-
);
164150
expect(processedEvent.contexts.response?.headers, <String, String>{});
165151
});
166152
});

0 commit comments

Comments
 (0)