Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e857777

Browse files
jmooringbep
authored andcommitted
resources: Improve getImageOps error message
Closes #14571
1 parent 44dc384 commit e857777

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

resources/resources_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Width: {{ $svg.Width }}
9797
}).BuildE()
9898

9999
b.Assert(err, qt.IsNotNil)
100-
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 }}`)
100+
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`)
101101
}
102102

103103
// Issue 10255.

resources/transform.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,20 @@ func (r resourceAdapter) WithResourceMeta(mp resource.ResourceMetaProvider) reso
395395
func (r *resourceAdapter) getImageOps() images.ImageResourceOps {
396396
img, ok := r.target.(images.ImageResourceOps)
397397
if !ok {
398-
if r.MediaType().SubType == "svg" {
399-
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 }}")
400-
}
401-
panic("this method is only available for image resources")
398+
instructions := "use reflect.IsImageResource, " +
399+
"reflect.IsImageResourceProcessable, or " +
400+
"reflect.IsImageResourceWithMeta to check if the resource " +
401+
"supports this method before calling it"
402+
msg := fmt.Sprintf(
403+
"resource %q of media type %q does not support this method: %s",
404+
r.Name(),
405+
r.MediaType(),
406+
instructions,
407+
)
408+
panic(msg)
402409
}
403410
r.init(false, false)
411+
404412
return img
405413
}
406414

0 commit comments

Comments
 (0)