Skip to content

Instantly share code, notes, and snippets.

@punund
Created June 19, 2011 13:31

Revisions

  1. @tanraya tanraya created this gist Jun 19, 2011.
    42 changes: 42 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    # coding: utf-8
    class ApplicationController < ActionController::Base
    protect_from_forgery
    rescue_from Exception, :with => :handle_exceptions

    private

    def handle_exceptions(e)
    case e
    when CanCan::AccessDenied
    authenticate_user!
    when ActiveRecord::RecordNotFound
    not_found
    else
    internal_error(e)
    end
    end

    def not_found
    # Just render view
    render 'application/not_found', :status => 404
    end

    def internal_error(exception)
    if RAILS_ENV == 'production'
    # Send message to admin email via exception_notification
    request.env['exception_notifier.options'] = {
    :sender_address => "[email protected]",
    :exception_recipients => "[email protected]"
    }

    ExceptionNotifier::Notifier.exception_notification(request.env, exception).deliver

    # And just render view
    render :layout => 'layouts/internal_error',
    :template => 'application/internal_error',
    :status => 500
    else
    throw exception
    end
    end
    end