Skip to content

Commit

Permalink
Fix error handling for the time func alias
Browse files Browse the repository at this point in the history
Fixes #8835
  • Loading branch information
bep committed Aug 1, 2021
1 parent 4d221ce commit 6c70e1f
Show file tree
Hide file tree
Showing 53 changed files with 122 additions and 65 deletions.
11 changes: 11 additions & 0 deletions hugolib/dates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

}
2 changes: 1 addition & 1 deletion tpl/cast/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/cast/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/collections/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/collections/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/compare/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/compare/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/crypto/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/crypto/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/data/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/data/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/debug/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/debug/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/encoding/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/encoding/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/fmt/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/fmt/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/hugo/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion tpl/hugo/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion tpl/images/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/images/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/inflect/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/inflect/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
7 changes: 5 additions & 2 deletions tpl/internal/templatefuncsRegistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tpl/js/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tpl/lang/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/lang/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/math/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/math/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/openapi/openapi3/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tpl/os/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/os/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/partials/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/partials/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/path/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/path/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/reflect/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/reflect/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/resources/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tpl/safe/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion tpl/safe/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
2 changes: 1 addition & 1 deletion tpl/site/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 6c70e1f

Please sign in to comment.