Ruby on Rails exception notification through mail

Posted by jeet
on Thursday, June 19

So you want to get notified when you get exception on production application environment , lets do in three easy steps

1: create a action mailer model

class Mymailer '
    @subject            = "[Error] exception in #{env['REQUEST_URI']}"
    @sent_on            = time
    @body["exception"]  = exception 
    @body["trace"]      = trace  
    @body["session"]    = session 
    @body["params"]     = params 
    @body["env"]        = env  
    content_type("text/html")  
  end

end

2: define this method in you application.rb file

def log_error(exception)
    super(exception)
    begin
      if ENV['RAILS_ENV'] == 'production'
        Notifier.deliver_error_page(exception,
          clean_backtrace(exception),
          @session.instance_variable_get("@data"),
          @params,
          @request.env)
      end
    rescue => e
      logger.error(e)
    end
  end

3: create a view file  view/mymailer/error_mail.html.erb


now you have access to these files , you can format your mail as you feel like

@body["exception"]  # exception
@body["trace"]      # trace   
@body["session"]    # session
@body["params"]     # params
@body["env"]        # env

now you have working exception handling system

if face any problem make a comment here
Comments

Leave a response