mirror of
https://github.com/mihonapp/mihon.git
synced 2025-06-26 19:17:51 +02:00
Preserve async stack traces of http requests
This commit is contained in:
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
Reference in New Issue
Block a user