Skip to content
Browse files

Add backend error translations and properly return them to client

  • Loading branch information...
1 parent 819313b commit f5e0c89052516b1208e01d4ad1ac554ec9af1528 @artem-mindrov artem-mindrov committed Jan 24, 2014
View
8 app/controllers/s3_multipart/uploads_controller.rb
@@ -10,7 +10,7 @@ def create
response = {error: e.message}
rescue => e
logger.error "EXC: #{e.message}"
- response = {error: 'There was an error initiating the upload'}
+ response = { error: t("s3_multipart.errors.create") }
ensure
render :json => response
end
@@ -29,7 +29,7 @@ def sign_batch
response = Upload.sign_batch(params)
rescue => e
logger.error "EXC: #{e.message}"
- response = {error: 'There was an error in processing your upload'}
+ response = {error: t("s3_multipart.errors.update")}
ensure
render :json => response
end
@@ -40,7 +40,7 @@ def sign_part
response = Upload.sign_part(params)
rescue => e
logger.error "EXC: #{e.message}"
- response = {error: 'There was an error in processing your upload'}
+ response = {error: t("s3_multipart.errors.update")}
ensure
render :json => response
end
@@ -54,7 +54,7 @@ def complete_upload
upload.execute_callback(:complete, session)
rescue => e
logger.error "EXC: #{e.message}"
- response = {error: 'There was an error completing the upload'}
+ response = {error: t("s3_multipart.errors.complete")}
ensure
render :json => response
end
View
12 app/models/s3_multipart/upload.rb
@@ -1,6 +1,7 @@
module S3Multipart
class Upload < ::ActiveRecord::Base
extend S3Multipart::TransferHelpers
+ include ActionView::Helpers::NumberHelper
before_create :validate_file_type, :validate_file_size
@@ -27,8 +28,13 @@ def validate_file_size
limits = deserialize(self.uploader).size_limits
if limits.present?
- raise FileSizeError, "File size is too small" if limits.key?(:min) && limits[:min] > size
- raise FileSizeError, "File size is too large" if limits.key?(:max) && limits[:max] < size
+ if limits.key?(:min) && limits[:min] > size
+ raise FileSizeError, I18n.t("s3_multipart.errors.limits.min", min: number_to_human_size(limits[:min]))
+ end
+
+ if limits.key?(:max) && limits[:max] < size
+ raise FileSizeError, I18n.t("s3_multipart.errors.limits.max", max: number_to_human_size(limits[:max]))
+ end
end
end
@@ -37,7 +43,7 @@ def validate_file_type
types = deserialize(self.uploader).file_types
unless types.blank? || types.include?(ext)
- raise FileTypeError, "File type not supported"
+ raise FileTypeError, I18n.t("s3_multipart.errors.types", types: upload.deserialize(upload.uploader).file_types.join(", "))
end
end
View
10 config/locales/s3_multipart.en.yml
@@ -0,0 +1,10 @@
+en:
+ s3_multipart:
+ errors:
+ limits:
+ min: "Please choose a larger file (min %{min})"
+ max: "Please choose a smaller file (max %{max})"
+ types: "Please choose a different format (allowed types: %{types})"
+ create: "There was an error initiating the upload"
+ update: "There was an error processing the upload"
+ complete: "There was an error completing the upload"
View
2 vendor/assets/javascripts/s3_multipart/lib.js
@@ -227,7 +227,7 @@ S3MP.prototype.deliverRequest = function(xhr, body, cb) {
if (response.error) {
return self.onError({
name: "ServerResponse",
- message: "The server responded with an error"
+ message: response.error
});
}
cb(response);

0 comments on commit f5e0c89

Please sign in to comment.
Something went wrong with that request. Please try again.