Hi, I am trying to use lens (i started with klaxon...
# http4k
n
Hi, I am trying to use lens (i started with klaxon) for serialize/deserialize. In one test I get this error "org.http4k.lens.LensFailure: body 'body' must be object" but the request I pass seems correct ... is a memory request built like this :
Copy code
Request(<http://Method.POST|Method.POST>, "/api/mav-request-batch").body(payload)
payload is a string containing some json ... don't understand where the error is .....
the lens is defined like this:
Copy code
val mavRequestBatchLens = Body.auto<MavRequestBatch>().toLens()
implementation "org.http4k:http4k-format-jackson:4.43.1.0"
d
The message is generic but if you look at the cause you should see where it failed.we are sort of stuck with the generic top level message due to the lens system
n
Uh you are right Dave sorry! the error was down below in the stack trace, haven't see it
It is a validation test, trying to test that invalid json is rejected
so the test is passed, need to parse the error message though
d
it's ok - it isn't very obvious for sure!
a
Have there been any attempts to try to extract more useful error messages? or is it just a question of effort?
d
I have had a look in the past. Changing it would be a bit problematic IIRC, but I'm not adverse to doing it if we can find a solution that doesn't compromise the entire lens infrastructure. We do capture the "cause" exception as a part of the LensFailure so it's possible this could be exposed in a more prominent way.
a
I was imagining it would need to be built into each format module. i.e. Jackson knows how to interpret jackson exceptions, and moshi knows how to interpret moshi exceptions, etc.
d
Alas it's all pretty generic. We could intercept the exception inside the main Json module and rethrow with the correct message (or one that is custom to the module in question). Happy to accept a PR on it if it's doable. The lazy option would be to modify this constructor or provide an alternative.:
Copy code
class LensFailure(val failures: List<Failure>, override val cause: Exception? = null, val target: Any? = null) : Exception(failures.joinToString { it.toString() }, cause) {
I've managed to surface the exception messages just for deserialising bodies, so hopefully I'll never have to read about this complaint again 😉. It's released now in v5.3.0.0
n
Thanks David I will try as soon as possible 😏 👍
a
Thank you so much. This will appease my team.