From: Joe Mooring Date: Thu, 26 Feb 2026 19:08:08 +0000 (-0800) Subject: resources: Improve getImageOps error message X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=e8577771c3275d16b7d1b31cca5e9fd3ea517416;p=brevno-suite%2Fhugo resources: Improve getImageOps error message Closes #14571 --- diff --git a/resources/resources_integration_test.go b/resources/resources_integration_test.go index 1cc870f80..8d0f13bb6 100644 --- a/resources/resources_integration_test.go +++ b/resources/resources_integration_test.go @@ -97,7 +97,7 @@ Width: {{ $svg.Width }} }).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 }}`) + b.Assert(err.Error(), qt.Contains, `error calling Width: resource "/circle.svg" of media type "image/svg+xml" does not support this method: use reflect.IsImageResource, reflect.IsImageResourceProcessable, or reflect.IsImageResourceWithMeta to check if the resource supports this method before calling it`) } // Issue 10255. diff --git a/resources/transform.go b/resources/transform.go index bcacf5471..169c50895 100644 --- a/resources/transform.go +++ b/resources/transform.go @@ -395,12 +395,20 @@ func (r resourceAdapter) WithResourceMeta(mp resource.ResourceMetaProvider) reso func (r *resourceAdapter) getImageOps() images.ImageResourceOps { img, ok := r.target.(images.ImageResourceOps) if !ok { - 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 }}") - } - panic("this method is only available for image resources") + instructions := "use reflect.IsImageResource, " + + "reflect.IsImageResourceProcessable, or " + + "reflect.IsImageResourceWithMeta to check if the resource " + + "supports this method before calling it" + msg := fmt.Sprintf( + "resource %q of media type %q does not support this method: %s", + r.Name(), + r.MediaType(), + instructions, + ) + panic(msg) } r.init(false, false) + return img }