Implemented catching exceptions
This commit is contained in:
parent
fbaa9fa941
commit
2dff398baa
|
@ -9,10 +9,16 @@ import io.ktor.http.ContentType
|
|||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.cancel
|
||||
import net.dankito.banking.fints.util.log.LoggerFactory
|
||||
|
||||
|
||||
open class KtorWebClient : IWebClient {
|
||||
|
||||
companion object {
|
||||
private val log = LoggerFactory.getLogger(KtorWebClient::class)
|
||||
}
|
||||
|
||||
|
||||
protected val client = HttpClient() {
|
||||
|
||||
}
|
||||
|
@ -31,6 +37,7 @@ open class KtorWebClient : IWebClient {
|
|||
override fun post(url: String, body: String, contentType: String, userAgent: String): WebClientResponse {
|
||||
try {
|
||||
val job = GlobalScope.async {
|
||||
try {
|
||||
val clientResponse = client.post<HttpResponse>(url) {
|
||||
this.body = TextContent(body, contentType = ContentType.Application.OctetStream)
|
||||
}
|
||||
|
@ -38,6 +45,11 @@ open class KtorWebClient : IWebClient {
|
|||
val responseBody = clientResponse.readText()
|
||||
|
||||
WebClientResponse(clientResponse.status.value == 200, clientResponse.status.value, body = responseBody)
|
||||
} catch (e: Exception) {
|
||||
log.error(e) { "Could not send request to url '$url'" }
|
||||
|
||||
WebClientResponse(false, error = e)
|
||||
}
|
||||
}
|
||||
|
||||
while (job.isCompleted == false) { } // let's warm the CPU to get suspend function synchronous (runBlocking is not available in common projects)
|
||||
|
|
Loading…
Reference in New Issue