sheerun/rails4-bootstrap

View on GitHub
app/controllers/errors_controller.rb

Summary

Maintainability
A
0 mins
Test Coverage
class ErrorsController < ActionController::Base

  # Because we don't inherit from ApplicationController
  layout 'error'

  # You can use these method in views
  helper_method :status_code, :status_name, :error_message

  # You can edit format.json for proper API response format.
  def show
    custom_template = template_exists?(status_code, 'errors')

    respond_to do |format|
      format.html { render action: custom_template ? status_code.to_s : 'show' }
      format.json { render json: { status: status_code, message: error_message } }
    end
  end

  protected

  def status_code
    (request.path.match(/\d{3}/) || ['500'])[0].to_i
  end

  def status_name
    Rack::Utils::HTTP_STATUS_CODES.fetch(status_code, "Internal Server Error")
  end

  def error_message
    if status_code != 500 && exception
      exception.message
    else
      "Our administrator has been notified about this event."
    end
  end

  def exception
    env['action_dispatch.exception']
  end
end