Exception: Card::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/card/error.rb

Overview

exceptions and errors. (arguably most of these should be Card::Exception)

Direct Known Subclasses

ServerError, UserError

Defined Under Namespace

Classes: Abort, BadQuery, CodenameNotFound, EditConflict, NotFound, PermissionDenied, ServerError, UserError

Constant Summary collapse

KEY_MAP =
{ permission_denied: PermissionDenied,
conflict: EditConflict }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil) ⇒ Error

Returns a new instance of Error.



15
16
17
18
19
20
21
# File 'lib/card/error.rb', line 15

def initialize message=nil
  if message.is_a? Card
    self.card = message
    message = message_from_card
  end
  super message
end

Instance Attribute Details

#cardObject

Returns the value of attribute card.



13
14
15
# File 'lib/card/error.rb', line 13

def card
  @card
end

Class Method Details

.card_error_class(exception, card) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/card/error.rb', line 103

def card_error_class exception, card
  case exception
  when ActiveRecord::RecordInvalid
    invalid_card_error_class card
  when ActiveRecord::RecordNotFound, ActionController::MissingFile
    Card::Error::NotFound
  else
    Card::Error::ServerError
  end
end

.cardify_exception(exception, card) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/card/error.rb', line 95

def cardify_exception exception, card
  unless exception.is_a? Card::Error
    exception = card_error_class(exception, card).new exception.message
  end
  exception.card ||= card
  exception
end

.invalid_card_error_class(card) ⇒ Object



114
115
116
117
118
119
# File 'lib/card/error.rb', line 114

def invalid_card_error_class card
  KEY_MAP.each do |key, klass|
    return klass if card.errors.key? key
  end
  Card::Error
end

Instance Method Details

#card_message_textObject



32
33
34
# File 'lib/card/error.rb', line 32

def card_message_text
  card.errors.first&.message
end

#message_from_cardObject



23
24
25
26
# File 'lib/card/error.rb', line 23

def message_from_card
  I18n.t :exception_for_card,
         scope: %i[lib card error], cardname: card.name, message: card_message_text
end

#reportObject



28
29
30
# File 'lib/card/error.rb', line 28

def report
  Rails.logger.info "exception = #{self.class}: #{message}"
end