Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Error Checking and Debugging with Ruby on ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Error Checking and Debugging with Ruby on Rails
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-04-26

    Table of Contents:
  • Error Checking and Debugging with Ruby on Rails
  • 15.21 Documenting Your Web Site
  • 15.22 Unit Testing Your Web Site
  • 15.23 Using breakpoint in Your Web Application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Error Checking and Debugging with Ruby on Rails


    (Page 1 of 4 )

    In this conclusion to a six-part series covering web development and Ruby on Rails, you'll learn how to send error messages to your email and more. This article is excerpted from chapter 15 of the Ruby Cookbook, written by Lucas Carlson and Leonard Richardson (O'Reilly, 2006; ISBN: 0596523696). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    15.20 Automatically Sending Error Messages to Your Email

    Problem

    You want to receive a descriptive email message every time one of your users encounters an application error.

    Solution

    Any errors that occur while running your application are sent to the ActionController::Base#log_error method. If you've set up a mailer (as shown in Recipe 15.19) you can override this method and have it send mail to you. Your code should look something like this:

      class ApplicationController < ActionController::Base

      private
       
    def log_error(exception)
          
    super
          
    Notification.deliver_error_message(exception,
            
    clean_backtrace(exception),
            
    session.instance_variable_get("@data"),
            
    params,
             request.env
           )
        end
      end

    That code rounds up a wide variety of information about the state of the Rails request at the time of the failure. It captures the exception object, the corresponding backtrace, the session data, the CGI request parameters, and the values of all environment variables.

    The overridden log_error calls Notification.deliver_error_messsage, which assumes you've created a mailer called "Notification", and defined the method Notification.error_message. Here's the implementation:

      class Notification < ActionMailer::Base
        def error_message(exception, trace, session, params, env, sent_on = Time.now)

          @recipients    =
    'me@mydomain.com'
          @from          = 'error@mydomain.com'
          @subject       = "Error message:
    #{env['REQUEST_URI']}"
          @sent_on       = sent_on
          @body = {
           
    :exception => exception,
            :trace => trace,
            :session => session,
            :params => params,
            :env => env
         
    }
        end
      end

    The template for this email looks like this:

      <!-- app/views/notification/error_message.rhtml -->

      Time: <%= Time.now %>
      Message: <%= @exception.message %>
      Location: <%= @env['REQUEST_URI'] %> 
      Action: <%= @params.delete('action') %> </td> </tr>
      Controller: <%= @params.delete('controller') %> </td> </tr>
      Query: <%= @env['QUERY_STRING'] %> </td> </tr>
      Method: <%= @env['REQUEST_METHOD'] %> </td> </tr>
      SSL: <%= @env['SERVER_PORT'].to_i == 443 ? "true" : "false" %>
      Agent: <%= @env['HTTP_USER_AGENT'] %>

      Backtrace
      <%= @trace.to_a.join("</p>\n<p>") %>

      Params
      <% @params.each do |key, val| -%>
      * <%= key %>: <%= val.to_yaml %>
      <% end -%>

      Session
      <% @session.each do |key, val| -%>
      * <%= key %>: <%= val.to_yaml %>
      <% end -%>

      Environment
      <% @env.each do |key, val| -%>
      * <%= key %>: <%= val %>
      <% end -%>

    Discussion

    ActionController::Base#log_error gives you the flexibility to handle errors however you like. This is especially useful if your Rails application is hosted on a machine to which you have limited access: you can have errors sent to you, instead of written to a file you might not be able to see. Or you might prefer to record the errors in a database, so that you can look for patterns.

    The method ApplicationController#log_error is declared private to avoid confusion. If it weren't private, all of the controllers would think they had a log_error action defined. Users would be able to visit /<controller>/log_error and get Rails to act strangely.

    See Also

    • Recipe 15.19, "Sending Mail with Rails"

    More Ruby-on-Rails Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Ruby Cookbook," published by O'Reilly. We...
     

    Buy this book now. This article is excerpted from chapter 15 of the Ruby Cookbook, written by Lucas Carlson and Leonard Richardson (O'Reilly, 2006; ISBN: 0596523696). Check it out today at your favorite bookstore. Buy this book now.

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek