CrashReporter

interface CrashReporter

Interface for reporting exceptions to a crash reporting service.

This abstraction allows for flexible crash reporting implementations while keeping the application code decoupled from specific crash reporting libraries. The primary implementation uses Firebase Crashlytics, but alternative services can be easily substituted through dependency injection.

Thread Safety

Implementations must be thread-safe as this interface may be called from any thread.

Usage

In Repository Layer

class UserRepository @Inject constructor(
private val crashReporter: CrashReporter
) {
suspend fun fetchUser(): Result<User> = suspendRunCatching {
api.getUser()
}.onFailure { error ->
crashReporter.reportException(error)
}
}

In ViewModel (via updateStateWith/updateWith)

@HiltViewModel
class HomeViewModel @Inject constructor(
private val repository: HomeRepository,
private val crashReporter: CrashReporter
) : ViewModel() {
fun loadData() {
_uiState.updateStateWith {
repository.getData().onFailure { error ->
// Report non-fatal errors while still showing error to user
crashReporter.reportException(error)
}
}
}
}

In Global Exception Handler

class MyApplication : Application() {
@Inject lateinit var crashReporter: CrashReporter

override fun onCreate() {
super.onCreate()
Thread.setDefaultUncaughtExceptionHandler { _, throwable ->
crashReporter.reportException(throwable)
}
}
}

See also

Functions

Link copied to clipboard
abstract fun reportException(throwable: Throwable)

Reports a non-fatal exception to the crash reporting service.