Commit edea587f by Dan Collis-Puro

Merge branch 'rake-task-exception-handling' of…

Merge branch 'rake-task-exception-handling' of git://github.com/holetse/paperclip into holetse-rake-task-exception-handling
parents bc0758ca 58e2c680
...@@ -24,6 +24,10 @@ module Paperclip ...@@ -24,6 +24,10 @@ module Paperclip
attachment_names attachment_names
end end
end end
def self.log_error(error)
$stderr.puts error
end
end end
end end
...@@ -39,10 +43,16 @@ namespace :paperclip do ...@@ -39,10 +43,16 @@ namespace :paperclip do
styles = (ENV['STYLES'] || ENV['styles'] || '').split(',').map(&:to_sym) styles = (ENV['STYLES'] || ENV['styles'] || '').split(',').map(&:to_sym)
names.each do |name| names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance| 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? unless instance.errors.blank?
puts "errors while processing #{klass} ID #{instance.id}:" Paperclip::Task.log_error("errors while processing #{klass} ID #{instance.id}:")
puts " " + instance.errors.full_messages.join("\n ") + "\n" Paperclip::Task.log_error(" " + instance.errors.full_messages.join("\n ") + "\n")
end end
end 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
context "Paperclip::Task.log_error method" do
should "print its argument to STDERR" do
msg = 'Some Message'
$stderr.expects(:puts).with(msg)
Paperclip::Task.log_error(msg)
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