Preserve async stack traces of http requests

This commit is contained in:
NerdNumber9
2019-04-14 00:27:44 -04:00
parent 8119eb4b34
commit 8a70dffae8
2 changed files with 18 additions and 0 deletions

View File

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.network
import exh.util.withRootCause
import okhttp3.Call
import okhttp3.OkHttpClient
import okhttp3.Request
@ -47,11 +48,18 @@ fun Call.asObservable(): Observable<Response> {
}
fun Call.asObservableSuccess(): Observable<Response> {
// Record stacktrace at creation time for easier debugging
// asObservable is involved in a lot of crashes so this is worth the performance hit
val asyncStackTrace = Exception("Async stacktrace")
return asObservable().doOnNext { response ->
if (!response.isSuccessful) {
response.close()
throw Exception("HTTP error ${response.code()}")
}
}.onErrorReturn {
// Set root cause to async stacktrace and throw again
throw it.withRootCause(asyncStackTrace)
}
}

View File

@ -4,4 +4,14 @@ inline fun <T> ignore(expr: () -> T): T? {
return try { expr() } catch (t: Throwable) { null }
}
fun <T : Throwable> T.withRootCause(cause: Throwable): T {
val curCause = this.cause
if(curCause == null) {
this.initCause(cause)
} else {
curCause.withRootCause(cause)
}
return this
}