Hey! I wonder if there is any way of telling which json fields were missing or had incorrect type when parsing request using lenses.
Let’s say my endpoint expects requests three fields:
id: String
,
name: String
and
count: Int
and the caller forgets to send the
count
or maybe sends a boolean there. When parsing such request, http4k will likely throw a lens failure, e.g:
Copy code
body 'body' must be object
org.http4k.lens.LensFailure: body 'body' must be object
for json parsing but maybe I’m approaching the problem the wrong way?
d
dave
04/22/2022, 4:08 PM
Unfortunately we don't have the technology to provide this because we are reliant on the underlying JSON libraries for unmarshalling. You can extract the first error from a lens failure by interrogating the stack trace (where you get a path-like message of what went wrong) 🙂
r
Raf Szałański
04/22/2022, 4:10 PM
Thanks! I noticed that Lens failures have got very dev-friendly structure and are easy to parse and that it’s the json library which obfuscates the error messages
Raf Szałański
04/22/2022, 4:10 PM
I’m working on some solution for one json library that we use with lenses and will try sharing!
⭐ 1
Raf Szałański
05/04/2022, 8:33 AM
@dave I started building a handler like that which works with jackson and I’d be happy to share it with the community. would you be interested in that?
d
dave
05/04/2022, 12:37 PM
it would be great to see what you've come up with - even if it's just a gist. A Gist with tests even better 😉