Languages

Fsharp
ActionScript
xBase
Clean
GPSS
PureBasic
Sieve
Erlang
JOVIAL
Mercury
Linda
DataFlex
PostScript
FoxPro2
VFP
Cobol
Prolog
Jython
Awk
VisualBasic
JavaScript
Matlab
ASP
Haskell
Csharp
D
Smalltalk
Nemerle
Pixilang
Java
SQL
Python
ObjectPascal
Ruby
Perl
Pascal
Assembler
PHP
C
Functions  Add function  Users  Registration  Enter   About  ASCII Table  Our helpers

Ruby-on-Rails


1 Error Checking and Debugging with Ruby on Rails

2 15.21 Documenting Your Web Site

3 15.22 Unit Testing Your Web Site

4 15.23 Using breakpoint in Your Web Application

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> <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.


1 2 3 4
Bacterial Charity Work Thwarts Medical Treatments

Cover image of the September 2, 2010 issue of the journal Nature

Drug resistant bacteria are a problem in many environments, especially healthcare institutions. While the ways in which these cells become resistant are understood at the cellular level, until now, the bacteria's survival strategies at the population level remained unclear.

A new study by James Collins and colleagues at Boston University and the Wyss Institute for Biologically Inspired Engineering at Harvard University reveals that a surprisingly small percentage of bacteria actually ...

More at http://www.nsf.gov/news/news_summ.jsp?cntn_id=117596&WT.mc_id=USNSF_51&WT.mc_ev=click


This is an NSF News item.

PycckaR
BepcuR


Articles
Articles


Library
Library


Downloads
Downloads

Google Chrome Golf 6
 © Internet, books, teachers and Rudevich Alexander brains.