resources: Improve getImageOps error message

Closes #14571
This commit is contained in:
Joe Mooring
2026-02-26 11:08:08 -08:00
committed by Bjørn Erik Pedersen
parent 44dc3847f4
commit e8577771c3
2 changed files with 13 additions and 5 deletions
+1 -1
View File
@@ -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.
+12 -4
View File
@@ -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
}