From: Bjørn Erik Pedersen Date: Thu, 18 Dec 2025 10:32:42 +0000 (+0100) Subject: Add full filename to image processing error messages if possible X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=65a76664e3279b585437148860148cbaad6c2d57;p=brevno-suite%2Fhugo Add full filename to image processing error messages if possible Fixes #14278 --- diff --git a/internal/warpc/webp_integration_test.go b/internal/warpc/webp_integration_test.go index 5e45e2491..8a98541b2 100644 --- a/internal/warpc/webp_integration_test.go +++ b/internal/warpc/webp_integration_test.go @@ -14,6 +14,8 @@ package warpc_test import ( + "path/filepath" + "runtime" "testing" qt "github.com/frankban/quicktest" @@ -70,10 +72,19 @@ sourcefilename: ../../resources/testdata/webp/invalid.webp {{ $resized := $image.Resize "123x456 webp" }} Resized RelPermalink: {{ $resized.RelPermalink }}| ` + tempDir := t.TempDir() - b, err := hugolib.TestE(t, files) - + b, err := hugolib.TestE(t, files, hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) { + cfg.NeedsOsFS = true + cfg.WorkingDir = tempDir + })) b.Assert(err, qt.IsNotNil) + + if runtime.GOOS != "windows" { + // Make sure the full image filename is in the error message. + filename := filepath.Join(tempDir, "assets/invalid.webp") + b.Assert(err.Error(), qt.Contains, filename) + } } // This test isn't great, but we have golden tests to verify the output itself. diff --git a/resources/image.go b/resources/image.go index d487cd69b..694d0f752 100644 --- a/resources/image.go +++ b/resources/image.go @@ -28,7 +28,6 @@ import ( "github.com/gohugoio/hugo/cache/filecache" "github.com/gohugoio/hugo/common/hashing" - "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/paths" "github.com/disintegration/gift" @@ -212,7 +211,6 @@ func (i *imageResource) Process(spec string) (images.ImageResource, error) { // filter and returns the transformed image. If one of width or height is 0, the image aspect // ratio is preserved. func (i *imageResource) Resize(spec string) (images.ImageResource, error) { - defer herrors.Recover() return i.processActionSpec(images.ActionResize, spec) } @@ -291,7 +289,14 @@ func (i *imageResource) Filter(filters ...any) (images.ImageResource, error) { func (i *imageResource) processActionSpec(action, spec string) (images.ImageResource, error) { options := append([]string{action}, strings.Fields(strings.ToLower(spec))...) - return i.processOptions(options) + ir, err := i.processOptions(options) + if err != nil { + if sourcePath := i.sourcePath(); sourcePath != "" { + err = fmt.Errorf("failed to %s image %q: %w", action, sourcePath, err) + } + return nil, err + } + return ir, nil } func (i *imageResource) processOptions(options []string) (images.ImageResource, error) { diff --git a/resources/resource.go b/resources/resource.go index 9886f2db4..1666e7692 100644 --- a/resources/resource.go +++ b/resources/resource.go @@ -242,6 +242,7 @@ type baseResourceInternal interface { resource.Source resource.NameNormalizedProvider + sourcePath() string fileInfo mediaTypeAssigner targetPather