Skip to content

Commit

Permalink
Fix error boundary (#214)
Browse files Browse the repository at this point in the history
* Fix runblocking behavior for error handling

* tmp
  • Loading branch information
xinlili-statsig authored Apr 26, 2024
1 parent 247c7cf commit 5736d6f
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions src/main/java/com/statsig/androidsdk/ErrorBoundary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.statsig.androidsdk

import com.google.gson.Gson
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.DataOutputStream
import java.lang.RuntimeException
import java.net.HttpURLConnection
Expand Down Expand Up @@ -62,6 +65,12 @@ internal class ErrorBoundary() {
}
}

fun getNoopExceptionHandler(): CoroutineExceptionHandler {
return CoroutineExceptionHandler { _, _ ->
// No-op
}
}

fun capture(task: () -> Unit, tag: String? = null, recover: ((exception: Exception?) -> Unit)? = null, configName: String? = null) {
var markerID = ""
try {
Expand Down Expand Up @@ -95,35 +104,36 @@ internal class ErrorBoundary() {

internal fun logException(exception: Throwable) {
try {
if (apiKey == null) {
return
}

val name = exception.javaClass.canonicalName ?: exception.javaClass.name
if (seen.contains(name)) {
return
CoroutineScope(this.getNoopExceptionHandler() + Dispatchers.IO).launch {
if (apiKey == null) {
return@launch
}

val name = exception.javaClass.canonicalName ?: exception.javaClass.name
if (seen.contains(name)) {
return@launch
}

seen.add(name)

val metadata = statsigMetadata ?: StatsigMetadata("")
val url = URL(getUrl())
val body = mapOf(
"exception" to name,
"info" to RuntimeException(exception).stackTraceToString(),
"statsigMetadata" to metadata,
)
val postData = Gson().toJson(body)

val conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "POST"
conn.doOutput = true
conn.setRequestProperty("Content-Type", "application/json")
conn.setRequestProperty("STATSIG-API-KEY", apiKey)
conn.useCaches = false
DataOutputStream(conn.outputStream).use { it.writeBytes(postData) }
conn.responseCode // triggers request
}

seen.add(name)

val metadata = statsigMetadata ?: StatsigMetadata("")
val url = URL(getUrl())
val body = mapOf(
"exception" to name,
"info" to RuntimeException(exception).stackTraceToString(),
"statsigMetadata" to metadata,
)
val postData = Gson().toJson(body)

val conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "POST"
conn.doOutput = true
conn.setRequestProperty("Content-Type", "application/json")
conn.setRequestProperty("STATSIG-API-KEY", apiKey)
conn.useCaches = false

DataOutputStream(conn.outputStream).use { it.writeBytes(postData) }
conn.responseCode // triggers request
} catch (e: Exception) {
// noop
}
Expand Down

0 comments on commit 5736d6f

Please sign in to comment.