How To Provide Users With Useful Cloudinary Exception Response in Rails?
Using Rails 4.2, we are allowing users to upload images to Cloudinary only using HTTP links. Our code in our model is:
upload = Cloudinary::Uploader.upload(self.picture_link.to_s)
This works well. We don't want to allow very large uploads, and it looks like Cloudinary is automatically restricting files to 10MB or below, which is fine for us.
On our development site, when a 10MB+ upload is attempted, Rails provides a useful error message. See attached PNG.
CloudinaryException in ItemsController#update
File size too large. Got 12984953. Maximum is 10485760.
Here is our question: What is the best way to have Rails provide an error message like this on our production website? On our production website, we are not showing errors:
config.consider_all_requests_local = false
It would be a security risk to set this to true, as that would expose all of our app's errors to the world.
We need a way only return this one particular exception to the user in some way.
We've looked at this RailsCast on handling exception responses: http://railscasts.com/episodes/53-handling-exceptions-revised?autoplay=true
... But we're not sure how we would return this to the user:
CloudinaryException (File size too large. Got 12984953. Maximum is 10485760.)
Any ideas?
thanks
-
Hi Alexander,
If you wish to keep the uploads being performed on the server-side, then you can use something like the following:
begin
upload = Cloudinary::Uploader.upload(self.picture_link.to_s)
rescue CloudinaryException => e
#Perform handling code, for example:
msg_to_display = e.message
endAlternatively, you might want to consider performing the uploads client-side, i.e., uploading images directly from the browser to Cloudinary. This way, you can use our jQuery library to validate the uploads on the client-side, before uploading the file to Cloudinary.
0
Post is closed for comments.
Comments
1 comment