Improve ML exception handler

- Throw an esception in debug build
- Add a log line to help debugging
- Move it to the companion object for safety
This commit is contained in:
Geoffrey Métais
2019-10-17 16:45:45 +02:00
parent 9c88015ae7
commit 5fb016eab9

View File

@@ -83,24 +83,6 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec
private var discoverTriggered = false
internal val sb = StringBuilder()
private val exceptionHandler = if (BuildConfig.BETA) AbstractMedialibrary.MedialibraryExceptionHandler { context, errMsg, _ ->
val intent = Intent(applicationContext, SendCrashActivity::class.java).apply {
putExtra(CRASH_ML_CTX, context)
putExtra(CRASH_ML_MSG, errMsg)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
// Lock the Medialibrary thread during DB extraction.
runBlocking {
SendCrashActivity.job = Job()
try {
startActivity(intent)
SendCrashActivity.job?.join()
} catch (e: Exception) {
SendCrashActivity.job = null
}
}
} else null
private val notificationActor by lazy {
actor<Notification>(capacity = Channel.UNLIMITED) {
for (update in channel) when (update) {
@@ -419,6 +401,7 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec
val context = this@MediaParsingService
var shouldInit = !dbExists()
val initCode = medialibrary.init(context)
setExceptionHandler()
medialibrary.exceptionHandler = exceptionHandler
if (initCode != AbstractMedialibrary.ML_INIT_ALREADY_INITIALIZED) {
shouldInit = shouldInit or (initCode == AbstractMedialibrary.ML_INIT_DB_RESET) or (initCode == AbstractMedialibrary.ML_INIT_DB_CORRUPTED)
@@ -469,6 +452,32 @@ class MediaParsingService : Service(), DevicesDiscoveryCb, CoroutineScope, Lifec
val progress = MutableLiveData<ScanProgress>()
val newStorages = MutableLiveData<MutableList<String>>()
val preselectedStorages = mutableListOf<String>()
var exceptionHandler : AbstractMedialibrary.MedialibraryExceptionHandler? = null
}
private fun setExceptionHandler() {
if (exceptionHandler !== null) return
exceptionHandler = if (BuildConfig.BETA) AbstractMedialibrary.MedialibraryExceptionHandler { context, errMsg, _ ->
val intent = Intent(applicationContext, SendCrashActivity::class.java).apply {
putExtra(CRASH_ML_CTX, context)
putExtra(CRASH_ML_MSG, errMsg)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
Log.wtf(TAG, "medialibrary reported unhandled exception: -----------------")
// Lock the Medialibrary thread during DB extraction.
runBlocking {
SendCrashActivity.job = Job()
try {
startActivity(intent)
SendCrashActivity.job?.join()
} catch (e: Exception) {
SendCrashActivity.job = null
}
}
} else if (BuildConfig.DEBUG) AbstractMedialibrary.MedialibraryExceptionHandler { context, errMsg, _ ->
throw IllegalStateException("$context:\n$errMsg")
} else null
}
}