From 3fcce51e1178f759a9b905331f7914b936bb366b Mon Sep 17 00:00:00 2001 From: brharrington Date: Wed, 31 Jan 2024 06:57:21 -0600 Subject: [PATCH] improve error message for invalid theme (#1601) If an invalid theme name was requested, it was returning a 500 with a confusing error message. It will now return a 400 and indicate the theme is invalid. --- .../netflix/atlas/eval/graph/DefaultSettings.scala | 11 +++++++++-- .../com/netflix/atlas/eval/graph/GraphUriSuite.scala | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/atlas-eval/src/main/scala/com/netflix/atlas/eval/graph/DefaultSettings.scala b/atlas-eval/src/main/scala/com/netflix/atlas/eval/graph/DefaultSettings.scala index 0a1fba736..2bb1d349d 100644 --- a/atlas-eval/src/main/scala/com/netflix/atlas/eval/graph/DefaultSettings.scala +++ b/atlas-eval/src/main/scala/com/netflix/atlas/eval/graph/DefaultSettings.scala @@ -68,11 +68,18 @@ case class DefaultSettings(root: Config, config: Config) { /** Default theme to use for the chart. */ val theme: String = config.getString("theme") + private def themeConfig(theme: String): Config = { + if (config.hasPath(theme)) + config.getConfig(theme) + else + throw new IllegalArgumentException(s"invalid theme: $theme") + } + /** Default palette name to use. */ - def primaryPalette(theme: String): String = config.getString(s"$theme.palette.primary") + def primaryPalette(theme: String): String = themeConfig(theme).getString("palette.primary") /** Default palette name to use for lines with an offset. */ - def offsetPalette(theme: String): String = config.getString(s"$theme.palette.offset") + def offsetPalette(theme: String): String = themeConfig(theme).getString(s"palette.offset") /** Resolve color for a given theme. */ def resolveColor(theme: String, color: String): Color = { diff --git a/atlas-eval/src/test/scala/com/netflix/atlas/eval/graph/GraphUriSuite.scala b/atlas-eval/src/test/scala/com/netflix/atlas/eval/graph/GraphUriSuite.scala index c2a9bd519..9556b6ffa 100644 --- a/atlas-eval/src/test/scala/com/netflix/atlas/eval/graph/GraphUriSuite.scala +++ b/atlas-eval/src/test/scala/com/netflix/atlas/eval/graph/GraphUriSuite.scala @@ -95,6 +95,13 @@ class GraphUriSuite extends FunSuite { assertEquals(cfg.flags.axes(0).newPlotDef().lower, PlotBound.AutoStyle) } + test("invalid theme") { + val e = intercept[IllegalArgumentException] { + parseUri("/api/v1/graph?q=name,foo,:eq,:sum&theme=foo") + } + assertEquals(e.getMessage, "invalid theme: foo") + } + test("hints: none") { val cfg = parseUri("/api/v1/graph?q=name,foo,:eq,:sum") assertEquals(cfg.flags.hints, Set.empty[String])