Skip to content

Commit

Permalink
resources: Improve error message on .Resize etc. on SVGs
Browse files Browse the repository at this point in the history
Fixes #9875
  • Loading branch information
bep committed May 25, 2022
1 parent 3854a6f commit bb232a3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
27 changes: 27 additions & 0 deletions resources/integration_test.go
Expand Up @@ -17,11 +17,13 @@ import (
"strings"
"testing"

qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/hugolib"
)

// Issue 8931
func TestImageCache(t *testing.T) {
t.Parallel()

files := `
-- config.toml --
Expand Down Expand Up @@ -67,3 +69,28 @@ bmp: {{ $bmp.RelPermalink }}|{{ $bmp.MediaType }}|
assertImages()

}

func TestSVGError(t *testing.T) {
t.Parallel()

files := `
-- config.toml --
-- assets/circle.svg --
<svg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>
-- layouts/index.html --
{{ $svg := resources.Get "circle.svg" }}
Width: {{ $svg.Width }}
`

b, err := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
NeedsOsFS: true,
Running: true,
}).BuildE()

b.Assert(err, qt.IsNotNil)
b.Assert(err.Error(), qt.Contains, `error calling Width: this method is only available for raster images. To determine if an image is SVG, you can do {{ if eq .MediaType.SubType "svg" }}{{ end }}`)

}
6 changes: 5 additions & 1 deletion resources/transform.go
Expand Up @@ -297,7 +297,11 @@ func (r *resourceAdapter) DecodeImage() (image.Image, error) {
func (r *resourceAdapter) getImageOps() images.ImageResourceOps {
img, ok := r.target.(images.ImageResourceOps)
if !ok {
panic(fmt.Sprintf("%T is not an image", r.target))
if r.MediaType().SubType == "svg" {
panic("this method is only available for raster images. To determine if an image is SVG, you can do {{ if eq .MediaType.SubType \"svg\" }}{{ end }}")
}
fmt.Println(r.MediaType().SubType)
panic("this method is only available for image resources")
}
r.init(false, false)
return img
Expand Down

0 comments on commit bb232a3

Please sign in to comment.