Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- I18n all error messages.

- Add test for all but mime_types
  • Loading branch information...
commit c564f2aa96cb3656d9ee1d1d7e5a23f7f85bec62 1 parent 5585a1f
Bart Teeuwisse authored
View
6 lib/carrierwave/locale/en.yml
@@ -2,4 +2,8 @@ en:
errors:
messages:
carrierwave_processing_error: failed to be processed
- carrierwave_integrity_error: is not of an allowed file type
+ carrierwave_integrity_error: is not of an allowed file type
+ extension_white_list_error: "You are not allowed to upload %{extension} files, allowed types: %{allowed_types}"
+ rmagick_processing_error: "Failed to manipulate with rmagick, maybe it is not an image? Original Error: %{e}"
+ mime_types_processing_error: "Failed to process file with MIME::Types, maybe not valid content-type? Original Error: %{e}"
+ mini_magick_processing_error: "Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: %{e}"
View
2  lib/carrierwave/processing/mime_types.rb
@@ -51,7 +51,7 @@ def set_content_type(override=false)
end
end
rescue ::MIME::InvalidContentType => e
- raise CarrierWave::ProcessingError.new("Failed to process file with MIME::Types, maybe not valid content-type? Original Error: #{e}")
+ raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.mime_types_processing_error", :e => e)
end
end # MimeTypes
View
2  lib/carrierwave/processing/mini_magick.rb
@@ -245,7 +245,7 @@ def manipulate!
image.write(current_path)
::MiniMagick::Image.open(current_path)
rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
- raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}")
+ raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e)
end
end # MiniMagick
View
2  lib/carrierwave/processing/rmagick.rb
@@ -266,7 +266,7 @@ def manipulate!(options={})
end
destroy_image(frames)
rescue ::Magick::ImageMagickError => e
- raise CarrierWave::ProcessingError.new("Failed to manipulate with rmagick, maybe it is not an image? Original Error: #{e}")
+ raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.rmagick_processing_error", :e => e)
end
private
View
2  lib/carrierwave/uploader/extension_whitelist.rb
@@ -40,7 +40,7 @@ def extension_white_list; end
def check_whitelist!(new_file)
extension = new_file.extension.to_s
if extension_white_list and not extension_white_list.detect { |item| extension =~ /\A#{item}\z/i }
- raise CarrierWave::IntegrityError, "You are not allowed to upload #{new_file.extension.inspect} files, allowed types: #{extension_white_list.inspect}"
+ raise CarrierWave::IntegrityError, I18n.translate(:"errors.messages.extension_white_list_error", :extension => new_file.extension.inspect, :allowed_types => extension_white_list.inspect)
end
end
View
4 spec/mount_spec.rb
@@ -405,7 +405,9 @@ def extension_white_list
end
end
@instance.image = stub_file('test.jpg')
- @instance.image_integrity_error.should be_an_instance_of(CarrierWave::IntegrityError)
+ e = @instance.image_integrity_error
+ e.should be_an_instance_of(CarrierWave::IntegrityError)
+ e.message.grep(/^You are not allowed to upload/).should be_true
end
end
View
45 spec/orm/activerecord_spec.rb
@@ -97,7 +97,7 @@ class Event < ActiveRecord::Base; end # setup a basic AR class for testing
it "should return valid XML when to_xml is called when image is nil" do
@event[:image].should be_nil
- Hash.from_xml(@event.to_xml)["event#{$arclass}"].should == {"textfile"=>nil, "foo"=>nil}
+ Hash.from_xml(@event.to_xml)["event#{$arclass}"].should == {"textfile"=>nil, "id"=>nil, "foo"=>nil}
end
# FIXME to_xml should work like to_json
@@ -143,26 +143,52 @@ def extension_white_list
%w(txt)
end
end
+ end
+
+ it "should use I18n for integrity error messages" do
+ # Localize the error message to Dutch
+ change_locale_and_store_translations(:nl, :errors => {
+ :messages => {
+ :extension_white_list_error => "Het opladen van %{extension} bestanden is niet toe gestaan. Geaccepteerde types: %{allowed_types}"
+ }
+ }) do
+ # Assigning image triggers check_whitelist! and thus should be inside change_locale_and_store_translations
+ @event.image = stub_file('test.jpg')
+ @event.should_not be_valid
+ @event.valid?
+ @event.errors[:image].should == ['Het opladen van "jpg" bestanden is niet toe gestaan. Geaccepteerde types: ["txt"]']
+ end
+ end
+ end
+
+ context 'when validating processing' do
+ before do
+ @uploader.class_eval do
+ process :monkey
+ def monkey
+ raise CarrierWave::ProcessingError
+ end
+ end
@event.image = stub_file('test.jpg')
end
- it "should make the record invalid when an integrity error occurs" do
+ it "should make the record invalid when a processing error occurs" do
@event.should_not be_valid
end
- it "should use I18n for integrity error messages" do
+ it "should use I18n for processing errors without messages" do
@event.valid?
- @event.errors[:image].should == ['is not an allowed file type']
+ @event.errors[:image].should == ['failed to be processed']
change_locale_and_store_translations(:pt, :activerecord => {
:errors => {
:messages => {
- :carrierwave_integrity_error => 'tipo de imagem não permitido.'
+ :carrierwave_processing_error => 'falha ao processar imagem.'
}
}
}) do
@event.should_not be_valid
- @event.errors[:image].should == ['tipo de imagem não permitido.']
+ @event.errors[:image].should == ['falha ao processar imagem.']
end
end
end
@@ -182,9 +208,9 @@ def monkey
@event.should_not be_valid
end
- it "should use I18n for processing error messages" do
+ it "should use the error's messages for processing errors with messages" do
@event.valid?
- @event.errors[:image].should == ['failed to be processed']
+ @event.errors[:image].should == ['Ohh noez!']
change_locale_and_store_translations(:pt, :activerecord => {
:errors => {
@@ -194,11 +220,10 @@ def monkey
}
}) do
@event.should_not be_valid
- @event.errors[:image].should == ['falha ao processar imagem.']
+ @event.errors[:image].should == ['Ohh noez!']
end
end
end
-
end
describe '#save' do
View
12 spec/processing/mime_types_spec.rb
@@ -48,4 +48,16 @@
end
+ describe "test errors" do
+ context "invalid mime type" do
+ before do
+ @instance.file.content_type = nil
+ # TODO: somehow force a ::MIME::InvalidContentType error when set_content_type is called.
+ end
+
+ it "should raise a MIME::InvalidContentType error" do
+ # lambda {@instance.set_content_type}.should raise_exception(::MIME::InvalidContentType, /^Failed to process file with MIME::Types, maybe not valid content-type\? Original Error:/)
+ end
+ end
+ end
end
View
23 spec/processing/mini_magick_spec.rb
@@ -74,4 +74,27 @@
end
end
+ describe "test errors" do
+ context "invalid image file" do
+ before do
+ File.open(@instance.current_path, 'w') do |f|
+ f.puts "bogus"
+ end
+ end
+
+ it "should fail to process a non image file" do
+ lambda {@instance.resize_to_limit(200, 200)}.should raise_exception(CarrierWave::ProcessingError, /^Failed to manipulate with MiniMagick, maybe it is not an image\? Original Error:/)
+ end
+
+ it "should use I18n" do
+ change_locale_and_store_translations(:nl, :errors => {
+ :messages => {
+ :mini_magick_processing_error => "Kon bestand niet met MiniMagick bewerken, misschien is het geen beeld bestand? MiniMagick foutmelding: %{e}"
+ }
+ }) do
+ lambda {@instance.resize_to_limit(200, 200)}.should raise_exception(CarrierWave::ProcessingError, /^Kon bestand niet met MiniMagick bewerken, misschien is het geen beeld bestand\? MiniMagick foutmelding:/)
+ end
+ end
+ end
+ end
end
View
23 spec/processing/rmagick_spec.rb
@@ -76,5 +76,28 @@
end
end
+ describe "test errors" do
+ context "invalid image file" do
+ before do
+ File.open(@instance.current_path, 'w') do |f|
+ f.puts "bogus"
+ end
+ end
+
+ it "should fail to process a non image file" do
+ lambda {@instance.resize_to_limit(200, 200)}.should raise_exception(CarrierWave::ProcessingError, /^Failed to manipulate with rmagick, maybe it is not an image\? Original Error:/)
+ end
+
+ it "should use I18n" do
+ change_locale_and_store_translations(:nl, :errors => {
+ :messages => {
+ :rmagick_processing_error => "Kon bestand niet met rmagick bewerken, misschien is het geen beeld bestand? rmagick foutmelding: %{e}"
+ }
+ }) do
+ lambda {@instance.resize_to_limit(200, 200)}.should raise_exception(CarrierWave::ProcessingError, /^Kon bestand niet met rmagick bewerken, misschien is het geen beeld bestand\? rmagick foutmelding:/)
+ end
+ end
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.