Remove config.Map.MatchErr()

Obscure and often misused.
This commit is contained in:
fox.cpp 2020-03-03 22:21:08 +03:00
parent 20233fa4c4
commit 46502217ed
No known key found for this signature in database
GPG key ID: E76D97CCEDE90B6C
12 changed files with 86 additions and 107 deletions

View file

@ -24,10 +24,10 @@ type logOut struct {
func logOutput(m *config.Map, node config.Node) (interface{}, error) { func logOutput(m *config.Map, node config.Node) (interface{}, error) {
if len(node.Args) == 0 { if len(node.Args) == 0 {
return nil, m.MatchErr("expected at least 1 argument") return nil, config.NodeErr(node, "expected at least 1 argument")
} }
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, config.NodeErr(node, "can't declare block here")
} }
return LogOutputOption(node.Args) return LogOutputOption(node.Args)

View file

@ -104,7 +104,7 @@ func (s *SASLAuth) AddProvider(m *config.Map, node config.Node) error {
} }
if !hasAny { if !hasAny {
return m.MatchErr("auth: specified module does not provide any SASL mechanism") return config.NodeErr(node, "auth: specified module does not provide any SASL mechanism")
} }
return nil return nil
} }

View file

@ -40,12 +40,12 @@ type FailAction struct {
func FailActionDirective(m *config.Map, node config.Node) (interface{}, error) { func FailActionDirective(m *config.Map, node config.Node) (interface{}, error) {
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, config.NodeErr(node, "can't declare block here")
} }
val, err := ParseActionDirective(node.Args) val, err := ParseActionDirective(node.Args)
if err != nil { if err != nil {
return nil, m.MatchErr("%v", err) return nil, config.NodeErr(node, "%v", err)
} }
return val, nil return val, nil
} }

View file

@ -2,14 +2,11 @@ package config
import ( import (
"errors" "errors"
"fmt"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"unicode" "unicode"
parser "github.com/foxcpp/maddy/pkg/cfgparser"
) )
type matcher struct { type matcher struct {
@ -38,10 +35,6 @@ func (m *matcher) assign(val interface{}) {
type Map struct { type Map struct {
allowUnknown bool allowUnknown bool
// Set to currently processed node when defaultVal or mapper functions are
// called.
curNode *Node
// All values saved by Map during processing. // All values saved by Map during processing.
Values map[string]interface{} Values map[string]interface{}
@ -57,17 +50,6 @@ func NewMap(globals map[string]interface{}, block Node) *Map {
return &Map{Globals: globals, Block: block} return &Map{Globals: globals, Block: block}
} }
// MatchErr returns error with formatted message, if called from defaultVal or
// mapper functions - message will be prepended with information about
// processed config node.
func (m *Map) MatchErr(format string, args ...interface{}) error {
if m.curNode != nil {
return parser.NodeErr(*m.curNode, format, args...)
} else {
return fmt.Errorf(format, args...)
}
}
// AllowUnknown makes config.Map skip unknown configuration directives instead // AllowUnknown makes config.Map skip unknown configuration directives instead
// of failing. // of failing.
func (m *Map) AllowUnknown() { func (m *Map) AllowUnknown() {
@ -85,10 +67,10 @@ func (m *Map) EnumList(name string, inheritGlobal, required bool, allowed []stri
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare a block here") return nil, NodeErr(node, "can't declare a block here")
} }
if len(node.Args) == 0 { if len(node.Args) == 0 {
return nil, m.MatchErr("expected at least one argument") return nil, NodeErr(node, "expected at least one argument")
} }
for _, arg := range node.Args { for _, arg := range node.Args {
@ -99,7 +81,7 @@ func (m *Map) EnumList(name string, inheritGlobal, required bool, allowed []stri
} }
} }
if !isAllowed { if !isAllowed {
return nil, m.MatchErr("invalid argument, valid values are: %v", allowed) return nil, NodeErr(node, "invalid argument, valid values are: %v", allowed)
} }
} }
@ -118,10 +100,10 @@ func (m *Map) Enum(name string, inheritGlobal, required bool, allowed []string,
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare a block here") return nil, NodeErr(node, "can't declare a block here")
} }
if len(node.Args) != 1 { if len(node.Args) != 1 {
return nil, m.MatchErr("expected exactly one argument") return nil, NodeErr(node, "expected exactly one argument")
} }
for _, str := range allowed { for _, str := range allowed {
@ -130,7 +112,7 @@ func (m *Map) Enum(name string, inheritGlobal, required bool, allowed []string,
} }
} }
return nil, m.MatchErr("invalid argument, valid values are: %v", allowed) return nil, NodeErr(node, "invalid argument, valid values are: %v", allowed)
}, store) }, store)
} }
@ -150,20 +132,20 @@ func (m *Map) Duration(name string, inheritGlobal, required bool, defaultVal tim
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, NodeErr(node, "can't declare block here")
} }
if len(node.Args) == 0 { if len(node.Args) == 0 {
return nil, m.MatchErr("at least one argument is required") return nil, NodeErr(node, "at least one argument is required")
} }
durationStr := strings.Join(node.Args, "") durationStr := strings.Join(node.Args, "")
dur, err := time.ParseDuration(durationStr) dur, err := time.ParseDuration(durationStr)
if err != nil { if err != nil {
return nil, m.MatchErr("%v", err) return nil, NodeErr(node, "%v", err)
} }
if dur < 0 { if dur < 0 {
return nil, m.MatchErr("duration must not be negative") return nil, NodeErr(node, "duration must not be negative")
} }
return dur, nil return dur, nil
@ -236,16 +218,16 @@ func (m *Map) DataSize(name string, inheritGlobal, required bool, defaultVal int
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, NodeErr(node, "can't declare block here")
} }
if len(node.Args) == 0 { if len(node.Args) == 0 {
return nil, m.MatchErr("at least one argument is required") return nil, NodeErr(node, "at least one argument is required")
} }
durationStr := strings.Join(node.Args, " ") durationStr := strings.Join(node.Args, " ")
dur, err := ParseDataSize(durationStr) dur, err := ParseDataSize(durationStr)
if err != nil { if err != nil {
return nil, m.MatchErr("%v", err) return nil, NodeErr(node, "%v", err)
} }
return dur, nil return dur, nil
@ -264,14 +246,14 @@ func (m *Map) Bool(name string, inheritGlobal, defaultVal bool, store *bool) {
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, NodeErr(node, "can't declare block here")
} }
if len(node.Args) == 0 { if len(node.Args) == 0 {
return true, nil return true, nil
} }
if len(node.Args) != 1 { if len(node.Args) != 1 {
return nil, m.MatchErr("expected exactly 1 argument") return nil, NodeErr(node, "expected exactly 1 argument")
} }
switch strings.ToLower(node.Args[0]) { switch strings.ToLower(node.Args[0]) {
@ -280,7 +262,7 @@ func (m *Map) Bool(name string, inheritGlobal, defaultVal bool, store *bool) {
case "0", "false", "off", "no": case "0", "false", "off", "no":
return false, nil return false, nil
} }
return nil, m.MatchErr("bool argument should be 'yes' or 'no'") return nil, NodeErr(node, "bool argument should be 'yes' or 'no'")
}, store) }, store)
} }
@ -297,10 +279,10 @@ func (m *Map) StringList(name string, inheritGlobal, required bool, defaultVal [
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Args) == 0 { if len(node.Args) == 0 {
return nil, m.MatchErr("expected at least one argument") return nil, NodeErr(node, "expected at least one argument")
} }
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, NodeErr(node, "can't declare block here")
} }
return node.Args, nil return node.Args, nil
@ -319,10 +301,10 @@ func (m *Map) String(name string, inheritGlobal, required bool, defaultVal strin
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Args) != 1 { if len(node.Args) != 1 {
return nil, m.MatchErr("expected 1 argument") return nil, NodeErr(node, "expected 1 argument")
} }
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, NodeErr(node, "can't declare block here")
} }
return node.Args[0], nil return node.Args[0], nil
@ -341,15 +323,15 @@ func (m *Map) Int(name string, inheritGlobal, required bool, defaultVal int, sto
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Args) != 1 { if len(node.Args) != 1 {
return nil, m.MatchErr("expected 1 argument") return nil, NodeErr(node, "expected 1 argument")
} }
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, NodeErr(node, "can't declare block here")
} }
i, err := strconv.Atoi(node.Args[0]) i, err := strconv.Atoi(node.Args[0])
if err != nil { if err != nil {
return nil, m.MatchErr("invalid integer: %s", node.Args[0]) return nil, NodeErr(node, "invalid integer: %s", node.Args[0])
} }
return i, nil return i, nil
}, store) }, store)
@ -367,15 +349,15 @@ func (m *Map) UInt(name string, inheritGlobal, required bool, defaultVal uint, s
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Args) != 1 { if len(node.Args) != 1 {
return nil, m.MatchErr("expected 1 argument") return nil, NodeErr(node, "expected 1 argument")
} }
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, NodeErr(node, "can't declare block here")
} }
i, err := strconv.ParseUint(node.Args[0], 10, 32) i, err := strconv.ParseUint(node.Args[0], 10, 32)
if err != nil { if err != nil {
return nil, m.MatchErr("invalid integer: %s", node.Args[0]) return nil, NodeErr(node, "invalid integer: %s", node.Args[0])
} }
return uint(i), nil return uint(i), nil
}, store) }, store)
@ -393,15 +375,15 @@ func (m *Map) Int32(name string, inheritGlobal, required bool, defaultVal int32,
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Args) != 1 { if len(node.Args) != 1 {
return nil, m.MatchErr("expected 1 argument") return nil, NodeErr(node, "expected 1 argument")
} }
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, NodeErr(node, "can't declare block here")
} }
i, err := strconv.ParseInt(node.Args[0], 10, 32) i, err := strconv.ParseInt(node.Args[0], 10, 32)
if err != nil { if err != nil {
return nil, m.MatchErr("invalid integer: %s", node.Args[0]) return nil, NodeErr(node, "invalid integer: %s", node.Args[0])
} }
return int32(i), nil return int32(i), nil
}, store) }, store)
@ -419,15 +401,15 @@ func (m *Map) UInt32(name string, inheritGlobal, required bool, defaultVal uint3
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Args) != 1 { if len(node.Args) != 1 {
return nil, m.MatchErr("expected 1 argument") return nil, NodeErr(node, "expected 1 argument")
} }
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, NodeErr(node, "can't declare block here")
} }
i, err := strconv.ParseUint(node.Args[0], 10, 32) i, err := strconv.ParseUint(node.Args[0], 10, 32)
if err != nil { if err != nil {
return nil, m.MatchErr("invalid integer: %s", node.Args[0]) return nil, NodeErr(node, "invalid integer: %s", node.Args[0])
} }
return uint32(i), nil return uint32(i), nil
}, store) }, store)
@ -445,15 +427,15 @@ func (m *Map) Int64(name string, inheritGlobal, required bool, defaultVal int64,
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Args) != 1 { if len(node.Args) != 1 {
return nil, m.MatchErr("expected 1 argument") return nil, NodeErr(node, "expected 1 argument")
} }
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, NodeErr(node, "can't declare block here")
} }
i, err := strconv.ParseInt(node.Args[0], 10, 64) i, err := strconv.ParseInt(node.Args[0], 10, 64)
if err != nil { if err != nil {
return nil, m.MatchErr("invalid integer: %s", node.Args[0]) return nil, NodeErr(node, "invalid integer: %s", node.Args[0])
} }
return i, nil return i, nil
}, store) }, store)
@ -471,15 +453,15 @@ func (m *Map) UInt64(name string, inheritGlobal, required bool, defaultVal uint6
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Args) != 1 { if len(node.Args) != 1 {
return nil, m.MatchErr("expected 1 argument") return nil, NodeErr(node, "expected 1 argument")
} }
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare block here") return nil, NodeErr(node, "can't declare block here")
} }
i, err := strconv.ParseUint(node.Args[0], 10, 64) i, err := strconv.ParseUint(node.Args[0], 10, 64)
if err != nil { if err != nil {
return nil, m.MatchErr("invalid integer: %s", node.Args[0]) return nil, NodeErr(node, "invalid integer: %s", node.Args[0])
} }
return i, nil return i, nil
}, store) }, store)
@ -497,12 +479,12 @@ func (m *Map) Float(name string, inheritGlobal, required bool, defaultVal float6
return defaultVal, nil return defaultVal, nil
}, func(m *Map, node Node) (interface{}, error) { }, func(m *Map, node Node) (interface{}, error) {
if len(node.Args) != 1 { if len(node.Args) != 1 {
return nil, m.MatchErr("expected 1 argument") return nil, NodeErr(node, "expected 1 argument")
} }
f, err := strconv.ParseFloat(node.Args[0], 64) f, err := strconv.ParseFloat(node.Args[0], 64)
if err != nil { if err != nil {
return nil, m.MatchErr("invalid float: %s", node.Args[0]) return nil, NodeErr(node, "invalid float: %s", node.Args[0])
} }
return f, nil return f, nil
}, store) }, store)
@ -594,12 +576,10 @@ func (m *Map) ProcessWith(globalCfg map[string]interface{}, block Node) (unknown
m.Values = make(map[string]interface{}) m.Values = make(map[string]interface{})
for _, subnode := range block.Children { for _, subnode := range block.Children {
m.curNode = &subnode
matcher, ok := m.entries[subnode.Name] matcher, ok := m.entries[subnode.Name]
if !ok { if !ok {
if !m.allowUnknown { if !m.allowUnknown {
return nil, m.MatchErr("unexpected directive: %s", subnode.Name) return nil, NodeErr(subnode, "unexpected directive: %s", subnode.Name)
} }
unknown = append(unknown, subnode) unknown = append(unknown, subnode)
continue continue
@ -614,7 +594,7 @@ func (m *Map) ProcessWith(globalCfg map[string]interface{}, block Node) (unknown
} }
if matched[subnode.Name] { if matched[subnode.Name] {
return nil, m.MatchErr("duplicate directive: %s", subnode.Name) return nil, NodeErr(subnode, "duplicate directive: %s", subnode.Name)
} }
matched[subnode.Name] = true matched[subnode.Name] = true
@ -627,7 +607,6 @@ func (m *Map) ProcessWith(globalCfg map[string]interface{}, block Node) (unknown
matcher.assign(val) matcher.assign(val)
} }
} }
m.curNode = &block
for _, matcher := range m.entries { for _, matcher := range m.entries {
if matched[matcher.name] { if matched[matcher.name] {
@ -651,7 +630,7 @@ func (m *Map) ProcessWith(globalCfg map[string]interface{}, block Node) (unknown
return nil, err return nil, err
} }
} else { } else {
return nil, m.MatchErr("missing required directive: %s", matcher.name) return nil, NodeErr(block, "missing required directive: %s", matcher.name)
} }
// If we put zero values into map then code that checks globalCfg // If we put zero values into map then code that checks globalCfg

View file

@ -56,21 +56,21 @@ func TLSVersionsDirective(m *Map, node Node) (interface{}, error) {
case 1: case 1:
value, ok := strVersionsMap[node.Args[0]] value, ok := strVersionsMap[node.Args[0]]
if !ok { if !ok {
return nil, m.MatchErr("invalid TLS version value: %s", node.Args[0]) return nil, NodeErr(node, "invalid TLS version value: %s", node.Args[0])
} }
return [2]uint16{value, value}, nil return [2]uint16{value, value}, nil
case 2: case 2:
minValue, ok := strVersionsMap[node.Args[0]] minValue, ok := strVersionsMap[node.Args[0]]
if !ok { if !ok {
return nil, m.MatchErr("invalid TLS version value: %s", node.Args[0]) return nil, NodeErr(node, "invalid TLS version value: %s", node.Args[0])
} }
maxValue, ok := strVersionsMap[node.Args[1]] maxValue, ok := strVersionsMap[node.Args[1]]
if !ok { if !ok {
return nil, m.MatchErr("invalid TLS version value: %s", node.Args[1]) return nil, NodeErr(node, "invalid TLS version value: %s", node.Args[1])
} }
return [2]uint16{minValue, maxValue}, nil return [2]uint16{minValue, maxValue}, nil
default: default:
return nil, m.MatchErr("expected 1 or 2 arguments") return nil, NodeErr(node, "expected 1 or 2 arguments")
} }
} }
@ -80,14 +80,14 @@ func TLSVersionsDirective(m *Map, node Node) (interface{}, error) {
// It returns list of []uint16 with corresponding cipher IDs. // It returns list of []uint16 with corresponding cipher IDs.
func TLSCiphersDirective(m *Map, node Node) (interface{}, error) { func TLSCiphersDirective(m *Map, node Node) (interface{}, error) {
if len(node.Args) == 0 { if len(node.Args) == 0 {
return nil, m.MatchErr("expected at least 1 argument, got 0") return nil, NodeErr(node, "expected at least 1 argument, got 0")
} }
res := make([]uint16, 0, len(node.Args)) res := make([]uint16, 0, len(node.Args))
for _, arg := range node.Args { for _, arg := range node.Args {
cipherId, ok := strCiphersMap[arg] cipherId, ok := strCiphersMap[arg]
if !ok { if !ok {
return nil, m.MatchErr("unknown cipher: %s", arg) return nil, NodeErr(node, "unknown cipher: %s", arg)
} }
res = append(res, cipherId) res = append(res, cipherId)
} }
@ -101,14 +101,14 @@ func TLSCiphersDirective(m *Map, node Node) (interface{}, error) {
// It returns []tls.CurveID. // It returns []tls.CurveID.
func TLSCurvesDirective(m *Map, node Node) (interface{}, error) { func TLSCurvesDirective(m *Map, node Node) (interface{}, error) {
if len(node.Args) == 0 { if len(node.Args) == 0 {
return nil, m.MatchErr("expected at least 1 argument, got 0") return nil, NodeErr(node, "expected at least 1 argument, got 0")
} }
res := make([]tls.CurveID, 0, len(node.Args)) res := make([]tls.CurveID, 0, len(node.Args))
for _, arg := range node.Args { for _, arg := range node.Args {
curveId, ok := strCurvesMap[arg] curveId, ok := strCurvesMap[arg]
if !ok { if !ok {
return nil, m.MatchErr("unknown curve: %s", arg) return nil, NodeErr(node, "unknown curve: %s", arg)
} }
res = append(res, curveId) res = append(res, curveId)
} }

View file

@ -60,7 +60,7 @@ func (cfg *TLSConfig) read(m *Map, node Node, generateSelfSig bool) error {
return nil return nil
default: default:
log.Println(node.Name, node.Args) log.Println(node.Name, node.Args)
return m.MatchErr("unexpected argument (%s), want 'off' or 'self_signed'", node.Args[0]) return NodeErr(node, "unexpected argument (%s), want 'off' or 'self_signed'", node.Args[0])
} }
case 2: case 2:
tlsCfg, err := readTLSBlock(m, node) tlsCfg, err := readTLSBlock(m, node)
@ -70,7 +70,7 @@ func (cfg *TLSConfig) read(m *Map, node Node, generateSelfSig bool) error {
cfg.cfg = tlsCfg cfg.cfg = tlsCfg
return nil return nil
default: default:
return m.MatchErr("expected 1 or 2 arguments") return NodeErr(node, "expected 1 or 2 arguments")
} }
} }
@ -124,7 +124,7 @@ func readTLSBlock(m *Map, blockNode Node) (*tls.Config, error) {
var tlsVersions [2]uint16 var tlsVersions [2]uint16
if len(blockNode.Args) != 2 { if len(blockNode.Args) != 2 {
return nil, m.MatchErr("two arguments required") return nil, NodeErr(blockNode, "two arguments required")
} }
certPath := blockNode.Args[0] certPath := blockNode.Args[0]
keyPath := blockNode.Args[1] keyPath := blockNode.Args[1]

View file

@ -149,12 +149,12 @@ func autoBufferMode(maxSize int, dir string) func(io.Reader) (buffer.Buffer, err
func bufferModeDirective(m *config.Map, node config.Node) (interface{}, error) { func bufferModeDirective(m *config.Map, node config.Node) (interface{}, error) {
if len(node.Args) < 1 { if len(node.Args) < 1 {
return nil, m.MatchErr("at least one argument required") return nil, config.NodeErr(node, "at least one argument required")
} }
switch node.Args[0] { switch node.Args[0] {
case "ram": case "ram":
if len(node.Args) > 1 { if len(node.Args) > 1 {
return nil, m.MatchErr("no additional arguments for 'ram' mode") return nil, config.NodeErr(node, "no additional arguments for 'ram' mode")
} }
return buffer.BufferInMemory, nil return buffer.BufferInMemory, nil
case "fs": case "fs":
@ -168,7 +168,7 @@ func bufferModeDirective(m *config.Map, node config.Node) (interface{}, error) {
return buffer.BufferInFile(r, path) return buffer.BufferInFile(r, path)
}, nil }, nil
default: default:
return nil, m.MatchErr("too many arguments for 'fs' mode") return nil, config.NodeErr(node, "too many arguments for 'fs' mode")
} }
case "auto": case "auto":
path := filepath.Join(config.StateDirectory, "buffer") path := filepath.Join(config.StateDirectory, "buffer")
@ -185,16 +185,16 @@ func bufferModeDirective(m *config.Map, node config.Node) (interface{}, error) {
var err error var err error
maxSize, err = config.ParseDataSize(node.Args[1]) maxSize, err = config.ParseDataSize(node.Args[1])
if err != nil { if err != nil {
return nil, m.MatchErr("%v", err) return nil, config.NodeErr(node, "%v", err)
} }
fallthrough fallthrough
case 1: case 1:
return autoBufferMode(maxSize, path), nil return autoBufferMode(maxSize, path), nil
default: default:
return nil, m.MatchErr("too many arguments for 'auto' mode") return nil, config.NodeErr(node, "too many arguments for 'auto' mode")
} }
default: default:
return nil, m.MatchErr("unknown buffer mode: %v", node.Args[0]) return nil, config.NodeErr(node, "unknown buffer mode: %v", node.Args[0])
} }
} }

View file

@ -53,9 +53,9 @@ func (g *Group) Init(cfg *config.Map) error {
) )
switch kind := child.Args[0]; kind { switch kind := child.Args[0]; kind {
case "rate": case "rate":
ctor, err = rateCtor(cfg, child.Args[1:]) ctor, err = rateCtor(child, child.Args[1:])
case "concurrency": case "concurrency":
ctor, err = concurrencyCtor(cfg, child.Args[1:]) ctor, err = concurrencyCtor(child, child.Args[1:])
default: default:
return config.NodeErr(child, "unknown limit kind: %v", kind) return config.NodeErr(child, "unknown limit kind: %v", kind)
} }
@ -111,7 +111,7 @@ func (g *Group) Init(cfg *config.Map) error {
return nil return nil
} }
func rateCtor(cfg *config.Map, args []string) (func() limiters.L, error) { func rateCtor(node config.Node, args []string) (func() limiters.L, error) {
period := 1 * time.Second period := 1 * time.Second
burst := 0 burst := 0
@ -120,16 +120,16 @@ func rateCtor(cfg *config.Map, args []string) (func() limiters.L, error) {
var err error var err error
period, err = time.ParseDuration(args[1]) period, err = time.ParseDuration(args[1])
if err != nil { if err != nil {
return nil, cfg.MatchErr("%v", err) return nil, config.NodeErr(node, "%v", err)
} }
case 1: case 1:
var err error var err error
burst, err = strconv.Atoi(args[0]) burst, err = strconv.Atoi(args[0])
if err != nil { if err != nil {
return nil, cfg.MatchErr("%v", err) return nil, config.NodeErr(node, "%v", err)
} }
case 0: case 0:
return nil, cfg.MatchErr("at least burst size is needed") return nil, config.NodeErr(node, "at least burst size is needed")
} }
return func() limiters.L { return func() limiters.L {
@ -137,13 +137,13 @@ func rateCtor(cfg *config.Map, args []string) (func() limiters.L, error) {
}, nil }, nil
} }
func concurrencyCtor(cfg *config.Map, args []string) (func() limiters.L, error) { func concurrencyCtor(node config.Node, args []string) (func() limiters.L, error) {
if len(args) != 1 { if len(args) != 1 {
return nil, cfg.MatchErr("max concurrency value is needed") return nil, config.NodeErr(node, "max concurrency value is needed")
} }
max, err := strconv.Atoi(args[0]) max, err := strconv.Atoi(args[0])
if err != nil { if err != nil {
return nil, cfg.MatchErr("%v", err) return nil, config.NodeErr(node, "%v", err)
} }
return func() limiters.L { return func() limiters.L {
return limiters.NewSemaphore(max) return limiters.NewSemaphore(max)

View file

@ -221,7 +221,7 @@ func (store *Storage) Init(cfg *config.Map) error {
return "messages", nil return "messages", nil
}, func(m *config.Map, node config.Node) (interface{}, error) { }, func(m *config.Map, node config.Node) (interface{}, error) {
if len(node.Args) != 1 { if len(node.Args) != 1 {
return nil, m.MatchErr("expected 0 or 1 arguments") return nil, config.NodeErr(node, "expected 0 or 1 arguments")
} }
return node.Args[0], nil return node.Args[0], nil
}, &fsstoreLocation) }, &fsstoreLocation)

View file

@ -35,7 +35,7 @@ func (pg *PolicyGroup) Init(cfg *config.Map) error {
for _, block := range other { for _, block := range other {
if _, ok := pg.pols[block.Name]; ok { if _, ok := pg.pols[block.Name]; ok {
return cfg.MatchErr("duplicate policy block: %v", block.Name) return config.NodeErr(block, "duplicate policy block: %v", block.Name)
} }
policy, err := policyFromNode(debugLog, cfg, block) policy, err := policyFromNode(debugLog, cfg, block)

View file

@ -766,7 +766,7 @@ func (l localPolicy) CheckConn(ctx context.Context, mxLevel MXLevel, tlsLevel TL
func policyFromNode(debugLog bool, m *config.Map, node config.Node) (Policy, error) { func policyFromNode(debugLog bool, m *config.Map, node config.Node) (Policy, error) {
if len(node.Args) != 0 { if len(node.Args) != 0 {
return nil, m.MatchErr("no arguments allowed") return nil, config.NodeErr(node, "no arguments allowed")
} }
var ( var (
@ -778,12 +778,12 @@ func policyFromNode(debugLog bool, m *config.Map, node config.Node) (Policy, err
policy, err = NewMTASTSPolicy(net.DefaultResolver, log.DefaultLogger.Debug, config.NewMap(m.Globals, node)) policy, err = NewMTASTSPolicy(net.DefaultResolver, log.DefaultLogger.Debug, config.NewMap(m.Globals, node))
case "dane": case "dane":
if node.Children != nil { if node.Children != nil {
return nil, m.MatchErr("policy offers no additional configuration") return nil, config.NodeErr(node, "policy offers no additional configuration")
} }
policy = NewDANEPolicy(debugLog) policy = NewDANEPolicy(debugLog)
case "dnssec": case "dnssec":
if node.Children != nil { if node.Children != nil {
return nil, m.MatchErr("policy offers no additional configuration") return nil, config.NodeErr(node, "policy offers no additional configuration")
} }
policy = &dnssecPolicy{} policy = &dnssecPolicy{}
case "sts_preload": case "sts_preload":
@ -792,7 +792,7 @@ func policyFromNode(debugLog bool, m *config.Map, node config.Node) (Policy, err
case "local_policy": case "local_policy":
policy, err = NewLocalPolicy(config.NewMap(m.Globals, node)) policy, err = NewLocalPolicy(config.NewMap(m.Globals, node))
default: default:
return nil, m.MatchErr("unknown policy module: %v", node.Name) return nil, config.NodeErr(node, "unknown policy module: %v", node.Name)
} }
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -15,17 +15,17 @@ type saslClientFactory = func(msgMeta *module.MsgMetadata) (sasl.Client, error)
// Authentication information of the current client should be passed in arguments. // Authentication information of the current client should be passed in arguments.
func saslAuthDirective(m *config.Map, node config.Node) (interface{}, error) { func saslAuthDirective(m *config.Map, node config.Node) (interface{}, error) {
if len(node.Children) != 0 { if len(node.Children) != 0 {
return nil, m.MatchErr("can't declare a block here") return nil, config.NodeErr(node, "can't declare a block here")
} }
if len(node.Args) == 0 { if len(node.Args) == 0 {
return nil, m.MatchErr("at least one argument required") return nil, config.NodeErr(node, "at least one argument required")
} }
switch node.Args[0] { switch node.Args[0] {
case "off": case "off":
return nil, nil return nil, nil
case "forward": case "forward":
if len(node.Args) > 1 { if len(node.Args) > 1 {
return nil, m.MatchErr("no additional arguments required") return nil, config.NodeErr(node, "no additional arguments required")
} }
return func(msgMeta *module.MsgMetadata) (sasl.Client, error) { return func(msgMeta *module.MsgMetadata) (sasl.Client, error) {
if msgMeta.Conn == nil || msgMeta.Conn.AuthUser == "" || msgMeta.Conn.AuthPassword == "" { if msgMeta.Conn == nil || msgMeta.Conn.AuthUser == "" || msgMeta.Conn.AuthPassword == "" {
@ -42,19 +42,19 @@ func saslAuthDirective(m *config.Map, node config.Node) (interface{}, error) {
}, nil }, nil
case "plain": case "plain":
if len(node.Args) != 3 { if len(node.Args) != 3 {
return nil, m.MatchErr("two additional arguments are required (username, password)") return nil, config.NodeErr(node, "two additional arguments are required (username, password)")
} }
return func(*module.MsgMetadata) (sasl.Client, error) { return func(*module.MsgMetadata) (sasl.Client, error) {
return sasl.NewPlainClient("", node.Args[1], node.Args[2]), nil return sasl.NewPlainClient("", node.Args[1], node.Args[2]), nil
}, nil }, nil
case "external": case "external":
if len(node.Args) > 1 { if len(node.Args) > 1 {
return nil, m.MatchErr("no additional arguments required") return nil, config.NodeErr(node, "no additional arguments required")
} }
return func(*module.MsgMetadata) (sasl.Client, error) { return func(*module.MsgMetadata) (sasl.Client, error) {
return sasl.NewExternalClient(""), nil return sasl.NewExternalClient(""), nil
}, nil }, nil
default: default:
return nil, m.MatchErr("unknown authentication mechanism: %s", node.Args[0]) return nil, config.NodeErr(node, "unknown authentication mechanism: %s", node.Args[0])
} }
} }