From e8577771c3275d16b7d1b31cca5e9fd3ea517416 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Thu, 26 Feb 2026 11:08:08 -0800 Subject: [PATCH] resources: Improve getImageOps error message Closes #14571 --- resources/resources_integration_test.go | 2 +- resources/transform.go | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) 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 } -- 2.39.5