HTTP Client Errors

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.

Since 8.0.0, this feature has been enabled by default. To disable it:

Copied
import Sentry

SentrySDK.start { options in
    options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
    options.enableCaptureFailedRequests = false
}

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:

Copied
import Sentry

SentrySDK.start { options in
    options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
    let httpStatusCodeRange = HttpStatusCodeRange(min: 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:

Copied
import Sentry

SentrySDK.start { options in
    options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
    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.

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 documentation.

The captured error event can be customized or dropped with a beforeSend:

Copied
import Sentry

SentrySDK.start { options in
    options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
    options.beforeSend = { event in
        // modify event here or return NULL to discard the event
        return event
    }
}
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").