Skip to content

Instantly share code, notes, and snippets.

@kevinmeredith
Created July 11, 2017 16:02
Show Gist options
  • Save kevinmeredith/77159d070c3f5a4b994f46321bbf6a95 to your computer and use it in GitHub Desktop.
Save kevinmeredith/77159d070c3f5a4b994f46321bbf6a95 to your computer and use it in GitHub Desktop.
What's going on here?
import argonaut._, Argonaut._
import scala.util.control.NoStackTrace

final case class UncaughtException(error: String) extends RuntimeException with NoStackTrace

implicit val UncaughtExceptionJsonEncoder: EncodeJson[UncaughtException] =
  jencode1L((e: UncaughtException) => e.getMessage)("error")

scala> UncaughtExceptionJsonEncoder.encode(UncaughtException("a"))
java.lang.NullPointerException
  at scala.collection.immutable.StringOps$.length$extension(StringOps.scala:47)
  at scala.collection.immutable.StringOps.length(StringOps.scala:47)

but the following works:

    EncodeJson((p: UncaughtException) =>
      ("error" := p.error) ->: jEmptyObject)

scala> X.encode(UncaughtException("a"))
res1: argonaut.Json = {"error":"a"}```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment