From b651f4497043db97f46fd721b620633b17a85327 Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Tue, 25 Apr 2023 09:22:56 -0500 Subject: [PATCH] error extension --- modules/core/src/main/scala/problem.scala | 13 ++++++++++--- .../src/test/scala/compiler/ProblemSpec.scala | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/modules/core/src/main/scala/problem.scala b/modules/core/src/main/scala/problem.scala index 493ffbbb..87dde788 100644 --- a/modules/core/src/main/scala/problem.scala +++ b/modules/core/src/main/scala/problem.scala @@ -10,7 +10,8 @@ import io.circe.syntax._ final case class Problem( message: String, locations: List[(Int, Int)] = Nil, - path: List[String] = Nil + path: List[String] = Nil, + extension: Option[JsonObject] = None, ) { override def toString = { @@ -23,13 +24,15 @@ final case class Problem( if (a == b) a.toString else s"$a..$b" } .mkString(", ") - (path.nonEmpty, locations.nonEmpty) match { + val s = (path.nonEmpty, locations.nonEmpty) match { case (true, true) => s"$message (at $pathText: $locationsText)" case (true, false) => s"$message (at $pathText)" case (false, true) => s"$message (at $locationsText)" case (false, false) => message } + extension.fold(s)(obj => s"$s, extension: ${obj.asJson.spaces2}") + } } @@ -54,10 +57,14 @@ object Problem { if (p.path.isEmpty) Nil else List(("path" -> p.path.asJson)) + val extensionField: List[(String, Json)] = + p.extension.fold(List.empty[(String, Json)])(obj => List("extension" -> obj.asJson)) + Json.fromFields( "message" -> p.message.asJson :: locationsField ::: - pathField + pathField ::: + extensionField ) } diff --git a/modules/core/src/test/scala/compiler/ProblemSpec.scala b/modules/core/src/test/scala/compiler/ProblemSpec.scala index 1ec1a9f3..dab3a60a 100644 --- a/modules/core/src/test/scala/compiler/ProblemSpec.scala +++ b/modules/core/src/test/scala/compiler/ProblemSpec.scala @@ -7,6 +7,7 @@ import cats.tests.CatsSuite import edu.gemini.grackle.syntax._ import edu.gemini.grackle.Problem import io.circe.syntax._ +import io.circe.JsonObject final class ProblemSpec extends CatsSuite { @@ -106,4 +107,22 @@ final class ProblemSpec extends CatsSuite { ) } + test("extension") { + val p = Problem("foo", extension = Some(JsonObject("bar" -> 42.asJson, "baz" -> List("a", "b").asJson))) + assert( + p.asJson == json""" + { + "message" : "foo", + "extension" : { + "bar" : 42, + "baz" : [ + "a", + "b" + ] + } + } + """ + ) + } + } \ No newline at end of file