Exception: Card::Error

Inherits:
StandardError
  • Object
show all
Defined in:
card/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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil) ⇒ Error

Returns a new instance of Error.



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

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

Instance Attribute Details

#backtraceObject



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

def backtrace
  @backtrace || super
end

#cardObject

Returns the value of attribute card.



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

def card
  @card
end

Class Method Details

.add_card_errors(card, exception) ⇒ Object



146
147
148
149
# File 'card/lib/card/error.rb', line 146

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



151
152
153
154
155
156
157
158
159
160
161
162
# File 'card/lib/card/error.rb', line 151

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



133
134
135
136
137
138
139
140
141
142
143
144
# File 'card/lib/card/error.rb', line 133

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



164
165
166
167
168
169
# File 'card/lib/card/error.rb', line 164

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



126
127
128
129
130
131
# File 'card/lib/card/error.rb', line 126

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

Instance Method Details

#card_message_textObject



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

def card_message_text
  card.errors.first&.message
end

#message_from_cardObject



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

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

#reportObject



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

def report
  Rails.logger.info "exception = #{self.class}: #{message}"
  Rails.logger.debug backtrace.join("\n")
end