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 => "abc@xmail.com", :subject => 'Depot App Error Incident'  ## replace your email id to receive mails
  end
end
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 %>

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

  def show
    begin
      @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'
    else
      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @cart }
      end
    end
  end

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.