Skip to content

Commit

Permalink
Merge pull request #759 from daddykotex/dfrancoeur/natchez-mock-optional
Browse files Browse the repository at this point in the history
Throwable getMessage can return null
  • Loading branch information
mpilquist authored Apr 12, 2023
2 parents 72f5bf7 + 887a5a8 commit 6c81713
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
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)
}
}
}
}

0 comments on commit 6c81713

Please sign in to comment.