the `BadRequestException` from Ktor just gives me ...
# ktor
j
the
BadRequestException
from Ktor just gives me “Illegal Input” and doesn’t actually say whats wrong with the request. Is there a way to improve it?
s
The
cause
references the original exception from KotlinX Serialization, and that should give you more information. https://github.com/ktorio/ktor/blob/d7d244cf9f1a8f91675a161b5b97f5ad9681c594/ktor-[…]/io/ktor/serialization/kotlinx/KotlinxSerializationConverter.kt
I'm not sure how the
JsonException
gets turned into
BadRequest
🤔
j
Oh, the problem was me then. I was returning the cause but forgot to log it. Sorry and thanks!
One last question, do you know if I can return the errors as list in this case?
s
Sadly I think KotlinX Serialization only returns a string of the first value it encounters that is not correct. You can also manually work with raw
Json
, and combining it with Arrow to do validation. This DSL makes it easy to work, and inspect raw
JsonElement
. https://github.com/nomisRev/kotlinx-serialization-jsonpath
I take a similar approach when I need to validate query or path parameters and want to get a complete list of the errors.
s
I just filed an issue on this on YouTrack, as the
Logging
plugin is essentially unable to display a proper deserialization error message. https://youtrack.jetbrains.com/issue/KTOR-5421
500 1
617 Views