I have this function ```typealias KtorCtx = Pipeli...
# arrow
d
I have this function
Copy code
typealias 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:
Copy code
@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?
y
It should be as simple as:
Copy code
suspend 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)
}
}
☝️ 1
d
So something like
effect { ...; recover({ .. }) { .. } }
won't do it? Or maybe it's just not any better... I guess your way is clearer. Thanks!
y
Effect does the same thing as a lambda, but it doesn't allow you to change the shape to accept extra arguments like
R
Or take in extra contexts like
KtorCtx
👌🏼 1