Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
caddyfile: keep error chain info in Dispenser.Errf (#4233)
* caddyfile: Errf enable error chain unwrapping * refactor: remove parseError
Ggicci committed
Jul 19, 2021 at 22:35 UTC
b6f51254ea2dcb29dde0ffb4076802505a43ace1
2 files changed
+10
-3
caddyconfig/caddyfile/dispenser.go
+3
-3
@@ -345,13 +345,13 @@ func (d *Dispenser) EOFErr() error {
345
346
// Err generates a custom parse-time error with a message of msg.
347
func (d *Dispenser) Err(msg string) error {
348
- msg = fmt.Sprintf("%s:%d - Error during parsing: %s", d.File(), d.Line(), msg)
349
- return errors.New(msg)
348
+ return d.Errf(msg)
349
}
350
351
// Errf is like Err, but for formatted error messages
352
func (d *Dispenser) Errf(format string, args ...interface{}) error {
354
- return d.Err(fmt.Sprintf(format, args...))
353
+ err := fmt.Errorf(format, args...)
354
+ return fmt.Errorf("%s:%d - Error during parsing: %w", d.File(), d.Line(), err)
355
}
356
357
// Delete deletes the current token and returns the updated slice
caddyconfig/caddyfile/dispenser_test.go
+7
@@ -15,6 +15,7 @@
15
package caddyfile
16
17
import (
18
+ "errors"
19
"reflect"
20
"strings"
21
"testing"
@@ -303,4 +304,10 @@ func TestDispenser_ArgErr_Err(t *testing.T) {
304
if !strings.Contains(err.Error(), "foobar") {
305
t.Errorf("Expected error message with custom message in it ('foobar'); got '%v'", err)
306
}
307
+
308
+ var ErrBarIsFull = errors.New("bar is full")
309
+ bookingError := d.Errf("unable to reserve: %w", ErrBarIsFull)
310
+ if !errors.Is(bookingError, ErrBarIsFull) {
311
+ t.Errorf("Errf(): should be able to unwrap the error chain")
312
+ }
313
}