Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throwable getMessage can return null #759

Merged
merged 2 commits into from
Apr 12, 2023

Conversation

daddykotex
Copy link
Contributor

We've had instances where a throwable with no error message would break the logger.

For instance, if you call asJson on a null it does:

//> using scala "2.13.10"
//> using lib "io.circe::circe-core:0.14.5"

import io.circe._
import io.circe.syntax._

case class CrappyError(msg: String) extends RuntimeException(msg)
object MyTest extends App {
  val err = CrappyError(null)
  println(err.getMessage.asJson)
}

results in:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "value" is null
        at io.circe.Printer$PrintingFolder.onString(Printer.scala:319)
        at io.circe.Printer$PrintingFolder.onString(Printer.scala:302)
        at io.circe.Json$JString.foldWith(Json.scala:369)
        at io.circe.Printer.unsafePrintToAppendable(Printer.scala:229)
        at io.circe.Printer.print(Printer.scala:197)
        at io.circe.Json.spaces2(Json.scala:127)
        at io.circe.Json.toString(Json.scala:220)
        at java.base/java.lang.String.valueOf(String.java:4215)
        at java.base/java.io.PrintStream.println(PrintStream.java:1047)
        at scala.Console$.println(Console.scala:268)
        at scala.Predef$.println(Predef.scala:427)

@mergify mergify bot added the log label Apr 4, 2023
@daddykotex
Copy link
Contributor Author

During a discussion, it was brought up to me that toString is not necessarily implemented in terms of getMessage so an alternative implementation might be to use getMessage and fallback to toString

wdyt?

@mpilquist
Copy link
Member

How about Option(t.getMessage).map(_.asJson).getOrElse(Json.nullValue)

@daddykotex
Copy link
Contributor Author

How about Option(t.getMessage).map(_.asJson).getOrElse(Json.nullValue)

yeah, that's a good idea. we have the class already so, I'll do that.

@mpilquist mpilquist merged commit 6c81713 into typelevel:main Apr 12, 2023
@daddykotex
Copy link
Contributor Author

Thanks for your review <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants