Sha256: 5291d3f0a43f0cf004f9c3f98d1c1c2bcab430357d795ee477d10eae9b942b75

Contents?: true

Size: 1.95 KB

Versions: 5

Compression:

Stored size: 1.95 KB

Contents

module Outpost
  module Controller
    module CustomErrors
      extend ActiveSupport::Concern

      NOT_FOUND_ERROR_CLASSES = [
        ActionController::RoutingError,
        ActionController::UnknownController,
        AbstractController::ActionNotFound,
        ActiveRecord::RecordNotFound
      ]

      if defined?(ActionController::UnknownFormat)
        NOT_FOUND_ERROR_CLASSES << ActionController::UnknownFormat
      end

      included do
        rescue_from StandardError,
          with: ->(e) { render_error(500, e) and return false }

        rescue_from *NOT_FOUND_ERROR_CLASSES,
          with: ->(e) { render_error(404, e) and return false }
      end


      private

      # We don't want this to be considered an action method,
      # so it's private
      def render_error(status, e=StandardError, template_prefix="outpost/")
        response.status = status

        if Rails.application.config.consider_all_requests_local
          raise e
        else
          respond_to do |format|
            format.html do
              render(
                :template   => "#{template_prefix}errors/error_#{status}",
                :layout     => "#{template_prefix}application",
                :status     => status,
                :locals     => { error: e }
              )
            end

            format.xml do
              render xml: {
                :error    => response.message,
                :code     => status
              }, status: status
            end

            format.json do
              render json: {
                :error    => response.message,
                :code     => status
              }, status: status
            end

            format.text do
              render(
                :text     => "#{status} - #{response.message}",
                :status   => status
              )
            end

            format.any do
              head status
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
outpost-cms-0.1.4 lib/outpost/controller/custom_errors.rb
outpost-cms-0.1.3 lib/outpost/controller/custom_errors.rb
outpost-cms-0.1.2 lib/outpost/controller/custom_errors.rb
outpost-cms-0.1.1 lib/outpost/controller/custom_errors.rb
outpost-cms-0.1.0 lib/outpost/controller/custom_errors.rb