Skip to content

Commit fab4a6e

Browse files
authored
feat: configuring http client errors for Apple targets (#76)
* add implementation * add to sample * adjust docs * move sample code to kotlin * add changelog
1 parent 8b4cab0 commit fab4a6e

File tree

7 files changed

+84
-1
lines changed

7 files changed

+84
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
### Features
66

7-
- Improve Objc/Swift experience with @HiddenFromObjc ([#62](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/62))
7+
- feat: configuring http client errors for Apple targets ([#76](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/76))
8+
- feat: improve Objc/Swift experience with @HiddenFromObjc ([#62](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/62))
89
- feat: add view hierarchy ([#53](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/53))
910

1011
### Fixes

sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryOptionsExtensions.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.sentry.kotlin.multiplatform.extensions
22

33
import PrivateSentrySDKOnly.Sentry.PrivateSentrySDKOnly
44
import cocoapods.Sentry.SentryEvent
5+
import cocoapods.Sentry.SentryHttpStatusCodeRange
56
import io.sentry.kotlin.multiplatform.BuildKonfig
67
import io.sentry.kotlin.multiplatform.CocoaSentryOptions
78
import io.sentry.kotlin.multiplatform.SentryOptions
@@ -59,4 +60,13 @@ internal fun CocoaSentryOptions.applyCocoaBaseOptions(options: SentryOptions) {
5960
cocoaBreadcrumb?.toKmpBreadcrumb()
6061
?.let { options.beforeBreadcrumb?.invoke(it) }?.toCocoaBreadcrumb()
6162
}
63+
64+
enableCaptureFailedRequests = options.enableCaptureFailedRequests
65+
failedRequestTargets = options.failedRequestTargets
66+
failedRequestStatusCodes = options.failedRequestStatusCodes.map {
67+
SentryHttpStatusCodeRange(
68+
min = it.min.convert(),
69+
max = it.max.convert()
70+
)
71+
}
6272
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.sentry.kotlin.multiplatform
2+
3+
/**
4+
* The Http status code range. Example for a range: 400 to 499, 500 to 599, 400 to 599 The range is
5+
* inclusive so the min and max is considered part of the range.
6+
*
7+
* Example for a single status code 400, 500
8+
*/
9+
public data class HttpStatusCodeRange(val min: Int = DEFAULT_MIN, val max: Int = DEFAULT_MAX) {
10+
11+
public constructor(statusCode: Int) : this(statusCode, statusCode)
12+
13+
public fun isInRange(statusCode: Int): Boolean {
14+
return statusCode in min..max
15+
}
16+
17+
public companion object {
18+
public const val DEFAULT_MIN: Int = 500
19+
public const val DEFAULT_MAX: Int = 599
20+
}
21+
}

sentry-kotlin-multiplatform/src/commonMain/kotlin/io/sentry/kotlin/multiplatform/SentryOptions.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,28 @@ public open class SentryOptions {
7272
* This is only available on iOS and Android.
7373
*/
7474
public var attachViewHierarchy: Boolean = false
75+
76+
/**
77+
* Enables or disables the feature to automatically capture HTTP client errors.
78+
* This is enabled by default.
79+
*
80+
* Available on Apple.
81+
*/
82+
public var enableCaptureFailedRequests: Boolean = true
83+
84+
/**
85+
* A list of HTTP status code ranges indicating which client errors should be captured as errors.
86+
* By default, only HTTP client errors with a response code between 500 and 599 are captured as errors.
87+
*
88+
* Available on Apple.
89+
*/
90+
public var failedRequestStatusCodes: List<HttpStatusCodeRange> = listOf(HttpStatusCodeRange())
91+
92+
/**
93+
* A list of HTTP request targets indicating which client errors should be captured as errors with either regex or a plain string.
94+
* By default, HTTP client errors from every target (.* regular expression) are automatically captured.
95+
*
96+
* Available on Apple.
97+
*/
98+
public var failedRequestTargets: List<String> = listOf(".*")
7599
}

sentry-samples/kmp-app/iosApp/iosApp/ContentView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ struct ContentView: View {
1111
Button("Capture Exception") {
1212
LoginImpl().login(username: "MyUsername")
1313
}
14+
Button("Capture Http Client Error") {
15+
HttpClientKt.captureHttpClientError()
16+
}
1417
Button("Hard Crash") {
1518
LoginImpl().login(username: nil)
1619
}

sentry-samples/kmp-app/shared/src/commonMain/kotlin/sample.kmp.app/AppSetup.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sample.kmp.app
22

33
import io.sentry.kotlin.multiplatform.Attachment
44
import io.sentry.kotlin.multiplatform.Context
5+
import io.sentry.kotlin.multiplatform.HttpStatusCodeRange
56
import io.sentry.kotlin.multiplatform.OptionsConfiguration
67
import io.sentry.kotlin.multiplatform.Sentry
78

@@ -45,6 +46,8 @@ private fun optionsConfiguration(): OptionsConfiguration {
4546
it.attachScreenshot = true
4647
it.attachViewHierarchy = true
4748
it.release = "[email protected]"
49+
it.failedRequestStatusCodes = listOf(HttpStatusCodeRange(400, 599))
50+
it.failedRequestTargets = listOf("httpbin.org")
4851
it.beforeBreadcrumb = { breadcrumb ->
4952
breadcrumb.message = "Add message before every breadcrumb"
5053
breadcrumb
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package sample.kmp.app
2+
3+
import platform.Foundation.NSURL
4+
import platform.Foundation.NSURLRequest
5+
import platform.Foundation.NSURLSession
6+
import platform.Foundation.dataTaskWithRequest
7+
8+
@Suppress("Unused") // Called from Swift
9+
fun captureHttpClientError() {
10+
val url = NSURL(string = "https://httpbin.org/status/404")
11+
val request = NSURLRequest(uRL = url)
12+
NSURLSession.sharedSession.dataTaskWithRequest(request) { data, response, error ->
13+
if (error != null) {
14+
// handle error
15+
println("error: $error")
16+
} else {
17+
// handle successful response
18+
println("response: $response")
19+
}
20+
}.resume()
21+
}

0 commit comments

Comments
 (0)