]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Adjust error handling in ToMath vs try (note)
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 9 Jan 2025 07:17:40 +0000 (08:17 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 9 Jan 2025 10:47:19 +0000 (11:47 +0100)
Closes #13239

common/types/types.go
hugolib/hugo_sites_build.go
tpl/transform/transform.go
tpl/transform/transform_integration_test.go

index d32391a88b7c1a8c6352abc0749cca203156a171..062ecc4034f27923742d1e9c8b34e64604120e61 100644 (file)
@@ -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
-}
index 1518e2db848eeb9e26c83b63ba5e943dceb76986..7e7f61031ca9423fe1f1c9dfaffa8d89c03b8c77 100644 (file)
@@ -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
        }
index c9af141ed7bb45c65153539235b84acfaa34b966..fc29a46df99474b40c315dcd7eaadcb912d5897b 100644 (file)
@@ -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"
@@ -200,15 +199,13 @@ 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) {
        if len(args) < 1 {
-               return res, errors.New("must provide at least one argument")
+               return "", errors.New("must provide at least one argument")
        }
        expression, err := cast.ToStringE(args[0])
        if err != nil {
-               return res, err
+               return "", err
        }
 
        katexInput := warpc.KatexInput{
@@ -223,7 +220,7 @@ func (ns *Namespace) ToMath(ctx context.Context, args ...any) (types.Result[temp
 
        if len(args) > 1 {
                if err := mapstructure.WeakDecode(args[1], &katexInput.Options); err != nil {
-                       return res, err
+                       return "", err
                }
        }
 
@@ -259,13 +256,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.
index 5f34ff81bf5891f96c814327397bc9458e247c5f..276b9b059f754e2b027dcc34fd18c8fbefae5374 100644 (file)
@@ -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/")
        })
 }