https://kotlinlang.org logo
#ktor
Title
# ktor
l

Levi

12/13/2023, 2:29 PM
Not necessarily a Ktor question, but I'm getting an error that
suspend function can only be called from a coroutine
when I try using Arrow's Either.fold inside of `post {...}`:
Copy code
fun Route.doTheThing(client: Client) {
  post() {
    Either.catchOrThrow<Exception, DoTheThingRequestBody> {
        call.receive<DoTheThingRequestBody>()
      }
      .fold(
        { call.respond(HttpStatusCode.BadRequest, "Invalid email") },
        {
          val response = client.doThing(it.email)
          call.respondText(response.bodyAsText(), ContentType.Application.Json, response.status)
        }
      )
  }
}
Compiler doesn't like the
call.Respond...
calls. Do I need to somehow use an explicit receiver to disambiguate? Can't seem to figure this out :|
a

Aleksei Tirman [JB]

12/14/2023, 7:31 AM
The compilator is able to compile the following code:
Copy code
data class DoTheThingRequestBody(val x: Int)

fun Route.doTheThing(client: HttpClient) {
    post {
        Either.catchOrThrow<Exception, DoTheThingRequestBody> {
            call.receive<DoTheThingRequestBody>()
        }
            .fold(
                { call.respond(HttpStatusCode.BadRequest, "Invalid email") },
                {
                    val response = client.get("")
                    call.respondText(response.bodyAsText(), ContentType.Application.Json, response.status)
                }
            )
    }
}
l

Levi

12/15/2023, 5:01 PM
doh! wow, thank you @Aleksei Tirman [JB] Really appreciate the reply, as always