Skip to content

Commit 80c1aad

Browse files
committed
runCatching CancellationException issues fixed
1 parent 1134e09 commit 80c1aad

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

ktor-boost/src/commonMain/kotlin/KtorDeferredResultExtension.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public suspend inline fun <reified T> HttpClient.getResultAsync(
2424
noinline block: HttpRequestBuilder.() -> Unit = {},
2525
): Deferred<Result<T>> =
2626
coroutineScope {
27-
async { runCatching { get(urlString, block).body() } }
27+
async { runSafeSuspendCatching { get(urlString, block).body() } }
2828
}
2929

3030
/**
@@ -39,7 +39,7 @@ public suspend inline fun <reified T> HttpClient.postResultAsync(
3939
noinline block: HttpRequestBuilder.() -> Unit = {},
4040
): Deferred<Result<T>> =
4141
coroutineScope {
42-
async { runCatching { post(urlString, block).body() } }
42+
async { runSafeSuspendCatching { post(urlString, block).body() } }
4343
}
4444

4545
/**
@@ -54,7 +54,7 @@ public suspend inline fun <reified T> HttpClient.putResultAsync(
5454
noinline block: HttpRequestBuilder.() -> Unit = {},
5555
): Deferred<Result<T>> =
5656
coroutineScope {
57-
async { runCatching { put(urlString, block).body() } }
57+
async { runSafeSuspendCatching { put(urlString, block).body() } }
5858
}
5959

6060
/**
@@ -69,7 +69,7 @@ public suspend inline fun <reified T> HttpClient.deleteResultAsync(
6969
noinline block: HttpRequestBuilder.() -> Unit = {},
7070
): Deferred<Result<T>> =
7171
coroutineScope {
72-
async { runCatching { delete(urlString, block).body() } }
72+
async { runSafeSuspendCatching { delete(urlString, block).body() } }
7373
}
7474

7575
/**
@@ -84,7 +84,7 @@ public suspend inline fun <reified T> HttpClient.patchResultAsync(
8484
noinline block: HttpRequestBuilder.() -> Unit = {},
8585
): Deferred<Result<T>> =
8686
coroutineScope {
87-
async { runCatching { patch(urlString, block).body() } }
87+
async { runSafeSuspendCatching { patch(urlString, block).body() } }
8888
}
8989

9090
/**
@@ -99,7 +99,7 @@ public suspend inline fun <reified T> HttpClient.headResultAsync(
9999
noinline block: HttpRequestBuilder.() -> Unit = {},
100100
): Deferred<Result<T>> =
101101
coroutineScope {
102-
async { runCatching { head(urlString, block).body() } }
102+
async { runSafeSuspendCatching { head(urlString, block).body() } }
103103
}
104104

105105
/**
@@ -114,5 +114,5 @@ public suspend inline fun <reified T> HttpClient.optionsResultAsync(
114114
noinline block: HttpRequestBuilder.() -> Unit = {},
115115
): Deferred<Result<T>> =
116116
coroutineScope {
117-
async { runCatching { options(urlString, block).body() } }
117+
async { runSafeSuspendCatching { options(urlString, block).body() } }
118118
}

ktor-boost/src/commonMain/kotlin/KtorResultExtension.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import io.ktor.client.request.put
1919
suspend inline fun <reified T> HttpClient.getResult(
2020
urlString: String,
2121
noinline block: HttpRequestBuilder.() -> Unit = {},
22-
): Result<T> = runCatching { get(urlString, block).body() }
22+
): Result<T> = runSafeSuspendCatching { get(urlString, block).body() }
2323

2424
/**
2525
* Performs an HTTP POST request synchronously and returns the result as a [Result] of type [T].
@@ -31,7 +31,7 @@ suspend inline fun <reified T> HttpClient.getResult(
3131
suspend inline fun <reified T> HttpClient.postResult(
3232
urlString: String,
3333
noinline block: HttpRequestBuilder.() -> Unit = {},
34-
): Result<T> = runCatching { post(urlString, block).body() }
34+
): Result<T> = runSafeSuspendCatching { post(urlString, block).body() }
3535

3636
/**
3737
* Performs an HTTP PUT request synchronously and returns the result as a [Result] of type [T].
@@ -43,7 +43,7 @@ suspend inline fun <reified T> HttpClient.postResult(
4343
suspend inline fun <reified T> HttpClient.putResult(
4444
urlString: String,
4545
noinline block: HttpRequestBuilder.() -> Unit = {},
46-
): Result<T> = runCatching { put(urlString, block).body() }
46+
): Result<T> = runSafeSuspendCatching { put(urlString, block).body() }
4747

4848
/**
4949
* Performs an HTTP DELETE request synchronously and returns the result as a [Result] of type [T].
@@ -55,7 +55,7 @@ suspend inline fun <reified T> HttpClient.putResult(
5555
suspend inline fun <reified T> HttpClient.deleteResult(
5656
urlString: String,
5757
noinline block: HttpRequestBuilder.() -> Unit = {},
58-
): Result<T> = runCatching { delete(urlString, block).body() }
58+
): Result<T> = runSafeSuspendCatching { delete(urlString, block).body() }
5959

6060
/**
6161
* Performs an HTTP PATCH request synchronously and returns the result as a [Result] of type [T].
@@ -67,7 +67,7 @@ suspend inline fun <reified T> HttpClient.deleteResult(
6767
suspend inline fun <reified T> HttpClient.patchResult(
6868
urlString: String,
6969
noinline block: HttpRequestBuilder.() -> Unit = {},
70-
): Result<T> = runCatching { patch(urlString, block).body() }
70+
): Result<T> = runSafeSuspendCatching { patch(urlString, block).body() }
7171

7272
/**
7373
* Performs an HTTP HEAD request synchronously and returns the result as a [Result] of type [T].
@@ -79,7 +79,7 @@ suspend inline fun <reified T> HttpClient.patchResult(
7979
suspend inline fun <reified T> HttpClient.headResult(
8080
urlString: String,
8181
noinline block: HttpRequestBuilder.() -> Unit = {},
82-
): Result<T> = runCatching { head(urlString, block).body() }
82+
): Result<T> = runSafeSuspendCatching { head(urlString, block).body() }
8383

8484
/**
8585
* Performs an HTTP OPTIONS request synchronously and returns the result as a [Result] of type [T].
@@ -91,4 +91,4 @@ suspend inline fun <reified T> HttpClient.headResult(
9191
suspend inline fun <reified T> HttpClient.optionsResult(
9292
urlString: String,
9393
noinline block: HttpRequestBuilder.() -> Unit = {},
94-
): Result<T> = runCatching { options(urlString, block).body() }
94+
): Result<T> = runSafeSuspendCatching { options(urlString, block).body() }

ktor-boost/src/commonTest/kotlin/HttpClientExtensionsTest.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import io.ktor.client.*
2-
import io.ktor.client.engine.mock.*
3-
import io.ktor.client.request.*
4-
import io.ktor.http.*
1+
import io.ktor.client.HttpClient
2+
import io.ktor.client.engine.mock.MockEngine
3+
import io.ktor.client.engine.mock.respondOk
4+
import io.ktor.http.HttpMethod
55
import kotlinx.coroutines.runBlocking
66
import kotlin.test.Test
77
import kotlin.test.assertEquals
@@ -32,6 +32,9 @@ class HttpClientExtensionsTest {
3232
runBlocking {
3333
val result = httpClient.getResult<String>("sample_get_url")
3434

35+
result.onSuccess {
36+
}.onFailure {
37+
}
3538
println(result.toString())
3639

3740
assertTrue(result.isSuccess)
@@ -92,7 +95,6 @@ class HttpClientExtensionsTest {
9295
fun `test headResult extension function`() {
9396
runBlocking {
9497
val result = httpClient.headResult<String>("sample_head_url")
95-
9698
assertTrue(result.isSuccess)
9799
val responseBody = result.getOrNull()
98100
assertNotNull(responseBody)

0 commit comments

Comments
 (0)