Skip to content

Commit b378ac1

Browse files
committed
add override to http clients
1 parent 5cc82a0 commit b378ac1

File tree

4 files changed

+87
-10
lines changed

4 files changed

+87
-10
lines changed

dart/lib/src/http_client/failed_request_client.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,22 @@ class FailedRequestClient extends BaseClient {
7373
this.failedRequestTargets = SentryHttpClient.defaultFailedRequestTargets,
7474
Client? client,
7575
Hub? hub,
76+
bool? captureFailedRequests,
7677
}) : _hub = hub ?? HubAdapter(),
77-
_client = client ?? Client() {
78-
if (_hub.options.captureFailedRequests) {
78+
_client = client ?? Client(),
79+
_captureFailedRequests = captureFailedRequests {
80+
if (captureFailedRequests == null) {
81+
if (_hub.options.captureFailedRequests) {
82+
_hub.options.sdk.addIntegration('HTTPClientError');
83+
}
84+
} else if (captureFailedRequests) {
7985
_hub.options.sdk.addIntegration('HTTPClientError');
8086
}
8187
}
8288

8389
final Client _client;
8490
final Hub _hub;
91+
final bool? _captureFailedRequests;
8592

8693
/// Describes which HTTP status codes should be considered as a failed
8794
/// requests.
@@ -129,7 +136,11 @@ class FailedRequestClient extends BaseClient {
129136
StackTrace? stackTrace,
130137
StreamedResponse? response,
131138
Duration duration) async {
132-
if (!_hub.options.captureFailedRequests) {
139+
140+
if (_captureFailedRequests == false) {
141+
return;
142+
}
143+
if (_captureFailedRequests != true && !_hub.options.captureFailedRequests) {
133144
return;
134145
}
135146

dart/lib/src/http_client/sentry_http_client.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ import 'failed_request_client.dart';
7373
/// Remarks:
7474
/// HTTP traffic can contain PII (personal identifiable information).
7575
/// Read more on data scrubbing [here](https://docs.sentry.io/product/data-management-settings/advanced-datascrubbing/).
76+
///
77+
/// The constructor parameter `captureFailedRequests` will override what you
78+
/// have configured in options.
7679
/// ```
7780
class SentryHttpClient extends BaseClient {
7881
static const defaultFailedRequestStatusCodes = [
@@ -86,6 +89,7 @@ class SentryHttpClient extends BaseClient {
8689
List<SentryStatusCode> failedRequestStatusCodes =
8790
defaultFailedRequestStatusCodes,
8891
List<String> failedRequestTargets = defaultFailedRequestTargets,
92+
bool? captureFailedRequests,
8993
}) {
9094
_hub = hub ?? HubAdapter();
9195

@@ -96,6 +100,7 @@ class SentryHttpClient extends BaseClient {
96100
failedRequestTargets: failedRequestTargets,
97101
hub: _hub,
98102
client: innerClient,
103+
captureFailedRequests: captureFailedRequests,
99104
);
100105

101106
if (_hub.options.isTracingEnabled()) {

dart/test/http_client/failed_request_client_test.dart

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,30 @@ void main() {
7070
expect(eventCall.contexts.response, isNull);
7171
});
7272

73-
test('event not reported if disabled', () async {
73+
test('exception does not gets reported if client throws but override disables capture', () async {
74+
fixture._hub.options.captureFailedRequests = true;
75+
fixture._hub.options.sendDefaultPii = true;
76+
7477
final sut = fixture.getSut(
7578
client: createThrowingClient(),
7679
captureFailedRequests: false,
7780
);
7881

82+
await expectLater(
83+
() async => await sut.get(requestUri, headers: {'Cookie': 'foo=bar'}),
84+
throwsException,
85+
);
86+
87+
expect(fixture.transport.calls, 0);
88+
});
89+
90+
test('event not reported if disabled', () async {
91+
fixture._hub.options.captureFailedRequests = false;
92+
93+
final sut = fixture.getSut(
94+
client: createThrowingClient(),
95+
);
96+
7997
await expectLater(
8098
() async => await sut.get(requestUri, headers: {'Cookie': 'foo=bar'}),
8199
throwsException,
@@ -84,10 +102,27 @@ void main() {
84102
expect(fixture.transport.calls, 0);
85103
});
86104

105+
test('event reported if disabled but overridden', () async {
106+
fixture._hub.options.captureFailedRequests = false;
107+
108+
final sut = fixture.getSut(
109+
client: createThrowingClient(),
110+
captureFailedRequests: true,
111+
);
112+
113+
await expectLater(
114+
() async => await sut.get(requestUri, headers: {'Cookie': 'foo=bar'}),
115+
throwsException,
116+
);
117+
118+
expect(fixture.transport.calls, 1);
119+
});
120+
87121
test('event not reported if not within the targets', () async {
122+
fixture._hub.options.captureFailedRequests = true;
123+
88124
final sut = fixture.getSut(
89125
client: fixture.getClient(statusCode: 500),
90-
captureFailedRequests: true,
91126
failedRequestTargets: const ["myapi.com"]);
92127

93128
final response = await sut.get(requestUri);
@@ -335,16 +370,16 @@ class Fixture {
335370
List<SentryStatusCode> failedRequestStatusCodes = const [
336371
SentryStatusCode.defaultRange()
337372
],
338-
bool captureFailedRequests = true,
339373
List<String> failedRequestTargets = const [".*"],
374+
bool? captureFailedRequests,
340375
}) {
341376
final mc = client ?? getClient();
342-
_hub.options.captureFailedRequests = captureFailedRequests;
343377
return FailedRequestClient(
344378
client: mc,
345379
hub: _hub,
346380
failedRequestStatusCodes: failedRequestStatusCodes,
347381
failedRequestTargets: failedRequestTargets,
382+
captureFailedRequests: captureFailedRequests
348383
);
349384
}
350385

dart/test/http_client/sentry_http_client_test.dart

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ void main() {
3232
});
3333

3434
test('no captured event with default config', () async {
35+
fixture.hub.options.captureFailedRequests = false;
36+
3537
final sut = fixture.getSut(
3638
client: createThrowingClient(),
37-
captureFailedRequests: false,
3839
);
3940

4041
await expectLater(() async => await sut.get(requestUri), throwsException);
@@ -43,6 +44,19 @@ void main() {
4344
expect(fixture.hub.addBreadcrumbCalls.length, 1);
4445
});
4546

47+
test('captured event with override', () async {
48+
fixture.hub.options.captureFailedRequests = false;
49+
50+
final sut = fixture.getSut(
51+
client: createThrowingClient(),
52+
captureFailedRequests: true,
53+
);
54+
55+
await expectLater(() async => await sut.get(requestUri), throwsException);
56+
57+
expect(fixture.hub.captureEventCalls.length, 1);
58+
});
59+
4660
test('one captured event with when enabling $FailedRequestClient',
4761
() async {
4862
fixture.hub.options.captureFailedRequests = true;
@@ -61,6 +75,18 @@ void main() {
6175
expect(fixture.hub.addBreadcrumbCalls.length, 1);
6276
});
6377

78+
test('no captured event with when enabling $FailedRequestClient with override', () async {
79+
fixture.hub.options.captureFailedRequests = true;
80+
final sut = fixture.getSut(
81+
client: createThrowingClient(),
82+
captureFailedRequests: false,
83+
);
84+
85+
await expectLater(() async => await sut.get(requestUri), throwsException);
86+
87+
expect(fixture.hub.captureEventCalls.length, 0);
88+
});
89+
6490
test('close does get called for user defined client', () async {
6591
final mockHub = MockHub();
6692

@@ -116,14 +142,14 @@ class Fixture {
116142
SentryHttpClient getSut({
117143
MockClient? client,
118144
List<SentryStatusCode> badStatusCodes = const [],
119-
bool captureFailedRequests = true,
145+
bool? captureFailedRequests,
120146
}) {
121147
final mc = client ?? getClient();
122-
hub.options.captureFailedRequests = captureFailedRequests;
123148
return SentryHttpClient(
124149
client: mc,
125150
hub: hub,
126151
failedRequestStatusCodes: badStatusCodes,
152+
captureFailedRequests: captureFailedRequests,
127153
);
128154
}
129155

0 commit comments

Comments
 (0)