Exception: Card::Error

Inherits:
StandardError 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, BadAddress, BadQuery, CodenameNotFound, EditConflict, NotFound, PermissionDenied, ServerError, UserError

Constant Summary collapse

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

Constants inherited from StandardError

StandardError::MAX_BACKTRACE_LINES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StandardError

#report

Constructor Details

#initialize(message = nil) ⇒ Error

Returns a new instance of Error.



25
26
27
28
29
30
31
# File 'lib/card/error.rb', line 25

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

Instance Attribute Details

#backtraceObject



37
38
39
# File 'lib/card/error.rb', line 37

def backtrace
  @backtrace || super
end

#cardObject

Returns the value of attribute card.



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

def card
  @card
end

Class Method Details

.add_card_errors(card, exception) ⇒ Object



150
151
152
153
# File 'lib/card/error.rb', line 150

def add_card_errors card, exception
  label = exception.class.to_s.split("::").last
  card.errors.add label, exception.message
end

.card_error_class(exception, card) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/card/error.rb', line 155

def card_error_class exception, card
  # "simple" error messages are visible to end users and are generally not
  # treated as software bugs (though they may be "shark" bugs)
  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



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/card/error.rb', line 137

def cardify_exception exception, card
  card_exception =
    if exception.is_a? Card::Error
      exception
    else
      card_error_class(exception, card).new exception.message
    end
  card_exception.card ||= card
  card_exception.backtrace ||= exception.backtrace
  add_card_errors card, card_exception if card.errors.empty?
  card_exception
end

.invalid_card_error_class(card) ⇒ Object



168
169
170
171
172
173
# File 'lib/card/error.rb', line 168

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

.report(exception, card) ⇒ Object



130
131
132
133
134
135
# File 'lib/card/error.rb', line 130

def report exception, card
  e = cardify_exception exception, card
  self.current = e
  e.report
  e
end

Instance Method Details

#card_message_textObject



41
42
43
# File 'lib/card/error.rb', line 41

def card_message_text
  card.errors.first&.message
end

#message_from_cardObject



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

def message_from_card
  ::I18n.t :lib_exception_for_card, cardname: card.name, message: card_message_text
end