Skip to content

Show null compatibility with scala string interpolator #4689

Open
@BusyByte

Description

We have a lot of java things we interact and they can throw exceptions or have null in certain pojo fields to where if we use them in a string interpolator it will just print null

val foo = Foo(a, null)
log.info(s"found a foo ${foo.secondField)}")

val exception = new RuntimeException()
log.error(s"something happened ${exception.getMessage}", exception)

We tried to migrate to show but it blows up and does not give you a good error message to indicate where the problem is.
I asked in Discord and here's the feedback:

  1. don't use nulls (it's very hard to do this especially when integrating with a lot of java teams/libraries)
  2. it's a bug, file a bug (doing that here)
  3. write my own version of show SafeShow and use that.

I've done number 3 and created SafeShow but incorporating it into our legacy monolith is an effort itself so I thought I'd open this to see if anything could be done in cats itself. (I've basically duplicated the show syntax, interpolator and use the typename as required field for constructing the Show type class instance using SafeShow and log the typename and the exception when we recover from NPE before printing "null"). This is not something I really want to maintain although it's not that much code (less than 100 lines of main code). Also most of our libraries just use plain Show so we have to go and change all those as well.

The ability to adopt and use things from cats for newbies is also a hurdle. When do I use our safe show and when do we not.
It's a lot for newbies to absorb and makes the interop with Java stuff harder. It's also a hurdle when things that seem like they should be equivalent are not.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions