summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-08-01 12:50:37 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-08-01 13:39:30 +0200
commit6c70e1f22f365322d5f754302e110c9ed716b215 (patch)
treebcf30eb942afad09a2a7f56e0d0f490614ec1311
parent4d221ce468a1209ee9dd6cbece9d1273dad6a29b (diff)
Fix error handling for the time func alias
Fixes #8835
-rw-r--r--hugolib/dates_test.go11
-rw-r--r--tpl/cast/init.go2
-rw-r--r--tpl/cast/init_test.go4
-rw-r--r--tpl/collections/init.go2
-rw-r--r--tpl/collections/init_test.go4
-rw-r--r--tpl/compare/init.go2
-rw-r--r--tpl/compare/init_test.go4
-rw-r--r--tpl/crypto/init.go2
-rw-r--r--tpl/crypto/init_test.go4
-rw-r--r--tpl/data/init.go2
-rw-r--r--tpl/data/init_test.go4
-rw-r--r--tpl/debug/init.go2
-rw-r--r--tpl/debug/init_test.go4
-rw-r--r--tpl/encoding/init.go2
-rw-r--r--tpl/encoding/init_test.go4
-rw-r--r--tpl/fmt/init.go2
-rw-r--r--tpl/fmt/init_test.go4
-rw-r--r--tpl/hugo/init.go2
-rw-r--r--tpl/hugo/init_test.go4
-rw-r--r--tpl/images/init.go2
-rw-r--r--tpl/images/init_test.go4
-rw-r--r--tpl/inflect/init.go2
-rw-r--r--tpl/inflect/init_test.go4
-rw-r--r--tpl/internal/templatefuncsRegistry.go7
-rw-r--r--tpl/js/init.go2
-rw-r--r--tpl/lang/init.go2
-rw-r--r--tpl/lang/init_test.go4
-rw-r--r--tpl/math/init.go2
-rw-r--r--tpl/math/init_test.go4
-rw-r--r--tpl/openapi/openapi3/init.go2
-rw-r--r--tpl/os/init.go2
-rw-r--r--tpl/os/init_test.go4
-rw-r--r--tpl/partials/init.go2
-rw-r--r--tpl/partials/init_test.go4
-rw-r--r--tpl/path/init.go2
-rw-r--r--tpl/path/init_test.go4
-rw-r--r--tpl/reflect/init.go2
-rw-r--r--tpl/reflect/init_test.go4
-rw-r--r--tpl/resources/init.go2
-rw-r--r--tpl/safe/init.go2
-rw-r--r--tpl/safe/init_test.go4
-rw-r--r--tpl/site/init.go2
-rw-r--r--tpl/site/init_test.go4
-rw-r--r--tpl/strings/init.go2
-rw-r--r--tpl/strings/init_test.go4
-rw-r--r--tpl/templates/init.go2
-rw-r--r--tpl/templates/init_test.go4
-rw-r--r--tpl/time/init.go20
-rw-r--r--tpl/time/init_test.go4
-rw-r--r--tpl/transform/init.go2
-rw-r--r--tpl/transform/init_test.go4
-rw-r--r--tpl/urls/init.go2
-rw-r--r--tpl/urls/init_test.go5
53 files changed, 122 insertions, 65 deletions
diff --git a/hugolib/dates_test.go b/hugolib/dates_test.go
index dfcb681a3..4b4dc29d2 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 3f1f3f253..079be4719 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 dd024b775..5eb4a9086 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 4126b4410..dc4e1ff31 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 3a3b2070f..570e58d93 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 f96e382e5..9aa533f55 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 29a525f93..8698cb5e3 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 2c6e94298..5ce2a4b5e 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 120e1e4e7..1c200d777 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 3bdc02786..5ac24eaab 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 9174d42a6..631a91b39 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 e478fb46d..1f032ce69 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 35c6a193e..226915b34 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 f97b17182..77c9c8c49 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 5fd71eb32..666a4e549 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 f322f5117..c02f985be 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 8fa3945b8..07b740a73 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 c57d4a27a..f2c43893e 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 9b1b14be4..bc806448e 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 e9fd52e73..f3233f6e9 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 d6dc26fe7..d8d8d7839 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 52b234df3..548827465 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 322813b5f..38499838c 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 6d58d8d2b..df300a5bb 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 0af10bb10..4ab8671cc 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 beb148ff6..f74b6fc35 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 61d7b5047..e62db95b9 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 4d8a23fde..32315d362 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 6c0ce0a93..9998eaf90 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 4b4396ff4..a0084d503 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 9d9b75473..c25d63d56 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 6a91c743a..5d756bab2 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 4666fa336..d6670d99e 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 6fd0b3e6d..467e202d4 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 a7f65073c..07f20d717 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