Debug MiniMagick calls in your Rails app

Posted . Visible to the public.

Most of our applications use CarrierWave for file uploads. CarrierWave has an integrated processing mechanism for different file versions with support for ImageMagick through CarrierWave::MiniMagick (which requires the mini_magick gem). In case your processing runs into an error, CarrierWave will just swallow it and rethrow an error with a very generic message like Processing failed. Maybe it is not an image? which does not help you finding out what the actual problem is. CarrierWave probably does this for security purposes, but does not even write the real error to logs (which is PITA).

You can, however, get more information in your development environment by setting MiniMagick.logger.level = Logger::DEBUG or MiniMagick.debug = true (deprecated) before the processing happens. This will print all ImageMagick calls to stdout and you can get an idea where the problem is coming from.

D, [2023-11-06T12:02:29.443083 #74] DEBUG -- : [0.03s] convert /repo/tmp/1699268548-841333865692559-0001-8387/first_generated_preview_image/sample.jpg -auto-orient -resize 1280x720> /tmp/image_processing20231106-74-zks7q9.jpg
D, [2023-11-06T12:02:29.644009 #74] DEBUG -- : [0.02s] convert /repo/tmp/1699268548-841333865692559-0001-8387/second_generated_preview_image/sample.jpg -auto-orient -resize 1280x720> /tmp/image_processing20231106-74-17enk2.jpg
D, [2023-11-06T12:02:29.831443 #74] DEBUG -- : [0.02s] convert /repo/tmp/1699268548-841333865692559-0001-8387/third_generated_preview_image/sample.jpg -auto-orient -resize 1280x720> /tmp/image_processing20231106-74-jjmtyc.jpg

Sometimes, this will be good enough to see what might cause the error. If it doesn't exactly help you, you will have to edit the carrierwave/processing/mini_magick.rb code within the CarrierWave gem and place a debugger statement (byebug) within the rescue block to be able to spy on the error object.

Dominic Beger
Last edit
Dominic Beger
License
Source code in this card is licensed under the MIT License.
Posted by Dominic Beger to makandra dev (2023-11-06 10:51)