dave08
02/08/2024, 4:50 PMtypealias KtorCtx = PipelineContext<Unit, ApplicationCall>
suspend inline fun <reified T : Any> KtorCtx.respond(
statusCode: HttpStatusCode = HttpStatusCode.OK,
noinline action: suspend Raise<ResponseError>.() -> T
): Unit = fold(
{ action() },
{ error ->
when(error) {
is ResponseError.ForbiddenResource -> call.respond(HttpStatusCode.Forbidden)
else -> {
val jsonError = Json.encodeToString(error)
call.respond(HttpStatusCode.BadRequest, error)
application.log.error(call.request.toLogString() + ": " + jsonError)
}
}
}) { a ->
call.respond(statusCode, a)
}
and I'd like to be able to pass it straight into the second param of Ktor's route functions, an example:
@KtorDsl
@JvmName("postTypedPath")
public inline fun <reified R : Any> <http://Route.post|Route.post>(
path: String,
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route = post(path) {
body(call.receive())
}
// Instead of:
...
post("/someRoute") {
respond {
...
}
}
// I'd like:
post("someRoute", respond {
})
I'd suppose fold isn't the right construct to make a lambda out of an Either, so what's the best way to do this with arrow?Youssef Shoaib [MOD]
02/08/2024, 5:37 PMsuspend inline fun <reified T : Any> respond(
statusCode: HttpStatusCode = HttpStatusCode.OK,
noinline action: suspend Raise<ResponseError>.() -> T
): suspend KtorCtx.(R) -> Unit = { fold(
{ action() },
{ error ->
when(error) {
is ResponseError.ForbiddenResource -> call.respond(HttpStatusCode.Forbidden)
else -> {
val jsonError = Json.encodeToString(error)
call.respond(HttpStatusCode.BadRequest, error)
application.log.error(call.request.toLogString() + ": " + jsonError)
}
}
}) { a ->
call.respond(statusCode, a)
}
}
dave08
02/11/2024, 10:52 AMeffect { ...; recover({ .. }) { .. } }
won't do it? Or maybe it's just not any better... I guess your way is clearer. Thanks!Youssef Shoaib [MOD]
02/11/2024, 10:56 AMR
Or take in extra contexts like KtorCtx