-
Notifications
You must be signed in to change notification settings - Fork 8
/
EmbraceUncaughtExceptionHandler.kt
39 lines (33 loc) · 1.36 KB
/
EmbraceUncaughtExceptionHandler.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package io.embrace.android.embracesdk.capture.crash
import io.embrace.android.embracesdk.logging.EmbLogger
import io.embrace.android.embracesdk.logging.InternalErrorType
/**
* Intercepts uncaught exceptions from the JVM and forwards them to the Embrace API. Once handled,
* the exception is then delegated to the default [Thread.UncaughtExceptionHandler].
*/
internal class EmbraceUncaughtExceptionHandler(
/**
* The default uncaught exception handler; is null if not set.
*/
private val defaultHandler: Thread.UncaughtExceptionHandler?,
/**
* The crash service which will submit the exception to the API as a crash
*/
private val crashService: CrashService,
private val logger: EmbLogger
) : Thread.UncaughtExceptionHandler {
init {
logger.logDebug("Registered EmbraceUncaughtExceptionHandler")
}
override fun uncaughtException(thread: Thread, exception: Throwable) {
try {
crashService.handleCrash(exception)
} catch (ex: Exception) {
logger.logError("Error occurred in the uncaught exception handler", ex)
logger.trackInternalError(InternalErrorType.UNCAUGHT_EXC_HANDLER, ex)
} finally {
logger.logDebug("Finished handling exception. Delegating to default handler.")
defaultHandler?.uncaughtException(thread, exception)
}
}
}