Jeevan Dongre
Jeevan Dongre

Reputation: 4649

exception handling in ruby on rails

I am a newbie to ruby on rails and developing some email apps, which uses AWS SES to send emails. I am uploading a csv file which contains only email address and an email will be sent to those email address.

Its a very basic app, which my app fails to send an email due to some reasons the app automatically stops sending emails. But I has to keep sending emails to the remaining email address.

How do I handle the exception. I have used ActionMailer.

Kindly Help me

Upvotes: 0

Views: 538

Answers (2)

Daniel Sagayaraj
Daniel Sagayaraj

Reputation: 317

If you want to know about the exception,use

begin
 #some code here
rescue =>ex
 Rails.logger.error "#{ex.class.name} :  #{ex.message}"
end

ps: You can also use rescue Exception =>ex .But don't use it until needed.Since it will catch all minor exceptions like 'NoMemoryError' which we don't want.Use the first one,it will catch only the Standard errors.

Upvotes: 0

zed_0xff
zed_0xff

Reputation: 33217

def send_all_emails
  @emails.each do |email|
    send_one_mail email
  end
end

def send_one_mail email
  # your actual email sending code here
rescue
  # this will log error to Rails log, but will not halt the whole app
  Rails.logger.error $!
end

Upvotes: 2

Related Questions