Fix error handling for the time func alias
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 1 Aug 2021 10:50:37 +0000 (12:50 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 1 Aug 2021 11:39:30 +0000 (13:39 +0200)
Fixes #8835

53 files changed:
hugolib/dates_test.go
tpl/cast/init.go
tpl/cast/init_test.go
tpl/collections/init.go
tpl/collections/init_test.go
tpl/compare/init.go
tpl/compare/init_test.go
tpl/crypto/init.go
tpl/crypto/init_test.go
tpl/data/init.go
tpl/data/init_test.go
tpl/debug/init.go
tpl/debug/init_test.go
tpl/encoding/init.go
tpl/encoding/init_test.go
tpl/fmt/init.go
tpl/fmt/init_test.go
tpl/hugo/init.go
tpl/hugo/init_test.go
tpl/images/init.go
tpl/images/init_test.go
tpl/inflect/init.go
tpl/inflect/init_test.go
tpl/internal/templatefuncsRegistry.go
tpl/js/init.go
tpl/lang/init.go
tpl/lang/init_test.go
tpl/math/init.go
tpl/math/init_test.go
tpl/openapi/openapi3/init.go
tpl/os/init.go
tpl/os/init_test.go
tpl/partials/init.go
tpl/partials/init_test.go
tpl/path/init.go
tpl/path/init_test.go
tpl/reflect/init.go
tpl/reflect/init_test.go
tpl/resources/init.go
tpl/safe/init.go
tpl/safe/init_test.go
tpl/site/init.go
tpl/site/init_test.go
tpl/strings/init.go
tpl/strings/init_test.go
tpl/templates/init.go
tpl/templates/init_test.go
tpl/time/init.go
tpl/time/init_test.go
tpl/transform/init.go
tpl/transform/init_test.go
tpl/urls/init.go
tpl/urls/init_test.go

index dfcb681a3a8e75111377b373b503799eb3cc12a6..4b4dc29d209c521de285148774965f4c515d83fa 100644 (file)
@@ -203,3 +203,14 @@ timeZone = "America/LosAngeles"   # Should be America/Los_Angeles
        b.Assert(err, qt.Not(qt.IsNil))
        b.Assert(err.Error(), qt.Contains, `failed to load config: invalid timeZone for language "en": unknown time zone America/LosAngeles`)
 }
+
+// Issue 8835
+func TestTimeOnError(t *testing.T) {
+       b := newTestSitesBuilder(t)
+
+       b.WithTemplates("index.html", `time: {{ time "2020-10-20" "invalid-timezone" }}`)
+       b.WithContent("p1.md", "")
+
+       b.Assert(b.BuildE(BuildCfg{}), qt.Not(qt.IsNil))
+
+}
index 3f1f3f253e6e8ecd80ce1b08c7478d4c51242dec..079be471995140617172cc7af0d0c31440cd781d 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.ToInt,
index dd024b77560ef641db1d0dcb297005750ee85663..5eb4a908651abbcc5238df5545cf4aff4a68d7fa 100644 (file)
@@ -37,5 +37,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{})
 }
index 4126b4410e463702351d44d52aab03b35702bbb0..dc4e1ff3183d2e8ca98722cffc50d57d143c1668 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.After,
index 3a3b2070f2e942b4ba3cf95c05467a968cae6124..570e58d933b99eca8a45bd7137f95be18c40bf6a 100644 (file)
@@ -37,5 +37,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{})
 }
index f96e382e5dce7860e52c3b216eb948f838e43142..9aa533f55611b47f2f54bcbef2494b0b6d6ea544 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Default,
index 29a525f93c393553d932ec27f2927e8731b6857b..8698cb5e3978e6b213253e1f39e68205af63aa6f 100644 (file)
@@ -36,5 +36,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{})
 }
index 2c6e9429876d2af5bf7d202ff9d01011fdbc7dad..5ce2a4b5e2f3b1665865c185088c2367e2ad007a 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.MD5,
index 120e1e4e7ddcb0cac1a9c0b5d31986e0cda55bb0..1c200d7776df17d7cafa72905ee4891c1006d7dd 100644 (file)
@@ -36,5 +36,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{})
 }
index 3bdc02786fc72bedff2afcedab321bc6071449e4..5ac24eaabe3fcc6396ce6775100d9c72169a0b94 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.GetCSV,
index 9174d42a6b29b160d554b4ab2408c07cd41d9412..631a91b3931c3f58ffc7ea70c8c1b3e2c22e0f0e 100644 (file)
@@ -41,5 +41,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{})
 }
index e478fb46d1327426ed0bbab3c7721a18d8f05ac8..1f032ce69f7237ce026ead5d353f90aaf4446598 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Dump,
index 35c6a193eae618b5f07a28e42f8d0e83cdcaf275..226915b340c1b80307bdef2e56551353e91f2b8b 100644 (file)
@@ -38,5 +38,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{})
 }
index f97b171826c504760c5aed650321100aba68cf2b..77c9c8c494c9b9d3a0befab7d73ae7da585c382e 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Base64Decode,
index 5fd71eb32ff8020e8ccad42eb47f93b591c445ff..666a4e5495fa07f120d7dd153cdaf3f0b82a8d0c 100644 (file)
@@ -36,5 +36,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{})
 }
index f322f511773faf6ba16d40e28c68e51f9eaae1dd..c02f985be645d68a913864e38fe002d89f047549 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Print,
index 8fa3945b87f8ebc6323a3dfb83f6e7e0f59e7c95..07b740a73d684a323ade7ad7c7825d6f6911466e 100644 (file)
@@ -38,5 +38,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{})
 }
index c57d4a27acbd2870a96b89493ca6a2db765228bd..f2c43893ef4a3d787c6af5c401b9950b1c377b64 100644 (file)
@@ -27,7 +27,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return h },
+                       Context: func(args ...interface{}) (interface{}, error) { return h, nil },
                }
 
                // We just add the Hugo struct as the namespace here. No method mappings.
index 9b1b14be43e4095a7e6d17fd32feb0dd604d5797..bc806448ed37b0ccf307ed1059773125fb1fea9a 100644 (file)
@@ -43,5 +43,7 @@ func TestInit(t *testing.T) {
        }
 
        c.Assert(found, qt.Equals, true)
-       c.Assert(ns.Context(), hqt.IsSameType, s.Hugo())
+       ctx, err := ns.Context()
+       c.Assert(err, qt.IsNil)
+       c.Assert(ctx, hqt.IsSameType, s.Hugo())
 }
index e9fd52e732839899e95592405ebb1439a593ae19..f3233f6e92b31e35dde93952702f58fb205a45aa 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Config,
index d6dc26fe7c67d908d0edc527838e80671171bc41..d8d8d78394cc2b7bb31ec3871dd1da8bb6d140e1 100644 (file)
@@ -36,5 +36,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{})
 }
index 52b234df3b0c6f11b23cbde6e82bb9f37df107d7..5488274654d6dd6135916723f7e5d045b35bc052 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Humanize,
index 322813b5f33e041422c2817902638526cf495529..38499838c962926ae4cd5498771f2385fde4fb88 100644 (file)
@@ -37,5 +37,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{})
 }
index 6d58d8d2bb6a4bc29b64a27c5255627b25fa711d..df300a5bb362a0832c5b40fd6d49d0234ddc0204 100644 (file)
@@ -49,7 +49,7 @@ type TemplateFuncsNamespace struct {
        Name string
 
        // This is the method receiver.
-       Context func(v ...interface{}) interface{}
+       Context func(v ...interface{}) (interface{}, error)
 
        // Additional info, aliases and examples, per method name.
        MethodMappings map[string]TemplateFuncMethodMapping
@@ -172,7 +172,10 @@ func (t *TemplateFuncsNamespace) toJSON() ([]byte, error) {
 
        buf.WriteString(fmt.Sprintf(`%q: {`, t.Name))
 
-       ctx := t.Context()
+       ctx, err := t.Context()
+       if err != nil {
+               return nil, err
+       }
        ctxType := reflect.TypeOf(ctx)
        for i := 0; i < ctxType.NumMethod(); i++ {
                method := ctxType.Method(i)
index 0af10bb10b3f562548ad580d8d1fb7770816a66d..4ab8671cc7102c61e118bf9bb5f0f538992215ab 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                return ns
index beb148ff628203406b1678806d19fcd9231989fc..f74b6fc35360a38fb81322395cbef1112b4e9a40 100644 (file)
@@ -27,7 +27,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Translate,
index 61d7b504715edd5e32241a70298047fe4bef0a40..e62db95b947c2fa2cd9b3e928925283edb09bc81 100644 (file)
@@ -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{})
 }
index 4d8a23fde56a731d0b5204f08e01edcd4aa9063d..32315d3622ae7965c6072f0e2a153cb957ae991f 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Add,
index 6c0ce0a9330861f83710edaba5322d225666e3c5..9998eaf902ed5e12c5843a6a029bf342b961a05d 100644 (file)
@@ -36,5 +36,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{})
 }
index 4b4396ff4cc51965c05a807cf55860585f384119..a0084d503fac622a00c398810bfca9dbd9a81843 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Unmarshal,
index 9d9b75473577652c7b9e2bc92068160fbeb4cb80..c25d63d561f352c080909aae704e4d6c939ca6a7 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Getenv,
index 6a91c743a0f2159b18137361069b46bd0419c406..5d756bab2dfb6c22da7856f8c72f2ae2eef1d041 100644 (file)
@@ -36,5 +36,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{})
 }
index 4666fa3365b08e4d3bd7da67157b7610123a37b1..d6670d99eaac36a655375283ff20f250dc4aba72 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Include,
index 6fd0b3e6dfb1127a1c5270331f2db9eca872c0ed..467e202d414784f621f84c93e35aa158b8097eb6 100644 (file)
@@ -40,5 +40,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{})
 }
index a7f65073cff72f735378c0dfa3b5082711df7fac..07f20d71700ea439c11cd50fcd3b8ecc685c2f29 100644 (file)
@@ -29,7 +29,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Split,
index 20744b239c14755d8dbe2d810d8da371e3a02dc2..2282c3305668bb80d7d24d09b8793263f14a0d09 100644 (file)
@@ -37,5 +37,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{})
 }
index 6ff3f8093d573c9649400983fd7eac20b16cb313..63500a6a45dfa7a871f39f33d1832ded547c66fa 100644 (file)
@@ -27,7 +27,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.IsMap,
index c0247b04513135568afafb4103dcafb67f617cd5..2ad33fc25836ac81fb5bc53fd3291eee2d429925 100644 (file)
@@ -37,5 +37,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{})
 }
index d0e20d0ae05c8e93b286af01450ccf7fe5762446..8bebec40d74e027b4853f6d2b2ad40b3cb79af8e 100644 (file)
@@ -30,7 +30,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Get,
index becaf38f3838419e14d167e75f7b8024fb6185ad..9fbae4044681510446c0c7811d88fd45f2d23e86 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.CSS,
index 2ed7b187265f61afa94338eedbe18a78f50b8f36..7aa1473d21c90e0628d3420f31ee102460ada343 100644 (file)
@@ -37,5 +37,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{})
 }
index 4dc9349973199527c972307a33eaf6d2e32a0e44..a24d28ad6fdac203df0ed54cb90fef11a1953a90 100644 (file)
@@ -27,7 +27,7 @@ func init() {
                s := d.Site
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return s },
+                       Context: func(args ...interface{}) (interface{}, error) { return s, nil },
                }
 
                if s == nil {
index f4c2ecd5c89e140ec3c2117292d7f3a57e2e18f6..46af2ef5b372150377f7c2df0e00a67cfc426070 100644 (file)
@@ -43,5 +43,7 @@ func TestInit(t *testing.T) {
        }
 
        c.Assert(found, qt.Equals, true)
-       c.Assert(ns.Context(), hqt.IsSameType, s)
+       ctx, err := ns.Context()
+       c.Assert(err, qt.IsNil)
+       c.Assert(ctx, hqt.IsSameType, s)
 }
index dab4fdbaad180d9e25401cc816568227bdf740ee..384a5cda7b4af764556a029be74b93f373c70d9d 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Chomp,
index dd15418c8a2d20ac312374a254ce44493a768085..39d928601f413c9728297a040260a2fdef888fed 100644 (file)
@@ -39,5 +39,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{})
 }
index 4ae5c12cc2ac42f0e6a73361b3548177d1d1fd0e..8da8440ae828ac36f5036e561eeb897b299f50e1 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Exists,
index cdad188bc3834d5f8132297ce27de26f37103788..ada53b185aa992f9586cdfa8cbbc8b4d1e57802f 100644 (file)
@@ -36,5 +36,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{})
 }
index 6cbdccb8f1f72818ebe9ffa585b32cb5e4c08965..0ef4fcdfd1ddad62476f60ff311568f56c2b51bc 100644 (file)
@@ -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/")
                                }
                        },
                }
index 8c00ec51fb059b934d721672fa6136a54484ab39..d7efabfa77709f8b2ef750a08f5cc78798958ecf 100644 (file)
@@ -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{})
 }
index 9e57c07f620b0e1a6f06830273f27988a0b36b56..aa7297bc4e56c840b6676bca22e9873bc590b2b7 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.Emojify,
index 47bd8a391e67fdff92516e747def7f6892ee3baa..ec3c358974a496a91744706c9f7b4604387c5945 100644 (file)
@@ -36,5 +36,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{})
 }
index 0abe0d125b0f711c67b2b9b213e7cdd67516fd98..0a97045e242492ef964e863405ce6beb067e2e19 100644 (file)
@@ -26,7 +26,7 @@ func init() {
 
                ns := &internal.TemplateFuncsNamespace{
                        Name:    name,
-                       Context: func(args ...interface{}) interface{} { return ctx },
+                       Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
                }
 
                ns.AddMethodMapping(ctx.AbsURL,
index 27b21144ad9769f8f565e4f513a821beddb2551c..7e53c247a5c6f9eec1446cb989c8ae18b9c2797b 100644 (file)
@@ -38,5 +38,8 @@ 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{})
+
 }