ktor question Hello, does anyone have experience ...
# server
s
ktor question Hello, does anyone have experience with receiving invalid JSON objects using
call.receive<T>()
? Currently my server returns
500 Internal Server Error
, I would like to return
400 Bad Request
in some general reusable fashion. I can't seem to find any resources regarding this topic, Thanks!
s
Use Status Pages to intercept the exception in your stack trace and change the response code
Copy code
install(StatusPages) {
    exception<Exception> { cause ->
        call.respond(HttpStatusCode.BadRequest)
    }
}
✔️ 1
s
Woah, thank you very much! That is exactly what I needed!
👍🏾 1
c
i made a pr for this but it never got merged. https://github.com/ktorio/ktor/pull/2242
l
I'm doing the same as @sultanofcardio
Copy code
install(StatusPages) {
    exception<JsonSyntaxException> {
      call.respond(HttpStatusCode.BadRequest, Error(it.message))
    }
  }