@marcel91200 / caddy Mirror · read-only / commits / 38a7b6b3
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 23:15 UTC

caddyfile: Adjust error formatting (#5765)

Francis Lavoie committed Aug 20, 2023 at 10:51 UTC 38a7b6b3d0e1150bc22e2f5409b74821132f8741
2 files changed +12 -9
caddyconfig/caddyfile/dispenser.go
+8 -5
@@ -391,22 +391,22 @@ func (d *Dispenser) Reset() {
391 // an argument.
392 func (d *Dispenser) ArgErr() error {
393 if d.Val() == "{" {
394 - return d.Err("Unexpected token '{', expecting argument")
394 + return d.Err("unexpected token '{', expecting argument")
395 }
396 - return d.Errf("Wrong argument count or unexpected line ending after '%s'", d.Val())
396 + return d.Errf("wrong argument count or unexpected line ending after '%s'", d.Val())
397 }
398
399 // SyntaxErr creates a generic syntax error which explains what was
400 // found and what was expected.
401 func (d *Dispenser) SyntaxErr(expected string) error {
402 - msg := fmt.Sprintf("%s:%d - Syntax error: Unexpected token '%s', expecting '%s', import chain: ['%s']", d.File(), d.Line(), d.Val(), expected, strings.Join(d.Token().imports, "','"))
402 + msg := fmt.Sprintf("syntax error: unexpected token '%s', expecting '%s', at %s:%d import chain: ['%s']", d.Val(), expected, d.File(), d.Line(), strings.Join(d.Token().imports, "','"))
403 return errors.New(msg)
404 }
405
406 // EOFErr returns an error indicating that the dispenser reached
407 // the end of the input when searching for the next token.
408 func (d *Dispenser) EOFErr() error {
409 - return d.Errf("Unexpected EOF")
409 + return d.Errf("unexpected EOF")
410 }
411
412 // Err generates a custom parse-time error with a message of msg.
@@ -421,7 +421,10 @@ func (d *Dispenser) Errf(format string, args ...any) error {
421
422 // WrapErr takes an existing error and adds the Caddyfile file and line number.
423 func (d *Dispenser) WrapErr(err error) error {
424 - return fmt.Errorf("%s:%d - Error during parsing: %w, import chain: ['%s']", d.File(), d.Line(), err, strings.Join(d.Token().imports, "','"))
424 + if len(d.Token().imports) > 0 {
425 + return fmt.Errorf("%w, at %s:%d import chain ['%s']", err, d.File(), d.Line(), strings.Join(d.Token().imports, "','"))
426 + }
427 + return fmt.Errorf("%w, at %s:%d", err, d.File(), d.Line())
428 }
429
430 // Delete deletes the current token and returns the updated slice
caddyconfig/httpcaddyfile/builtins.go
+4 -4
@@ -181,17 +181,17 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
181 case "protocols":
182 args := h.RemainingArgs()
183 if len(args) == 0 {
184 - return nil, h.SyntaxErr("one or two protocols")
184 + return nil, h.Errf("protocols requires one or two arguments")
185 }
186 if len(args) > 0 {
187 if _, ok := caddytls.SupportedProtocols[args[0]]; !ok {
188 - return nil, h.Errf("Wrong protocol name or protocol not supported: '%s'", args[0])
188 + return nil, h.Errf("wrong protocol name or protocol not supported: '%s'", args[0])
189 }
190 cp.ProtocolMin = args[0]
191 }
192 if len(args) > 1 {
193 if _, ok := caddytls.SupportedProtocols[args[1]]; !ok {
194 - return nil, h.Errf("Wrong protocol name or protocol not supported: '%s'", args[1])
194 + return nil, h.Errf("wrong protocol name or protocol not supported: '%s'", args[1])
195 }
196 cp.ProtocolMax = args[1]
197 }
@@ -199,7 +199,7 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
199 case "ciphers":
200 for h.NextArg() {
201 if !caddytls.CipherSuiteNameSupported(h.Val()) {
202 - return nil, h.Errf("Wrong cipher suite name or cipher suite not supported: '%s'", h.Val())
202 + return nil, h.Errf("wrong cipher suite name or cipher suite not supported: '%s'", h.Val())
203 }
204 cp.CipherSuites = append(cp.CipherSuites, h.Val())
205 }