Skip to content

Commit

Permalink
Adjust error handling in ToMath vs try (note)
Browse files Browse the repository at this point in the history
Closes #13239
  • Loading branch information
bep committed Jan 9, 2025
1 parent 892b491 commit 98cea69
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
19 changes: 0 additions & 19 deletions common/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,3 @@ func NewBool(b bool) *bool {
type PrintableValueProvider interface {
PrintableValue() any
}

var _ PrintableValueProvider = Result[any]{}

// Result is a generic result type.
type Result[T any] struct {
// The result value.
Value T

// The error value.
Err error
}

// PrintableValue returns the value or panics if there is an error.
func (r Result[T]) PrintableValue() any {
if r.Err != nil {
panic(r.Err)
}
return r.Value
}
12 changes: 8 additions & 4 deletions hugolib/hugo_sites_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,14 @@ func (h *HugoSites) render(l logg.LevelLogger, config *BuildCfg) error {
if err == nil {
return nil
}
if strings.Contains(err.Error(), "can't evaluate field Err in type resource.Resource") {
// In Hugo 0.141.0 we replaced the special error handling for resources.GetRemote
// with the more general try.
return fmt.Errorf("%s: Resource.Err was removed in Hugo v0.141.0 and replaced with a new try keyword, see https://gohugo.io/functions/go-template/try/", err)
// In Hugo 0.141.0 we replaced the special error handling for resources.GetRemote
// with the more general try.
if strings.Contains(err.Error(), "can't evaluate field Err in type") {
if strings.Contains(err.Error(), "resource.Resource") {
return fmt.Errorf("%s: Resource.Err was removed in Hugo v0.141.0 and replaced with a new try keyword, see https://gohugo.io/functions/go-template/try/", err)
} else if strings.Contains(err.Error(), "template.HTML") {
return fmt.Errorf("%s: the return type of transform.ToMath was changed in Hugo v0.141.0 and the error handling replaced with a new try keyword, see https://gohugo.io/functions/go-template/try/", err)
}
}
return err
}
Expand Down
13 changes: 5 additions & 8 deletions tpl/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/gohugoio/hugo/cache/dynacache"
"github.com/gohugoio/hugo/common/hashing"
"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/internal/warpc"
"github.com/gohugoio/hugo/markup/converter/hooks"
"github.com/gohugoio/hugo/markup/highlight"
Expand Down Expand Up @@ -200,8 +199,8 @@ func (ns *Namespace) Plainify(s any) (template.HTML, error) {

// ToMath converts a LaTeX string to math in the given format, default MathML.
// This uses KaTeX to render the math, see https://katex.org/.
func (ns *Namespace) ToMath(ctx context.Context, args ...any) (types.Result[template.HTML], error) {
var res types.Result[template.HTML]
func (ns *Namespace) ToMath(ctx context.Context, args ...any) (template.HTML, error) {
var res template.HTML

if len(args) < 1 {
return res, errors.New("must provide at least one argument")
Expand Down Expand Up @@ -259,13 +258,11 @@ func (ns *Namespace) ToMath(ctx context.Context, args ...any) (types.Result[temp

return template.HTML(s), err
})

res = types.Result[template.HTML]{
Value: v,
Err: err,
if err != nil {
return "", err
}

return res, nil
return v, nil
}

// For internal use.
Expand Down
26 changes: 23 additions & 3 deletions tpl/transform/transform_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,38 @@ disableKinds = ['page','rss','section','sitemap','taxonomy','term']
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
-- layouts/index.html --
{{ with transform.ToMath "c = \\foo{a^2 + b^2}" }}
{{ with try (transform.ToMath "c = \\foo{a^2 + b^2}") }}
{{ with .Err }}
{{ warnf "error: %s" . }}
{{ else }}
{{ . }}
{{ .Value }}
{{ end }}
{{ end }}
`
b, err := hugolib.TestE(t, files, hugolib.TestOptWarn())

b.Assert(err, qt.IsNil)
b.AssertLogContains("WARN error: KaTeX parse error: Undefined control sequence: \\foo")
b.AssertLogContains("WARN error: template: index.html:1:22: executing \"index.html\" at <transform.ToMath>: error calling ToMath: KaTeX parse error: Undefined control sequence: \\foo at position 5: c = \\̲f̲o̲o̲{a^2 + b^2}")
})

// See issue 13239.
t.Run("Handle in template, old Err construct", func(t *testing.T) {
files := `
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
-- layouts/index.html --
{{ with transform.ToMath "c = \\pm\\sqrt{a^2 + b^2}" }}
{{ with .Err }}
{{ warnf "error: %s" . }}
{{ else }}
{{ . }}
{{ end }}
{{ end }}
`
b, err := hugolib.TestE(t, files, hugolib.TestOptWarn())

b.Assert(err, qt.IsNotNil)
b.Assert(err.Error(), qt.Contains, "the return type of transform.ToMath was changed in Hugo v0.141.0 and the error handling replaced with a new try keyword, see https://gohugo.io/functions/go-template/try/")
})
}

Expand Down

0 comments on commit 98cea69

Please sign in to comment.