Sha256: 270b18ea11dd5bc1b6379945f5ed46dc57f5c106d77d1195551f1d7cdb1177a1

Contents?: true

Size: 1.93 KB

Versions: 6

Compression:

Stored size: 1.93 KB

Contents

class Card
  class Format
    module Error
      def ok? task
        task = :create if task == :update && card.new_card?
        @ok ||= {}
        @ok[task] = card.ok? task if @ok[task].nil?
        @ok[task]
      end

      def anyone_can? task
        return false unless task.is_a? Symbol
        @anyone_can ||= {}
        @anyone_can[task] = card.anyone_can? task if @anyone_can[task].nil?
        @anyone_can[task]
      end

      def view_for_unknown _view
        if main?
          root.error_status = 404
          :not_found
        else
          :unknown
        end
      end

      def view_for_denial view, task
        @denied_task = task
        root.error_status = 403 if focal? && voo.root?
        view_setting(:denial, view) || :denial
      end

      def monitor_depth
        raise Card::Error::UserError, tr(:too_deep) if depth >= Card.config.max_depth
        yield
      end

      def rescue_view e, view
        method = loud_error? ? :loud_error : :quiet_error
        send method, e, view
      end

      def error_cardname _exception
        if card&.name.present?
          safe_name
        else
          I18n.t :no_cardname, scope: %i[lib card format error]
        end
      end

      def loud_error?
        focal? || Card.config.raise_all_rendering_errors
      end

      def loud_error e, view
        card.errors.add "#{view} view", rendering_error(e, view) if card.errors.empty?
        raise e
      end

      def quiet_error e, view
        # TODO: unify with Card::Error#report
        Rails.logger.info e.message
        Rails.logger.debug e.backtrace.join("\n")
        rendering_error e, view
      end

      def rendering_error exception, view
        if exception.is_a? Card::Error::UserError
          exception.message
        else
          tr :error_rendering, scope: %i[lib card format error],
                               cardname: error_cardname(exception), view: view
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
card-1.99.5 lib/card/format/error.rb
card-1.99.4 lib/card/format/error.rb
card-1.99.3 lib/card/format/error.rb
card-1.99.2 lib/card/format/error.rb
card-1.99.1 lib/card/format/error.rb
card-1.99.0 lib/card/format/error.rb