|
| 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 | +``` |
0 commit comments