Commit 9cdc06e4 by Edward Holets

Added exception handling to paperclip:refresh:thumbnails rake task.

https://github.com/thoughtbot/paperclip/issues/760
parent 1da9e4d6
......@@ -16,6 +16,10 @@ module Paperclip
klass.attachment_definitions.keys
end
end
def self.log_error(error)
puts error
end
end
end
......@@ -31,10 +35,16 @@ namespace :paperclip do
styles = (ENV['STYLES'] || ENV['styles'] || '').split(',').map(&:to_sym)
names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
instance.send(name).reprocess!(*styles)
attachment = instance.send(name)
begin
attachment.reprocess!(*styles)
rescue Exception => e
Paperclip::Task.log_error("exception while processing #{klass} ID #{instance.id}:")
Paperclip::Task.log_error(" " + e.message + "\n")
end
unless instance.errors.blank?
puts "errors while processing #{klass} ID #{instance.id}:"
puts " " + instance.errors.full_messages.join("\n ") + "\n"
Paperclip::Task.log_error("errors while processing #{klass} ID #{instance.id}:")
Paperclip::Task.log_error(" " + instance.errors.full_messages.join("\n ") + "\n")
end
end
end
......
require './test/helper'
require 'rake'
load './lib/tasks/paperclip.rake'
class RakeTest < Test::Unit::TestCase
context "calling `rake paperclip:refresh:thumbnails`" do
setup do
rebuild_model
Paperclip::Task.stubs(:obtain_class).returns('Dummy')
@bogus_instance = Dummy.new
@bogus_instance.id = 'some_id'
@bogus_instance.avatar.stubs(:reprocess!)
@valid_instance = Dummy.new
@valid_instance.avatar.stubs(:reprocess!)
Paperclip::Task.stubs(:log_error)
Paperclip.stubs(:each_instance_with_attachment).multiple_yields @bogus_instance, @valid_instance
end
context "when there is an exception in reprocess!" do
setup do
@bogus_instance.avatar.stubs(:reprocess!).raises
end
should "catch the exception" do
assert_nothing_raised do
::Rake::Task['paperclip:refresh:thumbnails'].execute
end
end
should "continue to the next instance" do
@valid_instance.avatar.expects(:reprocess!)
::Rake::Task['paperclip:refresh:thumbnails'].execute
end
should "print the exception" do
exception_msg = 'Some Exception'
@bogus_instance.avatar.stubs(:reprocess!).raises(exception_msg)
Paperclip::Task.expects(:log_error).with do |str|
str.match exception_msg
end
::Rake::Task['paperclip:refresh:thumbnails'].execute
end
should "print the class name" do
Paperclip::Task.expects(:log_error).with do |str|
str.match 'Dummy'
end
::Rake::Task['paperclip:refresh:thumbnails'].execute
end
should "print the instance ID" do
Paperclip::Task.expects(:log_error).with do |str|
str.match "ID #{@bogus_instance.id}"
end
::Rake::Task['paperclip:refresh:thumbnails'].execute
end
end
context "when there is an error in reprocess!" do
setup do
@errors = mock('errors')
@errors.stubs(:full_messages).returns([''])
@errors.stubs(:blank?).returns(false)
@bogus_instance.stubs(:errors).returns(@errors)
end
should "continue to the next instance" do
@valid_instance.avatar.expects(:reprocess!)
::Rake::Task['paperclip:refresh:thumbnails'].execute
end
should "print the error" do
error_msg = 'Some Error'
@errors.stubs(:full_messages).returns([error_msg])
Paperclip::Task.expects(:log_error).with do |str|
str.match error_msg
end
::Rake::Task['paperclip:refresh:thumbnails'].execute
end
should "print the class name" do
Paperclip::Task.expects(:log_error).with do |str|
str.match 'Dummy'
end
::Rake::Task['paperclip:refresh:thumbnails'].execute
end
should "print the instance ID" do
Paperclip::Task.expects(:log_error).with do |str|
str.match "ID #{@bogus_instance.id}"
end
::Rake::Task['paperclip:refresh:thumbnails'].execute
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment