Confusing error messages from lens when using custom types?
See original GitHub issueHello there!
I have a Path
lens with a custom type (similar to the examples in the documentation). I wanted some extra validations on my custom type, and throw an exception when they fail.
This correctly makes the lens fail, but the resulting error message is really confusing, as it just says path 'field' must be a string
, rather than exposing the upstream error.
Example:
#!/usr/bin/env kscript
//DEPS org.http4k:http4k-core:3.163.0
import org.http4k.core.*
import org.http4k.filter.*
import org.http4k.routing.*
import org.http4k.lens.*
import org.http4k.core.Status.Companion.OK
import org.http4k.core.Method.GET
import org.http4k.filter.ServerFilters
data class CustomType(val value: String) {
init { if (value == "foo") throw IllegalArgumentException("Sorry, no foos please")}
}
val requiredCustomQuery = Path.map(::CustomType, { it.value }).of("name")
val app: HttpHandler = ServerFilters.CatchLensFailure().then(routes(
"/greet/{name}" bind GET to { req: Request ->
val custom = requiredCustomQuery(req)
Response(OK).body("hello ${custom}")
})
)
val inMemoryResponse: Response = app(Request(GET, "/greet/foo"))
println(inMemoryResponse)
Output:
HTTP/1.1 400 path 'name' must be string
What I expected:
I expected the exception to be included in the message, e.g. something like path 'name' is invalid: Sorry, no foos please
.
Am I missing something? Looking at the lens mechanism, there doesn’t seem to be a clear way to configure this, other than basically changing the CatchLensFailure
to ignore the Failure
object.
It seems like if I could provide my own meta
this could maybe be supported, but there doesn’t seem to be a way to use my own Meta
with a Path
lens…:?
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
You can achieve this by using one of the
DebuggingFilters
and just composing it into the handler stack.Assuming you mean the custom function passed to the CatchLensFailure, the function signature is just
(LensFailure) -> Response
Most people have problems with the JSON auto marshalling, but the underlying LensFailure cause actually contains a reasonable message.
Here’s a Gist with an example of all that stuff in one:
https://gist.github.com/daviddenton/a8b783af89e53c86d2b6de63f847e51c
Hi, I think this is overdue. One of the primary tenets of http4k is “5. Absolutely no magic involved” and while Lenses may not technically be “magic,” they sometimes feel like it – they handle the conversion of raw HTTP data into objects and back, under the hood and without any logging or explanation of why a particular request is malformed. Don’t get me wrong, they are very useful but I believe logging and transparency should be easily enabled (something like
CatchLensFailure(showStack = true, logRawRequest = true)
), or at least an example of a custom replacement function (difficult to write due to the complex function signature). Thank you 👍