Skip to content

Commit

Permalink
HTTP Client errors docs for iOS (#5702)
Browse files Browse the repository at this point in the history
Co-authored-by: Isabel <76437239+imatwawana@users.noreply.github.com>
  • Loading branch information
marandaneto and imatwawana committed Nov 4, 2022
1 parent 756d6f5 commit 3fad325
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/platform-includes/getting-started-config/apple.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func application(_ application: UIApplication,
options.enableAppHangTracking = true
options.enableFileIOTracking = true
options.enableCoreDataTracking = true
options.enableCaptureFailedRequests = true
}

return true
Expand All @@ -35,6 +36,7 @@ func application(_ application: UIApplication,
options.enableAppHangTracking = YES;
options.enableFileIOTracking = YES;
options.enableCoreDataTracking = YES;
options.enableCaptureFailedRequests = YES;
}];

return YES;
Expand All @@ -57,6 +59,7 @@ struct SwiftUIApp: App {
options.enableAppHangTracking = true
options.enableFileIOTracking = true
options.enableCoreDataTracking = true
options.enableCaptureFailedRequests = true
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/platform-includes/getting-started-primer/android.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The SDK builds a crash report that persists to disk and tries to send the report
- Apollo request spans with [Apollo Integration](/platforms/android/configuration/integrations/apollo/)
- Distributed tracing through [OkHttp](/platforms/android/configuration/integrations/okhttp/) and [Apollo](/platforms/android/configuration/integrations/apollo/) integrations
- [Application Not Responding (ANR)](/platforms/android/configuration/app-not-respond/) reported if the application is blocked for more than five seconds
- [HTTP Client Errors](/platforms/android/configuration/integrations/okhttp/#http-client-errors)
- Code samples provided in both Kotlin and Java as the Android SDK uses both languages
- We provide a [sample application](https://github.com/getsentry/sentry-java/tree/master/sentry-samples/sentry-samples-android) for our Android users
- Our [video tutorial](/platforms/android/android-video/) visually demonstrates how to set up our SDK
1 change: 1 addition & 0 deletions src/platform-includes/getting-started-primer/apple.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Error messages of fatalError, assert, and precondition
- [App Hang Detection](/platforms/apple/configuration/app-hangs/)
- [Out of memory](/platforms/apple/configuration/out-of-memory/)
- [HTTP Client Errors](/platforms/apple/configuration/http-client-errors/)
- 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.
- Events [enriched](/platforms/apple/enriching-events/context/) with device data
- Offline caching when a device is unable to connect; we send a report once we receive another event
Expand Down
102 changes: 102 additions & 0 deletions src/platforms/apple/common/configuration/http-client-errors.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
title: HTTP Client Errors
sidebar_order: 13
description: "This feature, once enabled, automatically captures HTTP client errors, like bad response codes, as error events and reports them to Sentry"
---

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.

Sending HTTP client errors is an opt-in feature and can be enabled by setting the `enableCaptureFailedRequests` option to `true`:

```swift {tabTitle:Swift}
import Sentry

SentrySDK.start { options in
options.dsn = "___PUBLIC_DSN___"
options.enableCaptureFailedRequests = true
}
```

```objc {tabTitle:Objective-C}
@import Sentry;

[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
options.dsn = @"___PUBLIC_DSN___";
options.enableCaptureFailedRequests = YES;
}];
```
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:
```swift {tabTitle:Swift}
import Sentry
SentrySDK.start { options in
options.dsn = "___PUBLIC_DSN___"
let httpStatusCodeRange = HttpStatusCodeRange(min: 400, max: 599)
options.failedRequestStatusCodes = [ httpStatusCodeRange ]
}
```

```objc {tabTitle:Objective-C}
@import Sentry;

[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
options.dsn = @"___PUBLIC_DSN___";
SentryHttpStatusCodeRange *httpStatusCodeRange =
[[SentryHttpStatusCodeRange alloc] initWithMin:400 max:599];
options.failedRequestStatusCodes = @[ httpStatusCodeRange ];
}];
```
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:
```swift {tabTitle:Swift}
import Sentry
SentrySDK.start { options in
options.dsn = "___PUBLIC_DSN___"
options.failedRequestTargets = [ "www.example.com" ]
}
```

```objc {tabTitle:Objective-C}
@import Sentry;

[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
options.dsn = @"___PUBLIC_DSN___";
options.failedRequestTargets = @[ @"www.example.com" ];
}];
```
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/).
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.
### Customize or Drop the Error Event
The captured error event can be customized or dropped with a `beforeSend`:
```swift {tabTitle:Swift}
import Sentry
SentrySDK.start { options in
options.dsn = "___PUBLIC_DSN___"
options.beforeSend = { event in
// modify event here or return NULL to discard the event
return event
}
}
```

```objc {tabTitle:Objective-C}
@import Sentry;

[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
options.dsn = @"___PUBLIC_DSN___";
options.beforeSend = ^SentryEvent * _Nullable(SentryEvent * _Nonnull event) {
// modify event here or return NULL to discard the event
return event;
}
}];
```
2 changes: 2 additions & 0 deletions src/platforms/apple/common/configuration/swizzling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ __macOS__
- <PlatformLink to="/performance/instrumentation/automatic-instrumentation/#file-io-tracking">Auto instrumentation for File I/O operations</PlatformLink>
- <PlatformLink to="/performance/instrumentation/automatic-instrumentation/#core-data-tracking">Auto instrumentation for Core Data operations</PlatformLink>
- <PlatformLink to="/performance/connect-services/">Automatically added sentry-trace header to HTTP requests for distributed tracing</PlatformLink>
- <PlatformLink to="/configuration/http-client-errors/">HTTP Client Errors</PlatformLink>


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

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

Expand Down
12 changes: 12 additions & 0 deletions src/platforms/common/configuration/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,18 @@ _(New in version 6.6.0)_

</ConfigKey>

<ConfigKey name="capture-failed-requests" supported={["apple"]}>

Once enabled, this feature automatically captures HTTP client errors, like bad response codes, as error events and reports them to Sentry.

<PlatformSection supported={["apple"]}>

_(New in version 7.30.0)_

</PlatformSection>

</ConfigKey>

<PlatformSection supported={["javascript", "python", "node", "dart", "java", "apple"]}>

## Integration Configuration
Expand Down
2 changes: 2 additions & 0 deletions src/wizard/apple/ios.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func application(_ application: UIApplication,
options.enableAppHangTracking = true
options.enableFileIOTracking = true
options.enableCoreDataTracking = true
options.enableCaptureFailedRequests = true
}

return true
Expand All @@ -68,6 +69,7 @@ struct SwiftUIApp: App {
options.enableAppHangTracking = true
options.enableFileIOTracking = true
options.enableCoreDataTracking = true
options.enableCaptureFailedRequests = true
}
}
}
Expand Down

1 comment on commit 3fad325

@vercel
Copy link

@vercel vercel bot commented on 3fad325 Nov 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sentry-docs – ./

docs.sentry.io
sentry-docs.sentry.dev
sentry-docs-git-master.sentry.dev

Please sign in to comment.