diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-08-01 12:50:37 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-08-01 13:39:30 +0200 |
commit | 6c70e1f22f365322d5f754302e110c9ed716b215 (patch) | |
tree | bcf30eb942afad09a2a7f56e0d0f490614ec1311 /tpl/time | |
parent | 4d221ce468a1209ee9dd6cbece9d1273dad6a29b (diff) |
Fix error handling for the time func alias
Fixes #8835
Diffstat (limited to 'tpl/time')
-rw-r--r-- | tpl/time/init.go | 20 | ||||
-rw-r--r-- | tpl/time/init_test.go | 4 |
2 files changed, 10 insertions, 14 deletions
diff --git a/tpl/time/init.go b/tpl/time/init.go index 6cbdccb8f..0ef4fcdfd 100644 --- a/tpl/time/init.go +++ b/tpl/time/init.go @@ -14,6 +14,8 @@ package time import ( + "errors" + "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/langs" "github.com/gohugoio/hugo/tpl/internal" @@ -30,7 +32,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) interface{} { + Context: func(args ...interface{}) (interface{}, error) { // Handle overlapping "time" namespace and func. // // If no args are passed to `time`, assume namespace usage and @@ -40,23 +42,15 @@ func init() { switch len(args) { case 0: - return ctx + return ctx, nil case 1: - t, err := ctx.AsTime(args[0]) - if err != nil { - return err - } - return t + return ctx.AsTime(args[0]) case 2: - t, err := ctx.AsTime(args[0], args[1]) - if err != nil { - return err - } - return t + return ctx.AsTime(args[0], args[1]) // 3 or more arguments. Currently not supported. default: - return "Invalid arguments supplied to `time`. Refer to time documentation: https://gohugo.io/functions/time/" + return nil, errors.New("Invalid arguments supplied to `time`. Refer to time documentation: https://gohugo.io/functions/time/") } }, } diff --git a/tpl/time/init_test.go b/tpl/time/init_test.go index 8c00ec51f..d7efabfa7 100644 --- a/tpl/time/init_test.go +++ b/tpl/time/init_test.go @@ -42,5 +42,7 @@ func TestInit(t *testing.T) { } c.Assert(found, qt.Equals, true) - c.Assert(ns.Context(), hqt.IsSameType, &Namespace{}) + ctx, err := ns.Context() + c.Assert(err, qt.IsNil) + c.Assert(ctx, hqt.IsSameType, &Namespace{}) } |