question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Confusing error messages from lens when using custom types?

See original GitHub issue

Hello 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:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
daviddentoncommented, May 18, 2020

I believe logging and transparency should be easily enabled

You can achieve this by using one of the DebuggingFilters and just composing it into the handler stack.

(difficult to write due to the complex function signature).

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

1reaction
2x2xplzcommented, May 18, 2020

We deliberately don’t expose the underlying stack for security, but it is available in the LensFailure class - so if you customise CatchLensFailure you can get at the cause. An example doing this in the cookbook would definitely help here.

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 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

A story told by Type Errors - Dmitrii Kovanikov
To use custom type errors you need to perform two steps: Construct an error message itself using the ErrorMessage data type. Put the...
Read more >
Error notifications: When Contact Lens can't analyze a contact
Get more information about error notifications that result when Contact Lens can't analyze a contact, even though analysis was enabled on the flow....
Read more >
Deciphering lens error messages, part 1 | by Griffin Smith
To get started, let's go with probably the simplest example — what happens when we try to use a lens on a data...
Read more >
Clients / Go - Lenses Documentation
All lenses-go#Client methods return a typed value based on the call and an error as second output to catch any errors coming from...
Read more >
scala - How to get better error messages when parsing JSON ...
When you use the method Parse.decodeEither , the resulting type is an Either[String, MyClass] . There are many ways to check or operate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found