Skip to content

Commit 3fad325

Browse files
HTTP Client errors docs for iOS (getsentry#5702)
Co-authored-by: Isabel <[email protected]>
1 parent 756d6f5 commit 3fad325

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

src/platform-includes/getting-started-config/apple.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func application(_ application: UIApplication,
1616
options.enableAppHangTracking = true
1717
options.enableFileIOTracking = true
1818
options.enableCoreDataTracking = true
19+
options.enableCaptureFailedRequests = true
1920
}
2021

2122
return true
@@ -35,6 +36,7 @@ func application(_ application: UIApplication,
3536
options.enableAppHangTracking = YES;
3637
options.enableFileIOTracking = YES;
3738
options.enableCoreDataTracking = YES;
39+
options.enableCaptureFailedRequests = YES;
3840
}];
3941

4042
return YES;
@@ -57,6 +59,7 @@ struct SwiftUIApp: App {
5759
options.enableAppHangTracking = true
5860
options.enableFileIOTracking = true
5961
options.enableCoreDataTracking = true
62+
options.enableCaptureFailedRequests = true
6063
}
6164
}
6265
}

src/platform-includes/getting-started-primer/android.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ The SDK builds a crash report that persists to disk and tries to send the report
3838
- Apollo request spans with [Apollo Integration](/platforms/android/configuration/integrations/apollo/)
3939
- Distributed tracing through [OkHttp](/platforms/android/configuration/integrations/okhttp/) and [Apollo](/platforms/android/configuration/integrations/apollo/) integrations
4040
- [Application Not Responding (ANR)](/platforms/android/configuration/app-not-respond/) reported if the application is blocked for more than five seconds
41+
- [HTTP Client Errors](/platforms/android/configuration/integrations/okhttp/#http-client-errors)
4142
- Code samples provided in both Kotlin and Java as the Android SDK uses both languages
4243
- We provide a [sample application](https://github.com/getsentry/sentry-java/tree/master/sentry-samples/sentry-samples-android) for our Android users
4344
- Our [video tutorial](/platforms/android/android-video/) visually demonstrates how to set up our SDK

src/platform-includes/getting-started-primer/apple.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Error messages of fatalError, assert, and precondition
1212
- [App Hang Detection](/platforms/apple/configuration/app-hangs/)
1313
- [Out of memory](/platforms/apple/configuration/out-of-memory/)
14+
- [HTTP Client Errors](/platforms/apple/configuration/http-client-errors/)
1415
- Start-up crashes. The SDK init waits synchronously for up to 5 seconds to flush out events if the app crashes within 2 seconds after the SDK init.
1516
- Events [enriched](/platforms/apple/enriching-events/context/) with device data
1617
- Offline caching when a device is unable to connect; we send a report once we receive another event
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title: HTTP Client Errors
3+
sidebar_order: 13
4+
description: "This feature, once enabled, automatically captures HTTP client errors, like bad response codes, as error events and reports them to Sentry"
5+
---
6+
7+
Once enabled, this feature automatically captures HTTP client errors, like bad response codes, as error events and reports them to Sentry. The error event will contain the `request` and `response` data, such as `url`, `status_code`, and so on.
8+
9+
Sending HTTP client errors is an opt-in feature and can be enabled by setting the `enableCaptureFailedRequests` option to `true`:
10+
11+
```swift {tabTitle:Swift}
12+
import Sentry
13+
14+
SentrySDK.start { options in
15+
options.dsn = "___PUBLIC_DSN___"
16+
options.enableCaptureFailedRequests = true
17+
}
18+
```
19+
20+
```objc {tabTitle:Objective-C}
21+
@import Sentry;
22+
23+
[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
24+
options.dsn = @"___PUBLIC_DSN___";
25+
options.enableCaptureFailedRequests = YES;
26+
}];
27+
```
28+
29+
By default, only HTTP client errors with a response code between `500` and `599` are captured as error events, but you can change this behavior by setting the `failedRequestStatusCodes` option:
30+
31+
```swift {tabTitle:Swift}
32+
import Sentry
33+
34+
SentrySDK.start { options in
35+
options.dsn = "___PUBLIC_DSN___"
36+
let httpStatusCodeRange = HttpStatusCodeRange(min: 400, max: 599)
37+
options.failedRequestStatusCodes = [ httpStatusCodeRange ]
38+
}
39+
```
40+
41+
```objc {tabTitle:Objective-C}
42+
@import Sentry;
43+
44+
[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
45+
options.dsn = @"___PUBLIC_DSN___";
46+
SentryHttpStatusCodeRange *httpStatusCodeRange =
47+
[[SentryHttpStatusCodeRange alloc] initWithMin:400 max:599];
48+
options.failedRequestStatusCodes = @[ httpStatusCodeRange ];
49+
}];
50+
```
51+
52+
HTTP client errors from every target (`.*` regular expression) are automatically captured, but you can change this behavior by setting the `failedRequestTargets` option with either a regular expression or a plain `String`. A plain string must contain at least one of the items from the list. Plain strings don't have to be full matches, meaning the URL of a request is matched when it contains a string provided through the option:
53+
54+
```swift {tabTitle:Swift}
55+
import Sentry
56+
57+
SentrySDK.start { options in
58+
options.dsn = "___PUBLIC_DSN___"
59+
options.failedRequestTargets = [ "www.example.com" ]
60+
}
61+
```
62+
63+
```objc {tabTitle:Objective-C}
64+
@import Sentry;
65+
66+
[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
67+
options.dsn = @"___PUBLIC_DSN___";
68+
options.failedRequestTargets = @[ @"www.example.com" ];
69+
}];
70+
```
71+
72+
Error events may contain PII data, such as `Headers` and `Cookies`. Sentry already does data scrubbing by default, but you can scrub any data before it is sent. Learn more in [Scrubbing Sensitive Data](/platforms/apple/guides/ios/data-management/sensitive-data/).
73+
74+
These events are searchable and you can set alerts on them if you use the `http.url` and `http.status_code` properties. Learn more in our full [Searchable Properties](/product/sentry-basics/search/searchable-properties/) documentation.
75+
76+
### Customize or Drop the Error Event
77+
78+
The captured error event can be customized or dropped with a `beforeSend`:
79+
80+
```swift {tabTitle:Swift}
81+
import Sentry
82+
83+
SentrySDK.start { options in
84+
options.dsn = "___PUBLIC_DSN___"
85+
options.beforeSend = { event in
86+
// modify event here or return NULL to discard the event
87+
return event
88+
}
89+
}
90+
```
91+
92+
```objc {tabTitle:Objective-C}
93+
@import Sentry;
94+
95+
[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
96+
options.dsn = @"___PUBLIC_DSN___";
97+
options.beforeSend = ^SentryEvent * _Nullable(SentryEvent * _Nonnull event) {
98+
// modify event here or return NULL to discard the event
99+
return event;
100+
}
101+
}];
102+
```

src/platforms/apple/common/configuration/swizzling.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ __macOS__
1212
- <PlatformLink to="/performance/instrumentation/automatic-instrumentation/#file-io-tracking">Auto instrumentation for File I/O operations</PlatformLink>
1313
- <PlatformLink to="/performance/instrumentation/automatic-instrumentation/#core-data-tracking">Auto instrumentation for Core Data operations</PlatformLink>
1414
- <PlatformLink to="/performance/connect-services/">Automatically added sentry-trace header to HTTP requests for distributed tracing</PlatformLink>
15+
- <PlatformLink to="/configuration/http-client-errors/">HTTP Client Errors</PlatformLink>
1516

1617

1718
__iOS, tvOS and Catalyst__
@@ -23,6 +24,7 @@ __iOS, tvOS and Catalyst__
2324
- <PlatformLink to="/performance/instrumentation/automatic-instrumentation/#core-data-tracking">Auto instrumentation for Core Data operations</PlatformLink>
2425
- <PlatformLink to="/performance/connect-services/">Automatically added sentry-trace header to HTTP requests for distributed tracing</PlatformLink>
2526
- <PlatformLink to="/performance/instrumentation/automatic-instrumentation/#user-interaction-tracing">User interaction transactions for UI clicks (experimental)</PlatformLink>
27+
- <PlatformLink to="/configuration/http-client-errors/">HTTP Client Errors</PlatformLink>
2628

2729
Since Cocoa 7.5.0, you can opt out of swizzling using options. When you disable swizzling, the SDK disables the features above:
2830

src/platforms/common/configuration/options.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,18 @@ _(New in version 6.6.0)_
507507

508508
</ConfigKey>
509509

510+
<ConfigKey name="capture-failed-requests" supported={["apple"]}>
511+
512+
Once enabled, this feature automatically captures HTTP client errors, like bad response codes, as error events and reports them to Sentry.
513+
514+
<PlatformSection supported={["apple"]}>
515+
516+
_(New in version 7.30.0)_
517+
518+
</PlatformSection>
519+
520+
</ConfigKey>
521+
510522
<PlatformSection supported={["javascript", "python", "node", "dart", "java", "apple"]}>
511523

512524
## Integration Configuration

src/wizard/apple/ios.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func application(_ application: UIApplication,
4242
options.enableAppHangTracking = true
4343
options.enableFileIOTracking = true
4444
options.enableCoreDataTracking = true
45+
options.enableCaptureFailedRequests = true
4546
}
4647

4748
return true
@@ -68,6 +69,7 @@ struct SwiftUIApp: App {
6869
options.enableAppHangTracking = true
6970
options.enableFileIOTracking = true
7071
options.enableCoreDataTracking = true
72+
options.enableCaptureFailedRequests = true
7173
}
7274
}
7375
}

0 commit comments

Comments
 (0)