diff options
author | Ggicci <ggicci.t@gmail.com> | 2021-07-19 22:35:14 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-19 08:35:14 -0600 |
commit | b6f51254ea2dcb29dde0ffb4076802505a43ace1 (patch) | |
tree | a0e750984bfc08063adba78fc6d2bcf24a7a6e94 /caddyconfig | |
parent | 124ba1ba714220322c65625012c564f89b75bfc9 (diff) |
caddyfile: keep error chain info in Dispenser.Errf (#4233)
* caddyfile: Errf enable error chain unwrapping
* refactor: remove parseError
Diffstat (limited to 'caddyconfig')
-rwxr-xr-x | caddyconfig/caddyfile/dispenser.go | 6 | ||||
-rwxr-xr-x | caddyconfig/caddyfile/dispenser_test.go | 7 |
2 files changed, 10 insertions, 3 deletions
diff --git a/caddyconfig/caddyfile/dispenser.go b/caddyconfig/caddyfile/dispenser.go index fe8c235..fa7f5e7 100755 --- a/caddyconfig/caddyfile/dispenser.go +++ b/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. func (d *Dispenser) Err(msg string) error { - msg = fmt.Sprintf("%s:%d - Error during parsing: %s", d.File(), d.Line(), msg) - return errors.New(msg) + return d.Errf(msg) } // Errf is like Err, but for formatted error messages 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 diff --git a/caddyconfig/caddyfile/dispenser_test.go b/caddyconfig/caddyfile/dispenser_test.go index 577872f..b64a973 100755 --- a/caddyconfig/caddyfile/dispenser_test.go +++ b/caddyconfig/caddyfile/dispenser_test.go @@ -15,6 +15,7 @@ package caddyfile import ( + "errors" "reflect" "strings" "testing" @@ -303,4 +304,10 @@ func TestDispenser_ArgErr_Err(t *testing.T) { if !strings.Contains(err.Error(), "foobar") { 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") + } } |