Skip to content

Commit

Permalink
Update GetImageBlob/GetImagesBlob to return error as second element (#…
Browse files Browse the repository at this point in the history
…321)

Update GetImageBlob/GetImagesBlob to return error as second element
  • Loading branch information
justinfx committed Jun 2, 2024
1 parent c7b3ce4 commit fb2c7f3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 147 deletions.
13 changes: 11 additions & 2 deletions imagick/magick_wand_exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

1 comment on commit fb2c7f3

@GwynethLlewelyn
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AH!... I was going to file an issue about this 😃

Good, good. I was just surprised why some rock-solid code that has been used for 4 years or so suddenly failed to compile... my language server (well, gopls) still believes that GetImageBlob() just returns one parameter and gives an error. But the Go compiler is not fooled as easily, and correctly accepts the 2-return-parameters GetImageBlob().

While I was frustrated after hours of trying to understand what was going on — even going as far as upgrading a live, production Ubuntu server (!) just to compile ImageMagick 7 from scratch again (!!) — at the end of the day, you're right, you should return an error, just for the sake of consistency with the rest of the functions...

Please sign in to comment.