Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make HttpException implement CopyableThrowable + add test #3475

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions retrofit/kotlin-test/src/test/java/retrofit2/KotlinSuspendTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package retrofit2
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
Expand All @@ -36,6 +37,9 @@ import retrofit2.http.GET
import retrofit2.http.HEAD
import retrofit2.http.Path
import java.io.IOException
import java.io.PrintWriter
import java.io.StringWriter
import java.lang.Runnable
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import kotlin.coroutines.CoroutineContext
Expand Down Expand Up @@ -202,6 +206,58 @@ class KotlinSuspendTest {
runBlocking { example.headUnit() }
}

@Test fun await404() {
val retrofit = Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(ToStringConverterFactory())
.build()
val example = retrofit.create(Service::class.java)

server.enqueue(MockResponse().setResponseCode(404))

try {
runBlocking {
val deferred = async { example.body() }

deferred.await()
}
fail()
} catch (e: HttpException) {
val writer = StringWriter()
e.printStackTrace(PrintWriter(writer))
val trace = writer.toString()

assertThat(trace).contains("KotlinSuspendTest")
assertThat(trace).contains("await404")
}
}

@Test fun launch404() {
val retrofit = Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(ToStringConverterFactory())
.build()
val example = retrofit.create(Service::class.java)

server.enqueue(MockResponse().setResponseCode(404))

try {
runBlocking {
val job = launch { example.body() }

job.join()
}
fail()
} catch (e: HttpException) {
val writer = StringWriter()
e.printStackTrace(PrintWriter(writer))
val trace = writer.toString()

assertThat(trace).contains("KotlinSuspendTest")
assertThat(trace).contains("launch404")
}
}

@Test fun params() {
val retrofit = Retrofit.Builder()
.baseUrl(server.url("/"))
Expand Down
16 changes: 14 additions & 2 deletions retrofit/src/main/java/retrofit2/KotlinExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package retrofit2

import kotlinx.coroutines.CopyableThrowable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.suspendCancellableCoroutine
import java.lang.reflect.ParameterizedType
Expand Down Expand Up @@ -51,7 +52,7 @@ suspend fun <T : Any> Call<T>.await(): T {
continuation.resume(body)
}
} else {
continuation.resumeWithException(HttpException(response))
continuation.resumeWithException(KotlinHttpException(response))
}
}

Expand All @@ -73,7 +74,7 @@ suspend fun <T : Any> Call<T?>.await(): T? {
if (response.isSuccessful) {
continuation.resume(response.body())
} else {
continuation.resumeWithException(HttpException(response))
continuation.resumeWithException(KotlinHttpException(response))
}
}

Expand Down Expand Up @@ -124,3 +125,14 @@ internal suspend fun Exception.suspendAndThrow(): Nothing {
COROUTINE_SUSPENDED
}
}

private class KotlinHttpException(
private val response: Response<*>
) : HttpException(response), CopyableThrowable<KotlinHttpException> {

override fun createCopy(): KotlinHttpException {
val result = KotlinHttpException(response)
result.initCause(this)
return result
}
}