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
default:
return "", 0, c.ArgErr()
return "", 0, c.Errf("unknown property '%s'", c.Val())
}
}
}

View File

@@ -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)
}
}
}
}

View File

@@ -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 {

View File

@@ -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)
}
}