From 0bcb17df06074663f3e9b761709655f31a7b363d Mon Sep 17 00:00:00 2001 From: Immanuel Tikhonov <122638311+immanuwell@users.noreply.github.com> Date: Thu, 28 May 2026 03:29:24 +0400 Subject: [PATCH] fix: use descriptive error for unknown block options in health and log plugins (#8128) --- plugin/health/setup.go | 2 +- plugin/health/setup_test.go | 25 +++++++++++++++---------- plugin/log/setup.go | 2 +- plugin/log/setup_test.go | 12 ++++++++++++ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/plugin/health/setup.go b/plugin/health/setup.go index e9163ad44..51ccadb60 100644 --- a/plugin/health/setup.go +++ b/plugin/health/setup.go @@ -58,7 +58,7 @@ func parse(c *caddy.Controller) (string, time.Duration, error) { } dur = l default: - return "", 0, c.ArgErr() + return "", 0, c.Errf("unknown property '%s'", c.Val()) } } } diff --git a/plugin/health/setup_test.go b/plugin/health/setup_test.go index 7bb213259..50a56f2a1 100644 --- a/plugin/health/setup_test.go +++ b/plugin/health/setup_test.go @@ -1,6 +1,7 @@ package health import ( + "strings" "testing" "github.com/coredns/caddy" @@ -8,24 +9,25 @@ import ( func TestSetupHealth(t *testing.T) { tests := []struct { - input string - shouldErr bool + input string + shouldErr bool + expectedErrContent string }{ - {`health`, false}, - {`health localhost:1234`, false}, + {`health`, false, ""}, + {`health localhost:1234`, false, ""}, {`health localhost:1234 { lameduck 4s -}`, false}, - {`health bla:a`, false}, +}`, false, ""}, + {`health bla:a`, false, ""}, - {`health bla`, true}, - {`health bla bla`, true}, + {`health bla`, true, ""}, + {`health bla bla`, true, ""}, {`health localhost:1234 { lameduck a -}`, true}, +}`, true, ""}, {`health localhost:1234 { lamedudk 4 -} `, true}, +} `, true, "unknown property"}, } for i, test := range tests { @@ -40,6 +42,9 @@ func TestSetupHealth(t *testing.T) { if !test.shouldErr { t.Errorf("Test %d: Expected no error but found one for input %s. Error was: %v", i, test.input, err) } + if test.expectedErrContent != "" && !strings.Contains(err.Error(), test.expectedErrContent) { + t.Errorf("Test %d: Expected error to contain %q, got: %v", i, test.expectedErrContent, err) + } } } } diff --git a/plugin/log/setup.go b/plugin/log/setup.go index ecafb1a54..8cd2a1b47 100644 --- a/plugin/log/setup.go +++ b/plugin/log/setup.go @@ -86,7 +86,7 @@ func logParse(c *caddy.Controller) ([]Rule, error) { classes[cls] = struct{}{} } default: - return nil, c.ArgErr() + return nil, c.Errf("unknown property '%s'", c.Val()) } } if len(classes) == 0 { diff --git a/plugin/log/setup_test.go b/plugin/log/setup_test.go index 2586aded9..6b9430e31 100644 --- a/plugin/log/setup_test.go +++ b/plugin/log/setup_test.go @@ -2,6 +2,7 @@ package log import ( "reflect" + "strings" "testing" "github.com/coredns/caddy" @@ -182,3 +183,14 @@ func TestLogParse(t *testing.T) { } } } + +func TestLogParseUnknownProperty(t *testing.T) { + c := caddy.NewTestController("dns", `log { unknown }`) + _, err := logParse(c) + if err == nil { + t.Fatal("expected error for unknown block option, got nil") + } + if !strings.Contains(err.Error(), "unknown property") { + t.Errorf("expected error to contain 'unknown property', got: %v", err) + } +}