Skip to content
This repository was archived by the owner on Jun 6, 2019. It is now read-only.

Proper cause for Exceptions Deferred is resumed with #30

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,18 @@ class CoroutineCallAdapterFactory private constructor() : CallAdapter.Factory()
}
}

val exceptionWithCapturedStack = CoroutineCallException()
call.enqueue(object : Callback<T> {
override fun onFailure(call: Call<T>, t: Throwable) {
(t.findRootCause() ?: t).initCause(exceptionWithCapturedStack)
deferred.completeExceptionally(t)
}

override fun onResponse(call: Call<T>, response: Response<T>) {
if (response.isSuccessful) {
deferred.complete(response.body()!!)
} else {
deferred.completeExceptionally(HttpException(response))
deferred.completeExceptionally(HttpException(response).apply { initCause(exceptionWithCapturedStack) })
}
}
})
Expand All @@ -125,8 +127,10 @@ class CoroutineCallAdapterFactory private constructor() : CallAdapter.Factory()
}
}

val exceptionWithCapturedStack = CoroutineCallException()
call.enqueue(object : Callback<T> {
override fun onFailure(call: Call<T>, t: Throwable) {
(t.findRootCause() ?: t).initCause(exceptionWithCapturedStack)
deferred.completeExceptionally(t)
}

Expand All @@ -138,4 +142,19 @@ class CoroutineCallAdapterFactory private constructor() : CallAdapter.Factory()
return deferred
}
}

}

internal fun Throwable.findRootCause(): Throwable? {
var cause = cause ?: return null

while (true) {
val nextCause = cause.cause
when (nextCause) {
null, cause -> return cause
else -> cause = nextCause
}
}
}

private class CoroutineCallException : RuntimeException("Originally called here:")
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.SocketPolicy.DISCONNECT_AFTER_REQUEST
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -60,23 +61,30 @@ class DeferredTest {
@Test fun bodySuccess404() = runBlocking {
server.enqueue(MockResponse().setResponseCode(404))

val currentFrame = Exception().stackTrace[0]
val deferred = service.body()
try {
deferred.await()
fail()
} catch (e: HttpException) {
assertThat(e).hasMessageThat().isEqualTo("HTTP 404 Client Error")

val rootCauseIsCurrentMethod = e.findRootCause()?.stackTrace?.any { it.equalsExceptLine(currentFrame) } ?: false
assertTrue("Root cause needs to be current method", rootCauseIsCurrentMethod)
}
}

@Test fun bodyFailure() = runBlocking {
server.enqueue(MockResponse().setSocketPolicy(DISCONNECT_AFTER_REQUEST))

val currentFrame = Exception().stackTrace[0]
val deferred = service.body()
try {
deferred.await()
fail()
} catch (e: IOException) {
val rootCauseIsCurrentMethod = e.findRootCause()?.stackTrace?.any { it.equalsExceptLine(currentFrame) } ?: false
assertTrue("Root cause needs to be current method", rootCauseIsCurrentMethod)
}
}

Expand All @@ -101,11 +109,17 @@ class DeferredTest {
@Test fun responseFailure() = runBlocking {
server.enqueue(MockResponse().setSocketPolicy(DISCONNECT_AFTER_REQUEST))

val currentFrame = Exception().stackTrace[0]
val deferred = service.response()
try {
deferred.await()
fail()
} catch (e: IOException) {
val rootCauseIsCurrentMethod = e.findRootCause()?.stackTrace?.any { it.equalsExceptLine(currentFrame) } ?: false
assertTrue("Root cause needs to be current method", rootCauseIsCurrentMethod)
}
}

private fun StackTraceElement.equalsExceptLine(other: StackTraceElement) =
other.className == className && other.fileName == fileName && other.methodName == methodName
}