small medium large xlarge


Update the application to send an e-mail to the system administrator, namely yourself, when there is an application failure such as the one we handled in Section 10.2, Iteration E2: Handling Errors, on page 133.

Suresh attempted:

Add method to mailer Notifier:

class Notifier < ActionMailer::Base
# existing lines here...
  def error_occured(error)
    @error = error
    mail :to => "", :subject => 'Depot App Error Incident'  ## replace your email id to receive mails
Create notifier template (in app/views/notifier) file “error_occured.text.erb”:

Hello Admin,

This is just to inform you that an exception was raised and rescued in the Depot app.

Error details:

<%= @error.message %>

Depot App
Modify the carts controller to send the mail out:

  def show
      @cart = Cart.find(params[:id])
    rescue ActiveRecord::RecordNotFound => e
      logger.error "Attempt to access invalid cart #{params[:id]}" 
      Notifier.error_occured(e).deliver   ##### new line
      redirect_to store_url, :notice => 'Invalid cart'
      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @cart }

The above code can be added to any other rescue blocks, but I am leaving it at that, for now.

Now visit http://localhost:3000/carts/wibble and you should receive a mail about the incident.

Page History
  • V3: Suresh Iyer [almost 7 years ago]
  • V2: Suresh Iyer [almost 7 years ago]
  • V1: Suresh Iyer [almost 7 years ago]