From 6c70e1f22f365322d5f754302e110c9ed716b215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 1 Aug 2021 12:50:37 +0200 Subject: [PATCH] Fix error handling for the time func alias Fixes #8835 --- hugolib/dates_test.go | 11 +++++++++++ tpl/cast/init.go | 2 +- tpl/cast/init_test.go | 4 +++- tpl/collections/init.go | 2 +- tpl/collections/init_test.go | 4 +++- tpl/compare/init.go | 2 +- tpl/compare/init_test.go | 4 +++- tpl/crypto/init.go | 2 +- tpl/crypto/init_test.go | 4 +++- tpl/data/init.go | 2 +- tpl/data/init_test.go | 4 +++- tpl/debug/init.go | 2 +- tpl/debug/init_test.go | 4 +++- tpl/encoding/init.go | 2 +- tpl/encoding/init_test.go | 4 +++- tpl/fmt/init.go | 2 +- tpl/fmt/init_test.go | 4 +++- tpl/hugo/init.go | 2 +- tpl/hugo/init_test.go | 4 +++- tpl/images/init.go | 2 +- tpl/images/init_test.go | 4 +++- tpl/inflect/init.go | 2 +- tpl/inflect/init_test.go | 4 +++- tpl/internal/templatefuncsRegistry.go | 7 +++++-- tpl/js/init.go | 2 +- tpl/lang/init.go | 2 +- tpl/lang/init_test.go | 4 +++- tpl/math/init.go | 2 +- tpl/math/init_test.go | 4 +++- tpl/openapi/openapi3/init.go | 2 +- tpl/os/init.go | 2 +- tpl/os/init_test.go | 4 +++- tpl/partials/init.go | 2 +- tpl/partials/init_test.go | 4 +++- tpl/path/init.go | 2 +- tpl/path/init_test.go | 4 +++- tpl/reflect/init.go | 2 +- tpl/reflect/init_test.go | 4 +++- tpl/resources/init.go | 2 +- tpl/safe/init.go | 2 +- tpl/safe/init_test.go | 4 +++- tpl/site/init.go | 2 +- tpl/site/init_test.go | 4 +++- tpl/strings/init.go | 2 +- tpl/strings/init_test.go | 4 +++- tpl/templates/init.go | 2 +- tpl/templates/init_test.go | 4 +++- tpl/time/init.go | 20 +++++++------------- tpl/time/init_test.go | 4 +++- tpl/transform/init.go | 2 +- tpl/transform/init_test.go | 4 +++- tpl/urls/init.go | 2 +- tpl/urls/init_test.go | 5 ++++- 53 files changed, 122 insertions(+), 65 deletions(-) diff --git a/hugolib/dates_test.go b/hugolib/dates_test.go index dfcb681a..4b4dc29d 100644 --- a/hugolib/dates_test.go +++ b/hugolib/dates_test.go @@ -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)) + +} diff --git a/tpl/cast/init.go b/tpl/cast/init.go index 3f1f3f25..079be471 100644 --- a/tpl/cast/init.go +++ b/tpl/cast/init.go @@ -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, diff --git a/tpl/cast/init_test.go b/tpl/cast/init_test.go index dd024b77..5eb4a908 100644 --- a/tpl/cast/init_test.go +++ b/tpl/cast/init_test.go @@ -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{}) } diff --git a/tpl/collections/init.go b/tpl/collections/init.go index 4126b441..dc4e1ff3 100644 --- a/tpl/collections/init.go +++ b/tpl/collections/init.go @@ -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, diff --git a/tpl/collections/init_test.go b/tpl/collections/init_test.go index 3a3b2070..570e58d9 100644 --- a/tpl/collections/init_test.go +++ b/tpl/collections/init_test.go @@ -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{}) } diff --git a/tpl/compare/init.go b/tpl/compare/init.go index f96e382e..9aa533f5 100644 --- a/tpl/compare/init.go +++ b/tpl/compare/init.go @@ -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, diff --git a/tpl/compare/init_test.go b/tpl/compare/init_test.go index 29a525f9..8698cb5e 100644 --- a/tpl/compare/init_test.go +++ b/tpl/compare/init_test.go @@ -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{}) } diff --git a/tpl/crypto/init.go b/tpl/crypto/init.go index 2c6e9429..5ce2a4b5 100644 --- a/tpl/crypto/init.go +++ b/tpl/crypto/init.go @@ -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, diff --git a/tpl/crypto/init_test.go b/tpl/crypto/init_test.go index 120e1e4e..1c200d77 100644 --- a/tpl/crypto/init_test.go +++ b/tpl/crypto/init_test.go @@ -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{}) } diff --git a/tpl/data/init.go b/tpl/data/init.go index 3bdc0278..5ac24eaa 100644 --- a/tpl/data/init.go +++ b/tpl/data/init.go @@ -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, diff --git a/tpl/data/init_test.go b/tpl/data/init_test.go index 9174d42a..631a91b3 100644 --- a/tpl/data/init_test.go +++ b/tpl/data/init_test.go @@ -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{}) } diff --git a/tpl/debug/init.go b/tpl/debug/init.go index e478fb46..1f032ce6 100644 --- a/tpl/debug/init.go +++ b/tpl/debug/init.go @@ -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, diff --git a/tpl/debug/init_test.go b/tpl/debug/init_test.go index 35c6a193..226915b3 100644 --- a/tpl/debug/init_test.go +++ b/tpl/debug/init_test.go @@ -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{}) } diff --git a/tpl/encoding/init.go b/tpl/encoding/init.go index f97b1718..77c9c8c4 100644 --- a/tpl/encoding/init.go +++ b/tpl/encoding/init.go @@ -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, diff --git a/tpl/encoding/init_test.go b/tpl/encoding/init_test.go index 5fd71eb3..666a4e54 100644 --- a/tpl/encoding/init_test.go +++ b/tpl/encoding/init_test.go @@ -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{}) } diff --git a/tpl/fmt/init.go b/tpl/fmt/init.go index f322f511..c02f985b 100644 --- a/tpl/fmt/init.go +++ b/tpl/fmt/init.go @@ -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, diff --git a/tpl/fmt/init_test.go b/tpl/fmt/init_test.go index 8fa3945b..07b740a7 100644 --- a/tpl/fmt/init_test.go +++ b/tpl/fmt/init_test.go @@ -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{}) } diff --git a/tpl/hugo/init.go b/tpl/hugo/init.go index c57d4a27..f2c43893 100644 --- a/tpl/hugo/init.go +++ b/tpl/hugo/init.go @@ -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. diff --git a/tpl/hugo/init_test.go b/tpl/hugo/init_test.go index 9b1b14be..bc806448 100644 --- a/tpl/hugo/init_test.go +++ b/tpl/hugo/init_test.go @@ -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()) } diff --git a/tpl/images/init.go b/tpl/images/init.go index e9fd52e7..f3233f6e 100644 --- a/tpl/images/init.go +++ b/tpl/images/init.go @@ -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, diff --git a/tpl/images/init_test.go b/tpl/images/init_test.go index d6dc26fe..d8d8d783 100644 --- a/tpl/images/init_test.go +++ b/tpl/images/init_test.go @@ -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{}) } diff --git a/tpl/inflect/init.go b/tpl/inflect/init.go index 52b234df..54882746 100644 --- a/tpl/inflect/init.go +++ b/tpl/inflect/init.go @@ -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, diff --git a/tpl/inflect/init_test.go b/tpl/inflect/init_test.go index 322813b5..38499838 100644 --- a/tpl/inflect/init_test.go +++ b/tpl/inflect/init_test.go @@ -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{}) } diff --git a/tpl/internal/templatefuncsRegistry.go b/tpl/internal/templatefuncsRegistry.go index 6d58d8d2..df300a5b 100644 --- a/tpl/internal/templatefuncsRegistry.go +++ b/tpl/internal/templatefuncsRegistry.go @@ -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) diff --git a/tpl/js/init.go b/tpl/js/init.go index 0af10bb1..4ab8671c 100644 --- a/tpl/js/init.go +++ b/tpl/js/init.go @@ -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 diff --git a/tpl/lang/init.go b/tpl/lang/init.go index beb148ff..f74b6fc3 100644 --- a/tpl/lang/init.go +++ b/tpl/lang/init.go @@ -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, diff --git a/tpl/lang/init_test.go b/tpl/lang/init_test.go index 61d7b504..e62db95b 100644 --- a/tpl/lang/init_test.go +++ b/tpl/lang/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{}) } diff --git a/tpl/math/init.go b/tpl/math/init.go index 4d8a23fd..32315d36 100644 --- a/tpl/math/init.go +++ b/tpl/math/init.go @@ -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, diff --git a/tpl/math/init_test.go b/tpl/math/init_test.go index 6c0ce0a9..9998eaf9 100644 --- a/tpl/math/init_test.go +++ b/tpl/math/init_test.go @@ -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{}) } diff --git a/tpl/openapi/openapi3/init.go b/tpl/openapi/openapi3/init.go index 4b4396ff..a0084d50 100644 --- a/tpl/openapi/openapi3/init.go +++ b/tpl/openapi/openapi3/init.go @@ -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, diff --git a/tpl/os/init.go b/tpl/os/init.go index 9d9b7547..c25d63d5 100644 --- a/tpl/os/init.go +++ b/tpl/os/init.go @@ -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, diff --git a/tpl/os/init_test.go b/tpl/os/init_test.go index 6a91c743..5d756bab 100644 --- a/tpl/os/init_test.go +++ b/tpl/os/init_test.go @@ -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{}) } diff --git a/tpl/partials/init.go b/tpl/partials/init.go index 4666fa33..d6670d99 100644 --- a/tpl/partials/init.go +++ b/tpl/partials/init.go @@ -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, diff --git a/tpl/partials/init_test.go b/tpl/partials/init_test.go index 6fd0b3e6..467e202d 100644 --- a/tpl/partials/init_test.go +++ b/tpl/partials/init_test.go @@ -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{}) } diff --git a/tpl/path/init.go b/tpl/path/init.go index a7f65073..07f20d71 100644 --- a/tpl/path/init.go +++ b/tpl/path/init.go @@ -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, diff --git a/tpl/path/init_test.go b/tpl/path/init_test.go index 20744b23..2282c330 100644 --- a/tpl/path/init_test.go +++ b/tpl/path/init_test.go @@ -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{}) } diff --git a/tpl/reflect/init.go b/tpl/reflect/init.go index 6ff3f809..63500a6a 100644 --- a/tpl/reflect/init.go +++ b/tpl/reflect/init.go @@ -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, diff --git a/tpl/reflect/init_test.go b/tpl/reflect/init_test.go index c0247b04..2ad33fc2 100644 --- a/tpl/reflect/init_test.go +++ b/tpl/reflect/init_test.go @@ -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{}) } diff --git a/tpl/resources/init.go b/tpl/resources/init.go index d0e20d0a..8bebec40 100644 --- a/tpl/resources/init.go +++ b/tpl/resources/init.go @@ -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, diff --git a/tpl/safe/init.go b/tpl/safe/init.go index becaf38f..9fbae404 100644 --- a/tpl/safe/init.go +++ b/tpl/safe/init.go @@ -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, diff --git a/tpl/safe/init_test.go b/tpl/safe/init_test.go index 2ed7b187..7aa1473d 100644 --- a/tpl/safe/init_test.go +++ b/tpl/safe/init_test.go @@ -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{}) } diff --git a/tpl/site/init.go b/tpl/site/init.go index 4dc93499..a24d28ad 100644 --- a/tpl/site/init.go +++ b/tpl/site/init.go @@ -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 { diff --git a/tpl/site/init_test.go b/tpl/site/init_test.go index f4c2ecd5..46af2ef5 100644 --- a/tpl/site/init_test.go +++ b/tpl/site/init_test.go @@ -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) } diff --git a/tpl/strings/init.go b/tpl/strings/init.go index dab4fdba..384a5cda 100644 --- a/tpl/strings/init.go +++ b/tpl/strings/init.go @@ -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, diff --git a/tpl/strings/init_test.go b/tpl/strings/init_test.go index dd15418c..39d92860 100644 --- a/tpl/strings/init_test.go +++ b/tpl/strings/init_test.go @@ -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{}) } diff --git a/tpl/templates/init.go b/tpl/templates/init.go index 4ae5c12c..8da8440a 100644 --- a/tpl/templates/init.go +++ b/tpl/templates/init.go @@ -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, diff --git a/tpl/templates/init_test.go b/tpl/templates/init_test.go index cdad188b..ada53b18 100644 --- a/tpl/templates/init_test.go +++ b/tpl/templates/init_test.go @@ -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{}) } diff --git a/tpl/time/init.go b/tpl/time/init.go index 6cbdccb8..0ef4fcdf 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 8c00ec51..d7efabfa 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{}) } diff --git a/tpl/transform/init.go b/tpl/transform/init.go index 9e57c07f..aa7297bc 100644 --- a/tpl/transform/init.go +++ b/tpl/transform/init.go @@ -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, diff --git a/tpl/transform/init_test.go b/tpl/transform/init_test.go index 47bd8a39..ec3c3589 100644 --- a/tpl/transform/init_test.go +++ b/tpl/transform/init_test.go @@ -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{}) } diff --git a/tpl/urls/init.go b/tpl/urls/init.go index 0abe0d12..0a97045e 100644 --- a/tpl/urls/init.go +++ b/tpl/urls/init.go @@ -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, diff --git a/tpl/urls/init_test.go b/tpl/urls/init_test.go index 27b21144..7e53c247 100644 --- a/tpl/urls/init_test.go +++ b/tpl/urls/init_test.go @@ -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{}) + }