Sha256: f5001f4fe42def5319ce1824c90f23dba7368f2a4c0569475f1bc0e7366f6290

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 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

      #----------------------

      def render_error(status, e=StandardError)
        response.status = status

        if Rails.application.config.consider_all_requests_local
          raise e
        else
          respond_to do |format|
            format.html do
              render(
                :template   => "/errors/error_#{status}",
                :layout     => "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

2 entries across 2 versions & 1 rubygems

Version Path
outpost-cms-0.0.5 lib/outpost/controller/custom_errors.rb
outpost-cms-0.0.4 lib/outpost/controller/custom_errors.rb