Fixes #34201
Change-Id: Ic2e2967e4b01167345cf38bd006cabb206a64377
GitHub-Last-Rev:
5d0c4856550614484a8dbfb68c37aa1abcfcc529
GitHub-Pull-Request: golang/go#42398
Reviewed-on: https://go-review.googlesource.com/c/go/+/267838
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Trust: Daniel Martí <mvdan@mvdan.cc>
Trust: Pontus Leitzler <leitzler@gmail.com>
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Go Bot <gobot@golang.org>
// error to the caller.
if err != nil {
s.at(node)
- s.errorf("error calling %s: %v", name, err)
+ s.errorf("error calling %s: %w", name, err)
}
if v.Type() == reflectValueType {
v = v.Interface().(reflect.Value)
}
}
+type CustomError struct{}
+
+func (*CustomError) Error() string { return "heyo !" }
+
+// Check that a custom error can be returned.
+func TestExecError_CustomError(t *testing.T) {
+ failingFunc := func() (string, error) {
+ return "", &CustomError{}
+ }
+ tmpl := Must(New("top").Funcs(FuncMap{
+ "err": failingFunc,
+ }).Parse("{{ err }}"))
+
+ var b bytes.Buffer
+ err := tmpl.Execute(&b, nil)
+
+ var e *CustomError
+ if !errors.As(err, &e) {
+ t.Fatalf("expected custom error; got %s", err)
+ }
+}
+
func TestJSEscaping(t *testing.T) {
testCases := []struct {
in, exp string
// return value evaluates to non-nil during execution, execution terminates and
// Execute returns that error.
//
+// Errors returned by Execute wrap the underlying error; call errors.As to
+// uncover them.
+//
// When template execution invokes a function with an argument list, that list
// must be assignable to the function's parameter types. Functions meant to
// apply to arguments of arbitrary type can use parameters of type interface{} or
var err error
if argv[i], err = prepareArg(arg, argType); err != nil {
- return reflect.Value{}, fmt.Errorf("arg %d: %s", i, err)
+ return reflect.Value{}, fmt.Errorf("arg %d: %w", i, err)
}
}
return safeCall(fn, argv)