Sha256: ba40bc3d792db97657d108645ee6ad053e7ac6e8bb5dffa94be90c98e89bfee9

Contents?: true

Size: 1.99 KB

Versions: 37

Compression:

Stored size: 1.99 KB

Contents

module RsErrors
  extend ActiveSupport::Concern
  included do
    if Rails.env.production? || Rails.env.staging?
      rescue_from Exception, with: :render_500
      rescue_from ActionController::RoutingError, with: :render_404
      rescue_from ActionController::UnknownController, with: :render_404
      rescue_from AbstractController::ActionNotFound, with: :render_404
      if RocketCMS.mongoid?
        rescue_from Mongoid::Errors::DocumentNotFound, with: :render_404
        rescue_from Mongoid::Errors::InvalidFind, with: :render_404
      end
      if RocketCMS.active_record?
        rescue_from ActiveRecord::RecordNotFound, with: :render_404
      end
    end

    if defined?(CanCan)
      rescue_from CanCan::AccessDenied do |exception|
        if !user_signed_in?
          #scope = rails_admin? ? main_app : self
          #redirect_to scope.new_user_session_path, alert: "Необходимо авторизоваться"
          authenticate_user!
        else
          redirect_to '/', alert: "Доступ запрещен"
        end
      end
    end
  end

  protected
  def render_404(exception = nil)
    Rails.logger.error "404: #{request.url}"
    unless exception.nil?
      Rails.logger.error exception.message
      Rails.logger.error exception.backtrace.join("\n")
      capture_exception(exception) if defined?(Raven)
    end
    render_error(404)
  end

  def render_500(exception)
    Rails.logger.error "500: #{request.url}"
    Rails.logger.error exception.message
    Rails.logger.error exception.backtrace.join("\n")
    capture_exception(exception) if defined?(Raven)
    begin
      if rails_admin_controller?
        return render text: 'Внутренняя ошибка'
      end
    rescue
    end
    render_error(500)
  end

  def render_error(code = 500)
    render template: "errors/error_#{code}", formats: [:html], handlers: [:haml, :slim], layout: RocketCMS.configuration.error_layout, status: code
  end

  def rails_admin?
    self.is_a?(RailsAdmin::ApplicationController)
  end
end

Version data entries

37 entries across 37 versions & 2 rubygems

Version Path
ack_rocket_cms-0.7.2.4 app/controllers/concerns/rs_errors.rb
ack_rocket_cms-0.7.2.3.2 app/controllers/concerns/rs_errors.rb
ack_rocket_cms-0.7.2.3.1 app/controllers/concerns/rs_errors.rb
ack_rocket_cms-0.7.2.3 app/controllers/concerns/rs_errors.rb
ack_rocket_cms-0.7.2.2 app/controllers/concerns/rs_errors.rb
rocket_cms-0.6.10 app/controllers/concerns/rs_errors.rb
ack_rocket_cms-0.7.2.1 app/controllers/concerns/rs_errors.rb
rocket_cms-0.6.9 app/controllers/concerns/rs_errors.rb
rocket_cms-0.6.8 app/controllers/concerns/rs_errors.rb
rocket_cms-0.6.7 app/controllers/concerns/rs_errors.rb
rocket_cms-0.6.6 app/controllers/concerns/rs_errors.rb
rocket_cms-0.6.5 app/controllers/concerns/rs_errors.rb
rocket_cms-0.6.4 app/controllers/concerns/rs_errors.rb
rocket_cms-0.6.3 app/controllers/concerns/rs_errors.rb
rocket_cms-0.6.2 app/controllers/concerns/rs_errors.rb
rocket_cms-0.6.1 app/controllers/concerns/rs_errors.rb
rocket_cms-0.6.0 app/controllers/concerns/rs_errors.rb
rocket_cms-0.5.24 app/controllers/concerns/rs_errors.rb
rocket_cms-0.5.23 app/controllers/concerns/rs_errors.rb
rocket_cms-0.5.22 app/controllers/concerns/rs_errors.rb