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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/log/shared/src/main/scala/LogSpan.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private[log] final case class LogSpan[F[_]: Sync: Logger](
putAny(
"exit.case" -> "error".asJson,
"exit.error.class" -> err.getClass.getName.asJson,
"exit.error.message" -> err.getMessage.asJson,
"exit.error.message" -> Option(err.getMessage).map(_.asJson).getOrElse(Json.Null),
"exit.error.stackTrace" -> err.getStackTrace.map(_.toString).asJson
) *> put(fields: _*)

Expand Down
46 changes: 46 additions & 0 deletions modules/log/shared/src/test/scala/LogSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package log

import munit.CatsEffectSuite
import cats.effect.IO
import cats.syntax.traverse._
import io.circe.Json
import natchez.Span.SpanKind

Expand All @@ -28,6 +29,7 @@ class LogSuite extends CatsEffectSuite {
.remove("trace.span_id")
.remove("trace.parent_id")
.remove("trace.trace_id")
.remove("exit.error.stackTrace") // contains thread info
.mapValues(filter)
)
)
Expand Down Expand Up @@ -90,4 +92,48 @@ class LogSuite extends CatsEffectSuite {
}
}

test("log formatter should handle exceptions") {
val exWithMsg = new RuntimeException("oops")
val exNull = new RuntimeException(null: String)

val tests = List[(Throwable, String)](
exWithMsg -> """|test: [info] {
| "name" : "root span",
| "service" : "service",
| "span.kind" : "Server",
| "span.links" : [
| ],
| "exit.case" : "succeeded",
| "exit.error.class" : "java.lang.RuntimeException",
| "exit.error.message" : "oops",
| "children" : [
| ]
|}
|""".stripMargin,
exNull -> """|test: [info] {
| "name" : "root span",
| "service" : "service",
| "span.kind" : "Server",
| "span.links" : [
| ],
| "exit.case" : "succeeded",
| "exit.error.class" : "java.lang.RuntimeException",
| "exit.error.message" : null,
| "children" : [
| ]
|}
|""".stripMargin
)

tests.traverse { case (exception, expected) =>
MockLogger.newInstance[IO]("test").flatMap { implicit log =>
Log
.entryPoint[IO]("service", filter(_).spaces2)
.root("root span", Span.Options.Defaults.withSpanKind(SpanKind.Server))
.use { root =>
root.attachError(err = exception)
} *> log.get.assertEquals(expected)
}
}
}
}