Skip to content

Commit 65a7666

Browse files
committed
Add full filename to image processing error messages if possible
Fixes #14278
1 parent da5b1fc commit 65a7666

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

‎internal/warpc/webp_integration_test.go‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package warpc_test
1515

1616
import (
17+
"path/filepath"
18+
"runtime"
1719
"testing"
1820

1921
qt "github.com/frankban/quicktest"
@@ -70,10 +72,19 @@ sourcefilename: ../../resources/testdata/webp/invalid.webp
7072
{{ $resized := $image.Resize "123x456 webp" }}
7173
Resized RelPermalink: {{ $resized.RelPermalink }}|
7274
`
75+
tempDir := t.TempDir()
7376

74-
b, err := hugolib.TestE(t, files)
75-
77+
b, err := hugolib.TestE(t, files, hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) {
78+
cfg.NeedsOsFS = true
79+
cfg.WorkingDir = tempDir
80+
}))
7681
b.Assert(err, qt.IsNotNil)
82+
83+
if runtime.GOOS != "windows" {
84+
// Make sure the full image filename is in the error message.
85+
filename := filepath.Join(tempDir, "assets/invalid.webp")
86+
b.Assert(err.Error(), qt.Contains, filename)
87+
}
7788
}
7889

7990
// This test isn't great, but we have golden tests to verify the output itself.

‎resources/image.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828

2929
"github.com/gohugoio/hugo/cache/filecache"
3030
"github.com/gohugoio/hugo/common/hashing"
31-
"github.com/gohugoio/hugo/common/herrors"
3231
"github.com/gohugoio/hugo/common/paths"
3332

3433
"github.com/disintegration/gift"
@@ -212,7 +211,6 @@ func (i *imageResource) Process(spec string) (images.ImageResource, error) {
212211
// filter and returns the transformed image. If one of width or height is 0, the image aspect
213212
// ratio is preserved.
214213
func (i *imageResource) Resize(spec string) (images.ImageResource, error) {
215-
defer herrors.Recover()
216214
return i.processActionSpec(images.ActionResize, spec)
217215
}
218216

@@ -291,7 +289,14 @@ func (i *imageResource) Filter(filters ...any) (images.ImageResource, error) {
291289

292290
func (i *imageResource) processActionSpec(action, spec string) (images.ImageResource, error) {
293291
options := append([]string{action}, strings.Fields(strings.ToLower(spec))...)
294-
return i.processOptions(options)
292+
ir, err := i.processOptions(options)
293+
if err != nil {
294+
if sourcePath := i.sourcePath(); sourcePath != "" {
295+
err = fmt.Errorf("failed to %s image %q: %w", action, sourcePath, err)
296+
}
297+
return nil, err
298+
}
299+
return ir, nil
295300
}
296301

297302
func (i *imageResource) processOptions(options []string) (images.ImageResource, error) {

‎resources/resource.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ type baseResourceInternal interface {
242242
resource.Source
243243
resource.NameNormalizedProvider
244244

245+
sourcePath() string
245246
fileInfo
246247
mediaTypeAssigner
247248
targetPather

0 commit comments

Comments
 (0)