exceptions_controller.rb 1.1 KB

12345678910111213141516171819202122232425262728
  1. class ExceptionsController < ApplicationController
  2. layout "application"
  3. def show
  4. @exception = env["action_dispatch.exception"]
  5. @status_code = ActionDispatch::ExceptionWrapper.new(env, @exception).status_code
  6. @rescue_response = ActionDispatch::ExceptionWrapper.rescue_responses[@exception.class.name]
  7. respond_to do |format|
  8. format.html { render :show, status: @status_code, layout: !request.xhr? }
  9. format.xml { render xml: details, root: "error", status: @status_code }
  10. format.json { render json: { error: details }, status: @status_code }
  11. end
  12. end
  13. protected
  14. def details
  15. @details ||= {}.tap do |h|
  16. I18n.with_options scope: [:exception, :show, @rescue_response], exception_name: @exception.class.name, exception_message: @exception.message do |i18n|
  17. h[:name] = i18n.t "#{@exception.class.name.underscore}.title", default: i18n.t(:title, default: @exception.class.name)
  18. h[:message] = i18n.t "#{@exception.class.name.underscore}.description", default: i18n.t(:description, default: @exception.message)
  19. end
  20. end
  21. end
  22. helper_method :details
  23. end