Sean Keane
05/27/2020, 12:41 PMinternal class MainScope<T>(private val coroutineContextProvider: CoroutineContextProvider, private val response: (Response<T>) -> Unit) : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = coroutineContextProvider.context() + job + exceptionHandler
private val job = SupervisorJob()
//This needs more investigation with thread scope
private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
logWarn(throwable.message.orEmpty())
response.invoke(Response(error = throwable, value = null))
}
}
When an exception occurs I want to capture it and pass it back through the response object.
class Response<out T>(val value:T?, val error: Throwable? = null) {
fun isSuccess(block: (T) -> Unit): Response<T> {
value?.let(block)
return this
}
fun isFailure(block: (Throwable) -> Unit): Response<T> {
error?.let(block)
return this
}
}
But the exception handler never gets called when a failure occurs.
This is how I launch.
MainScope(coroutineContextProvider, onResponse).launch {
//CODE
}
Am I missing something for handling the exception or should it just be captured when an exception occursfatih
05/27/2020, 7:16 PMval handler = CoroutineExceptionHandler{ context, exception -> response.doSomething(exception) }
And give it like scope.launch(handler) {...}
Sean Keane
05/27/2020, 8:46 PMSean Keane
05/27/2020, 10:56 PM