Skip to content

Commit

Permalink
Passing IM error up the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
dooman87 committed Aug 13, 2024
1 parent bb3cf17 commit 2d6f5f3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
5 changes: 3 additions & 2 deletions img/processor/imagemagick.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os/exec"
"strconv"
"strings"
)

type ImageMagick struct {
Expand Down Expand Up @@ -266,7 +267,7 @@ func (p *ImageMagick) execImagemagick(in *bytes.Reader, args []string, imgId str
if err != nil {
img.Log.Printf("[%s] Error executing convert command: %s\n", imgId, err.Error())
img.Log.Printf("[%s] ERROR: %s\n", imgId, cmderr.String())
return nil, err
return nil, fmt.Errorf("Error executing convert command: %w\nStderr: [%s]", err, strings.TrimSpace(cmderr.String()))
}

return out.Bytes(), nil
Expand Down Expand Up @@ -308,7 +309,7 @@ func (p *ImageMagick) LoadImageInfo(src *img.Image) (*img.Info, error) {
if err != nil {
img.Log.Printf("[%s] Error executing identify command: %s\n", err.Error(), imgId)
img.Log.Printf("[%s] ERROR: %s\n", cmderr.String(), imgId)
return nil, err
return nil, fmt.Errorf("Error executing identify command: %w\nStderr: [%s]", err, strings.TrimSpace(cmderr.String()))
}

imageInfo := &img.Info{
Expand Down
54 changes: 54 additions & 0 deletions img/processor/imagemagick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,3 +692,57 @@ func testImages(t *testing.T, fn transform, files []*testTransformation) {
fmt.Printf("%60s | %10d | %10d | %.2f\n", r.file, r.optSize, r.origSize, 1.0-(float32(r.optSize)/float32(r.origSize)))
}
}

func TestOptimise_ErrorIdentify(t *testing.T) {
_, err := proc.Optimise(&img.TransformationConfig{
Src: &img.Image{
Id: "",
Data: []byte("This is not an image!"),
MimeType: "image/jpeg",
},
})

if err == nil {
t.Error("expected error but got none")
}

expectedError := "Error executing identify command: exit status 1\nStderr: [identify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/746.]"
if err.Error() != expectedError {
t.Errorf("expected error %s, but got %s", expectedError, err.Error())
}
}

func TestOptimise_ErrorConvert(t *testing.T) {
f := fmt.Sprintf("%s/%s", "./test_files/transformations", "medium-jpeg.jpg")

orig, err := ioutil.ReadFile(f)
if err != nil {
t.Errorf("Can't read file %s: %+v", f, err)
}

proc.GetAdditionalArgs = func(op string, image []byte, source *img.Info, target *img.Info) []string {
return []string{"-i_dont_know", "this"}
}

_, err = proc.Resize(&img.TransformationConfig{
Src: &img.Image{
Id: "",
Data: orig,
MimeType: "image/jpeg",
},
Config: &img.ResizeConfig{
Size: "300x300",
},
})

proc.GetAdditionalArgs = nil

if err == nil {
t.Error("expected error but got none")
}

expectedError := "Error executing convert command: exit status 1\nStderr: [convert: unrecognized option `-i_dont_know' @ error/convert.c/ConvertImageCommand/1965.]"
if err.Error() != expectedError {
t.Errorf("expected error %s, but got %s", expectedError, err.Error())
}
}

0 comments on commit 2d6f5f3

Please sign in to comment.