Browse Source

caddyfile: keep error chain info in Dispenser.Errf (#4233)

* caddyfile: Errf enable error chain unwrapping

* refactor: remove parseError
master
Ggicci 4 years ago
committed by GitHub
parent
commit
b6f51254ea
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      caddyconfig/caddyfile/dispenser.go
  2. 7
      caddyconfig/caddyfile/dispenser_test.go

6
caddyconfig/caddyfile/dispenser.go

@ -345,13 +345,13 @@ func (d *Dispenser) EOFErr() error {
// Err generates a custom parse-time error with a message of msg. // Err generates a custom parse-time error with a message of msg.
func (d *Dispenser) Err(msg string) error { func (d *Dispenser) Err(msg string) error {
msg = fmt.Sprintf("%s:%d - Error during parsing: %s", d.File(), d.Line(), msg) return d.Errf(msg)
return errors.New(msg)
} }
// Errf is like Err, but for formatted error messages // Errf is like Err, but for formatted error messages
func (d *Dispenser) Errf(format string, args ...interface{}) error { func (d *Dispenser) Errf(format string, args ...interface{}) error {
return d.Err(fmt.Sprintf(format, args...)) err := fmt.Errorf(format, args...)
return fmt.Errorf("%s:%d - Error during parsing: %w", d.File(), d.Line(), err)
} }
// Delete deletes the current token and returns the updated slice // Delete deletes the current token and returns the updated slice

7
caddyconfig/caddyfile/dispenser_test.go

@ -15,6 +15,7 @@
package caddyfile package caddyfile
import ( import (
"errors"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
@ -303,4 +304,10 @@ func TestDispenser_ArgErr_Err(t *testing.T) {
if !strings.Contains(err.Error(), "foobar") { if !strings.Contains(err.Error(), "foobar") {
t.Errorf("Expected error message with custom message in it ('foobar'); got '%v'", err) t.Errorf("Expected error message with custom message in it ('foobar'); got '%v'", err)
} }
var ErrBarIsFull = errors.New("bar is full")
bookingError := d.Errf("unable to reserve: %w", ErrBarIsFull)
if !errors.Is(bookingError, ErrBarIsFull) {
t.Errorf("Errf(): should be able to unwrap the error chain")
}
} }

Loading…
Cancel
Save