fix: use descriptive error for unknown block options in health and log plugins (#8128)

This commit is contained in:
Immanuel Tikhonov
2026-05-28 03:29:24 +04:00
committed by GitHub
parent afdf121a5a
commit 0bcb17df06
4 changed files with 29 additions and 12 deletions

View File

@@ -58,7 +58,7 @@ func parse(c *caddy.Controller) (string, time.Duration, error) {
} }
dur = l dur = l
default: default:
return "", 0, c.ArgErr() return "", 0, c.Errf("unknown property '%s'", c.Val())
} }
} }
} }

View File

@@ -1,6 +1,7 @@
package health package health
import ( import (
"strings"
"testing" "testing"
"github.com/coredns/caddy" "github.com/coredns/caddy"
@@ -10,22 +11,23 @@ func TestSetupHealth(t *testing.T) {
tests := []struct { tests := []struct {
input string input string
shouldErr bool shouldErr bool
expectedErrContent string
}{ }{
{`health`, false}, {`health`, false, ""},
{`health localhost:1234`, false}, {`health localhost:1234`, false, ""},
{`health localhost:1234 { {`health localhost:1234 {
lameduck 4s lameduck 4s
}`, false}, }`, false, ""},
{`health bla:a`, false}, {`health bla:a`, false, ""},
{`health bla`, true}, {`health bla`, true, ""},
{`health bla bla`, true}, {`health bla bla`, true, ""},
{`health localhost:1234 { {`health localhost:1234 {
lameduck a lameduck a
}`, true}, }`, true, ""},
{`health localhost:1234 { {`health localhost:1234 {
lamedudk 4 lamedudk 4
} `, true}, } `, true, "unknown property"},
} }
for i, test := range tests { for i, test := range tests {
@@ -40,6 +42,9 @@ func TestSetupHealth(t *testing.T) {
if !test.shouldErr { if !test.shouldErr {
t.Errorf("Test %d: Expected no error but found one for input %s. Error was: %v", i, test.input, err) 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)
}
} }
} }
} }

View File

@@ -86,7 +86,7 @@ func logParse(c *caddy.Controller) ([]Rule, error) {
classes[cls] = struct{}{} classes[cls] = struct{}{}
} }
default: default:
return nil, c.ArgErr() return nil, c.Errf("unknown property '%s'", c.Val())
} }
} }
if len(classes) == 0 { if len(classes) == 0 {

View File

@@ -2,6 +2,7 @@ package log
import ( import (
"reflect" "reflect"
"strings"
"testing" "testing"
"github.com/coredns/caddy" "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)
}
}