Skip to content

Commit 22d9a1e

Browse files
committed
chore: add unhandled global exception callback for bugsnag
ensure we dispatch any unexpected exceptions to bugsnag with our notify_slack attribute for visibility Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 96a651d commit 22d9a1e

3 files changed

Lines changed: 48 additions & 23 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.flipcash.app.internal.debug
2+
3+
import com.bugsnag.android.OnErrorCallback
4+
5+
internal val FlipcashErrorCallback = OnErrorCallback onError@{ event ->
6+
// Only tag unhandled errors — handled errors go through ErrorUtils.handleError()
7+
if (!event.isUnhandled) return@onError true
8+
val error = event.originalError ?: return@onError true
9+
10+
val cause = error.cause ?: error
11+
event.addMetadata("alert", "slack_notify", true)
12+
event.addMetadata("alert", "error_type", cause.javaClass.simpleName)
13+
event.addMetadata("alert", "error_family", cause.javaClass.enclosingClass?.simpleName ?: "Unknown")
14+
true
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.flipcash.app.internal.debug
2+
3+
import timber.log.Timber
4+
5+
internal val FlipcashDebugTree = object : Timber.DebugTree() {
6+
override fun createStackElementTag(element: StackTraceElement): String {
7+
val elementTag = super.createStackElementTag(element)
8+
.orEmpty()
9+
.split("$")
10+
.filter { it.isNotEmpty() }
11+
.take(2)
12+
.joinToString(" ")
13+
.replace("_", " ")
14+
15+
val methodName = element.methodName
16+
.split("$")
17+
.firstOrNull()
18+
.orEmpty()
19+
20+
return String.format(
21+
"%s | %s ",
22+
elementTag,
23+
methodName
24+
)
25+
}
26+
}

apps/flipcash/app/src/main/kotlin/com/flipcash/app/internal/startup/TraceInitializer.kt

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package com.flipcash.app.internal.startup
33
import android.content.Context
44
import androidx.startup.Initializer
55
import com.bugsnag.android.Bugsnag
6+
import com.bugsnag.android.Configuration
67
import com.flipcash.app.android.BuildConfig
8+
import com.flipcash.app.internal.debug.FlipcashDebugTree
9+
import com.flipcash.app.internal.debug.FlipcashErrorCallback
710
import kotlinx.coroutines.CoroutineScope
811
import kotlinx.coroutines.Dispatchers
912
import kotlinx.coroutines.launch
@@ -12,31 +15,12 @@ import timber.log.Timber
1215
class TraceInitializer: Initializer<Unit> {
1316
override fun create(context: Context) {
1417
if (BuildConfig.DEBUG) {
15-
Timber.plant(object : Timber.DebugTree() {
16-
override fun createStackElementTag(element: StackTraceElement): String {
17-
val elementTag = super.createStackElementTag(element)
18-
.orEmpty()
19-
.split("$")
20-
.filter { it.isNotEmpty() }
21-
.take(2)
22-
.joinToString(" ")
23-
.replace("_", " ")
24-
25-
val methodName = element.methodName
26-
.split("$")
27-
.firstOrNull()
28-
.orEmpty()
29-
30-
return String.format(
31-
"%s | %s ",
32-
elementTag,
33-
methodName
34-
)
35-
}
36-
})
18+
Timber.plant(FlipcashDebugTree)
3719
} else {
3820
CoroutineScope(Dispatchers.IO).launch {
39-
Bugsnag.start(context)
21+
val config = Configuration.load(context)
22+
config.addOnError(FlipcashErrorCallback)
23+
Bugsnag.start(context, config)
4024
}
4125
}
4226
}

0 commit comments

Comments
 (0)