Stylianos Gakis
08/15/2021, 10:15 PMignoreUnknownKeys = true
.
And I am using the post("path") { myType: MyType -> //code }
syntax. I could potentially change this with
val login: Login = try {
call.receive()
} catch (e: SerializationException) {
return@post call.respondWith(Response.badRequest(InvalidInput))
}
Or even val login: Login = call.receiveOrNull() ?: return@post call.respondWith(Response.badRequest(InvalidInput))
But I don’t necessarily like this if there is an alternative.
How would I deal with this? Can I create an interceptor that catches all SerializationExceptions? Something else? Is my proposed solution the only way? I really do want to use the typed version of the post() method if possible.CLOVIS
08/15/2021, 11:07 PMAleksei Tirman [JB]
08/16/2021, 11:21 AMfun main(args: Array<String>) {
embeddedServer(Netty, port = 7070, host = "0.0.0.0") {
install(StatusPages) {
exception<kotlinx.serialization.SerializationException> {
call.respondText(status = HttpStatusCode.BadRequest, text = it.message ?: "")
}
}
install(ContentNegotiation) {
json()
}
routing {
post("/") { data: Req ->
call.respondText { "x: ${data.x}" }
}
}
}.start(wait = true)
}
@Serializable
data class Req(val x: Int)
Stylianos Gakis
08/16/2021, 11:24 AMreceiveOrNull()
idea for more specialized handling of specific cases. Thank you! And thanks for the code snippet too. The documentation was super easy to follow to install the StatusPages myself too 😊