diff --git a/imagick/magick_wand_exception.go b/imagick/magick_wand_exception.go index e9961b4..5820e28 100644 --- a/imagick/magick_wand_exception.go +++ b/imagick/magick_wand_exception.go @@ -29,13 +29,22 @@ func (mw *MagickWand) clearException() bool { return 1 == C.int(C.MagickClearException(mw.mw)) } -// Returns the kind, reason and description of any error that occurs when using other methods in this API +// GetLastError returns the kind, reason and description of any error that occurs when using other methods in this API. +// The exception is cleared after this call. func (mw *MagickWand) GetLastError() error { + return mw.getLastError(true) +} + +// Returns the kind, reason and description of any error that occurs when using other methods in this API. +// Clears the exception, if clear is true. +func (mw *MagickWand) getLastError(clear bool) error { var et C.ExceptionType csdescription := C.MagickGetException(mw.mw, &et) defer relinquishMemory(unsafe.Pointer(csdescription)) if ExceptionType(et) != EXCEPTION_UNDEFINED { - mw.clearException() + if clear { + mw.clearException() + } return &MagickWandException{ExceptionType(C.int(et)), C.GoString(csdescription)} } runtime.KeepAlive(mw) diff --git a/imagick/magick_wand_image.go b/imagick/magick_wand_image.go index ced987e..963b7b4 100644 --- a/imagick/magick_wand_image.go +++ b/imagick/magick_wand_image.go @@ -937,16 +937,16 @@ func (mw *MagickWand) GetImageBackgroundColor() (bgColor *PixelWand, err error) // position in the image sequence. Use SetImageFormat() to set the format to // write to the blob (GIF, JPEG, PNG, etc.). Utilize ResetIterator() to ensure // the write is from the beginning of the image sequence. -func (mw *MagickWand) GetImageBlob() []byte { +func (mw *MagickWand) GetImageBlob() ([]byte, error) { clen := C.size_t(0) csblob := C.MagickGetImageBlob(mw.mw, &clen) defer relinquishMemory(unsafe.Pointer(csblob)) if err := mw.GetLastError(); err != nil { - return nil + return nil, err } ret := C.GoBytes(unsafe.Pointer(csblob), C.int(clen)) runtime.KeepAlive(mw) - return ret + return ret, nil } // GetImagesBlob Implements direct to memory image formats. It returns the image sequence @@ -955,15 +955,16 @@ func (mw *MagickWand) GetImageBlob() []byte { // format, use SetImageFormat(). Note, some image formats do not permit // multiple images to the same image stream (e.g. JPEG). in this instance, // just the first image of the sequence is returned as a blob. -func (mw *MagickWand) GetImagesBlob() []byte { +func (mw *MagickWand) GetImagesBlob() ([]byte, error) { clen := C.size_t(0) csblob := C.MagickGetImagesBlob(mw.mw, &clen) defer relinquishMemory(unsafe.Pointer(csblob)) if err := mw.GetLastError(); err != nil { - return nil + return nil, err } + ret := C.GoBytes(unsafe.Pointer(csblob), C.int(clen)) runtime.KeepAlive(mw) - return C.GoBytes(unsafe.Pointer(csblob), C.int(clen)) + return ret, nil } // GetImageBluePrimary Returns the chromaticy blue primary point for the image. diff --git a/imagick/magick_wand_test.go b/imagick/magick_wand_test.go index 5779dcd..082836b 100644 --- a/imagick/magick_wand_test.go +++ b/imagick/magick_wand_test.go @@ -234,7 +234,7 @@ func TestReadImageBlob(t *testing.T) { mw := NewMagickWand() // Try to read an empty wand - blob := mw.GetImageBlob() + blob, _ := mw.GetImageBlob() if blob != nil { t.Fatal("Expected nil blob on invalid wand") } @@ -246,7 +246,10 @@ func TestReadImageBlob(t *testing.T) { } mw.ReadImage(`logo:`) - blob = mw.GetImageBlob() + blob, err := mw.GetImageBlob() + if err != nil { + t.Fatal(err) + } // Read a valid blob if err := mw.ReadImageBlob(blob); err != nil {