Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### Features

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

### Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.sentry.kotlin.multiplatform.extensions

import PrivateSentrySDKOnly.Sentry.PrivateSentrySDKOnly
import cocoapods.Sentry.SentryEvent
import cocoapods.Sentry.SentryHttpStatusCodeRange
import io.sentry.kotlin.multiplatform.BuildKonfig
import io.sentry.kotlin.multiplatform.CocoaSentryOptions
import io.sentry.kotlin.multiplatform.SentryOptions
Expand Down Expand Up @@ -59,4 +60,13 @@ internal fun CocoaSentryOptions.applyCocoaBaseOptions(options: SentryOptions) {
cocoaBreadcrumb?.toKmpBreadcrumb()
?.let { options.beforeBreadcrumb?.invoke(it) }?.toCocoaBreadcrumb()
}

enableCaptureFailedRequests = options.enableCaptureFailedRequests
failedRequestTargets = options.failedRequestTargets
failedRequestStatusCodes = options.failedRequestStatusCodes.map {
SentryHttpStatusCodeRange(
min = it.min.convert(),
max = it.max.convert()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.sentry.kotlin.multiplatform

/**
* The Http status code range. Example for a range: 400 to 499, 500 to 599, 400 to 599 The range is
* inclusive so the min and max is considered part of the range.
*
* Example for a single status code 400, 500
*/
public data class HttpStatusCodeRange(val min: Int = DEFAULT_MIN, val max: Int = DEFAULT_MAX) {

public constructor(statusCode: Int) : this(statusCode, statusCode)

public fun isInRange(statusCode: Int): Boolean {
return statusCode in min..max
}

public companion object {
public const val DEFAULT_MIN: Int = 500
public const val DEFAULT_MAX: Int = 599
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,28 @@ public open class SentryOptions {
* This is only available on iOS and Android.
*/
public var attachViewHierarchy: Boolean = false

/**
* Enables or disables the feature to automatically capture HTTP client errors.
* This is enabled by default.
*
* Available on Apple.
*/
public var enableCaptureFailedRequests: Boolean = true

/**
* A list of HTTP status code ranges indicating which client errors should be captured as errors.
* By default, only HTTP client errors with a response code between 500 and 599 are captured as errors.
*
* Available on Apple.
*/
public var failedRequestStatusCodes: List<HttpStatusCodeRange> = listOf(HttpStatusCodeRange())

/**
* A list of HTTP request targets indicating which client errors should be captured as errors with either regex or a plain string.
* By default, HTTP client errors from every target (.* regular expression) are automatically captured.
*
* Available on Apple.
*/
public var failedRequestTargets: List<String> = listOf(".*")
}
3 changes: 3 additions & 0 deletions sentry-samples/kmp-app/iosApp/iosApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ struct ContentView: View {
Button("Capture Exception") {
LoginImpl().login(username: "MyUsername")
}
Button("Capture Http Client Error") {
HttpClientKt.captureHttpClientError()
}
Button("Hard Crash") {
LoginImpl().login(username: nil)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sample.kmp.app

import io.sentry.kotlin.multiplatform.Attachment
import io.sentry.kotlin.multiplatform.Context
import io.sentry.kotlin.multiplatform.HttpStatusCodeRange
import io.sentry.kotlin.multiplatform.OptionsConfiguration
import io.sentry.kotlin.multiplatform.Sentry

Expand Down Expand Up @@ -45,6 +46,8 @@ private fun optionsConfiguration(): OptionsConfiguration {
it.attachScreenshot = true
it.attachViewHierarchy = true
it.release = "[email protected]"
it.failedRequestStatusCodes = listOf(HttpStatusCodeRange(400, 599))
it.failedRequestTargets = listOf("httpbin.org")
it.beforeBreadcrumb = { breadcrumb ->
breadcrumb.message = "Add message before every breadcrumb"
breadcrumb
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sample.kmp.app

import platform.Foundation.NSURL
import platform.Foundation.NSURLRequest
import platform.Foundation.NSURLSession
import platform.Foundation.dataTaskWithRequest

@Suppress("Unused") // Called from Swift
fun captureHttpClientError() {
val url = NSURL(string = "https://httpbin.org/status/404")
val request = NSURLRequest(uRL = url)
NSURLSession.sharedSession.dataTaskWithRequest(request) { data, response, error ->
if (error != null) {
// handle error
println("error: $error")
} else {
// handle successful response
println("response: $response")
}
}.resume()
}