Question

I am in the process of upgrading an old rails app and am upgrading Paperclip. I have a custom processor that uses Paperclip.run to run some manual convert statements in my processor. Occasionally a bad file slips through that imagemagick doesn't like, so we would catch the error and respond to the user.

The old way was as follows(This code fails):

begin
   Paperclip.run("convert", cmd)
rescue PaperclipCommandLineError
     raise PaperclipError, "There was an error processing the tiles for #{@basename}"   if whiny
end

Which used to catch the command error and raise a validation error. This no longer works because there is no longer a PaperclipCommandLineError.

Looking at the code it looks like Paperclip.run() uses https://github.com/thoughtbot/cocaine to handle it's commands. Looking at the cocaine documentation it looks like we could catch the exit code and recover that way with something like:

begin
  Cocaine::CommandLine.new("convert", cmd)
rescue Cocaine::ExitStatusError => e
  #error handling here
end

Will Paperclip.run() return a Cocaine::ExitStatusError if there is a problem? Or, since I am just using Paperclip.run() as a wrapper to run my command, should I use Cocaine::CommandLine instead? Thank you for your insight.

Was it helpful?

Solution

You are correct with Paperclip now using cocaine for commands now. However the call to use cocaine is internal so you can still use Paperclip.run.

The arguments to run are a little different:

def run(cmd, arguments = "", interpolation_values = {}, local_options = {})

so you might need to work on your cmd a little to get it in the correct format. You can see examples in the source though where it makes calls file and the various ImageMagick functions.

You should be able to trap your error with:

begin
   Paperclip.run(cmd)
rescue ArgumentError, Cocaine::CommandLineError
   raise Paperclip::Error.new("There was an error processing the tiles for #{@basename}") if @whiny
end

Note you also have to raise the Paperclip slightly differently now as PaperclipError no longer exists.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top