Skip to content

Commit 57e75a3

Browse files
frantisek-nagyFrantisek Nagy
authored andcommitted
Make HttpException implement CopyableThrowable + add test
1 parent 6b6984e commit 57e75a3

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

retrofit/kotlin-test/src/test/java/retrofit2/KotlinSuspendTest.kt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package retrofit2
1818
import kotlinx.coroutines.CoroutineDispatcher
1919
import kotlinx.coroutines.GlobalScope
2020
import kotlinx.coroutines.async
21+
import kotlinx.coroutines.launch
2122
import kotlinx.coroutines.runBlocking
2223
import kotlinx.coroutines.withContext
2324
import okhttp3.OkHttpClient
@@ -36,6 +37,9 @@ import retrofit2.http.GET
3637
import retrofit2.http.HEAD
3738
import retrofit2.http.Path
3839
import java.io.IOException
40+
import java.io.PrintWriter
41+
import java.io.StringWriter
42+
import java.lang.Runnable
3943
import java.lang.reflect.ParameterizedType
4044
import java.lang.reflect.Type
4145
import kotlin.coroutines.CoroutineContext
@@ -202,6 +206,58 @@ class KotlinSuspendTest {
202206
runBlocking { example.headUnit() }
203207
}
204208

209+
@Test fun await404() {
210+
val retrofit = Retrofit.Builder()
211+
.baseUrl(server.url("/"))
212+
.addConverterFactory(ToStringConverterFactory())
213+
.build()
214+
val example = retrofit.create(Service::class.java)
215+
216+
server.enqueue(MockResponse().setResponseCode(404))
217+
218+
try {
219+
runBlocking {
220+
val deferred = async { example.body() }
221+
222+
deferred.await()
223+
}
224+
fail()
225+
} catch (e: HttpException) {
226+
val writer = StringWriter()
227+
e.printStackTrace(PrintWriter(writer))
228+
val trace = writer.toString()
229+
230+
assertThat(trace).contains("KotlinSuspendTest")
231+
assertThat(trace).contains("await404")
232+
}
233+
}
234+
235+
@Test fun launch404() {
236+
val retrofit = Retrofit.Builder()
237+
.baseUrl(server.url("/"))
238+
.addConverterFactory(ToStringConverterFactory())
239+
.build()
240+
val example = retrofit.create(Service::class.java)
241+
242+
server.enqueue(MockResponse().setResponseCode(404))
243+
244+
try {
245+
runBlocking {
246+
val job = launch { example.body() }
247+
248+
job.join()
249+
}
250+
fail()
251+
} catch (e: HttpException) {
252+
val writer = StringWriter()
253+
e.printStackTrace(PrintWriter(writer))
254+
val trace = writer.toString()
255+
256+
assertThat(trace).contains("KotlinSuspendTest")
257+
assertThat(trace).contains("launch404")
258+
}
259+
}
260+
205261
@Test fun params() {
206262
val retrofit = Retrofit.Builder()
207263
.baseUrl(server.url("/"))

retrofit/src/main/java/retrofit2/KotlinExtensions.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package retrofit2
2020

21+
import kotlinx.coroutines.CopyableThrowable
2122
import kotlinx.coroutines.Dispatchers
2223
import kotlinx.coroutines.suspendCancellableCoroutine
2324
import java.lang.reflect.ParameterizedType
@@ -51,7 +52,7 @@ suspend fun <T : Any> Call<T>.await(): T {
5152
continuation.resume(body)
5253
}
5354
} else {
54-
continuation.resumeWithException(HttpException(response))
55+
continuation.resumeWithException(KotlinHttpException(response))
5556
}
5657
}
5758

@@ -73,7 +74,7 @@ suspend fun <T : Any> Call<T?>.await(): T? {
7374
if (response.isSuccessful) {
7475
continuation.resume(response.body())
7576
} else {
76-
continuation.resumeWithException(HttpException(response))
77+
continuation.resumeWithException(KotlinHttpException(response))
7778
}
7879
}
7980

@@ -124,3 +125,14 @@ internal suspend fun Exception.suspendAndThrow(): Nothing {
124125
COROUTINE_SUSPENDED
125126
}
126127
}
128+
129+
private class KotlinHttpException(
130+
private val response: Response<*>
131+
) : HttpException(response), CopyableThrowable<KotlinHttpException> {
132+
133+
override fun createCopy(): KotlinHttpException {
134+
val result = KotlinHttpException(response)
135+
result.initCause(this)
136+
return result
137+
}
138+
}

0 commit comments

Comments
 (0)