dave08
04/13/2023, 12:05 PMfold
handles TimeoutCancellationException
s? It seems like they're rethrown... but in Ktor, I'd like to use it to return some kind of timeout response to the client... (I suspect that Ktor just hangs w/o some kind of timeout in certain situations...)private suspend inline fun KtorCtx.respond(
statusCode: HttpStatusCode,
noinline action: suspend Raise<ErrorContext>.() -> String
): Unit = fold(
{
withTimeout(5.seconds) {
action()
}
},
{
if (it is TimeoutCancellationException)
call.respond(RequestTimeout)
else
throw it
},
{ error ->
val jsonError = Json.encodeToString(error)
call.respond(BadRequest, error.toString())
application.log.error(jsonError)
}) { a ->
call.respondText(a, ContentType.Application.Json, statusCode)
}
stojan
04/13/2023, 12:12 PMwithTimeoutOrNull
dave08
04/13/2023, 12:17 PM?: ""
private suspend inline fun KtorCtx.respond(
statusCode: HttpStatusCode,
noinline action: suspend Raise<ErrorContext>.() -> String
): Unit = fold(
{
withTimeoutOrNull(5.seconds) {
action()
}?.also { call.respond(RequestTimeout) } ?: ""
},
{ error ->
val jsonError = Json.encodeToString(error)
call.respond(BadRequest, error.toString())
application.log.error(jsonError)
}) { a ->
call.respondText(a, ContentType.Application.Json, statusCode)
}
a
isn't a type respondText
accepts...simon.vergauwen
04/13/2023, 1:37 PMTimeoutCancellation
inherits from CancellationException
. There is some discussion on KotlinX Coroutines to change this, but it’s a binar breaking change.
They might change this is 2.0, but you can use withTimeoutOrNull
& custom exception or Java’s TimeoutException