Skip to content

Instantly share code, notes, and snippets.

@bjones
Created July 30, 2010 02:29
Show Gist options
  • Save bjones/499767 to your computer and use it in GitHub Desktop.
Save bjones/499767 to your computer and use it in GitHub Desktop.
class ApplicationController < ActionController::Base
rescue_from ActionController::RoutingError, :with => :route_not_found
rescue_from ActionController::UnknownAction, :with => :unknown_action
rescue_from ActionController::MethodNotAllowed, :with => :method_not_allowed
if (defined? ActiveRecord)
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
end
def route_not_found(exception)
handle_error('no route', :status => :not_found)
end
def unknown_action(exception)
handle_error('unknown action', :status => :not_found)
end
def method_not_allowed(exception)
handle_error('method not allowed', :status => :not_found)
end
def record_not_found(exception)
handle_error('no record', :status => :not_found)
end
def handle_error(message, params)
error = {
:error => message,
:request => request.request_method.to_s.upcase! + ' ' + request.request_uri
}
status = params[:status] || :not_found
respond_to do |format|
format.xml { render :xml => error.to_xml, :status => status }
format.json { render :json => error.to_json, :status => status }
format.yaml { render :text => error.to_yaml, :status => status }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment