Exception: StandardError

Inherits:
Exception
  • Object
show all
Defined in:
lib/coaster/core_ext/standard_error.rb,
lib/coaster/core_ext/standard_error/raven.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, cause = $!) ⇒ StandardError

Returns a new instance of StandardError.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/coaster/core_ext/standard_error.rb', line 24

def initialize(message = nil, cause = $!)
  @fingerprint = Coaster.default_fingerprint.dup
  @tags = {}
  @level = 'error'
  @attributes = HashWithIndifferentAccess.new
  @tkey = nil

  case message
    when Exception
      msg = message
      set_backtrace(message.backtrace)
    when StandardError
      @fingerprint = message.fingerprint
      @tags = message.tags
      @level = message.level
      @tkey = message.tkey
      @attributes = message.attributes
      msg = message
      set_backtrace(message.backtrace)
    when Hash then
      hash = message.with_indifferent_access rescue message
      msg = hash.delete(:m)
      msg = hash.delete(:msg) || msg
      msg = hash.delete(:message) || msg
      @fingerprint = hash.delete(:fingerprint) || hash.delete(:fingerprints)
      @tags = hash.delete(:tags) || hash.delete(:tag)
      @level = hash.delete(:level) || hash.delete(:severity) || @level
      @tkey = hash.delete(:tkey)
      msg = cause.message if msg.nil? && cause
      @attributes.merge!(hash)
    when String then
      msg = message
    when FalseClass, NilClass then
      msg = ''
    else
      msg = message
  end

  @fingerprint = [] unless @fingerprint.is_a?(Array)
  @tags = {} unless @tags.is_a?(Hash)
  msg ||= self.class.title
  super(msg)
end

Instance Attribute Details

#fingerprintObject

Returns the value of attribute fingerprint.



22
23
24
# File 'lib/coaster/core_ext/standard_error.rb', line 22

def fingerprint
  @fingerprint
end

#levelObject

Returns the value of attribute level.



22
23
24
# File 'lib/coaster/core_ext/standard_error.rb', line 22

def level
  @level
end

#ravenObject



10
11
12
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 10

def raven
  @raven ||= {}.with_indifferent_access
end

#tagsObject

Returns the value of attribute tags.



22
23
24
# File 'lib/coaster/core_ext/standard_error.rb', line 22

def tags
  @tags
end

#tkeyObject

Returns the value of attribute tkey.



22
23
24
# File 'lib/coaster/core_ext/standard_error.rb', line 22

def tkey
  @tkey
end

Class Method Details

.codeObject



10
11
12
# File 'lib/coaster/core_ext/standard_error.rb', line 10

def status
  999999 # Unknown
end

.http_statusObject



12
13
14
# File 'lib/coaster/core_ext/standard_error.rb', line 12

def http_status
  500
end

.statusObject



7
8
9
# File 'lib/coaster/core_ext/standard_error.rb', line 7

def status
  999999 # Unknown
end

.titleObject



16
17
18
19
# File 'lib/coaster/core_ext/standard_error.rb', line 16

def title
  t = _translate('.title')
  t.instance_variable_defined?(:@missing) ? nil : t
end

Instance Method Details

#_translate_paramsObject



133
134
135
136
137
138
# File 'lib/coaster/core_ext/standard_error.rb', line 133

def _translate_params
  attributes.merge(
    type: self.class.name, status: status,
    http_status: http_status, message: message
  )
end

#attributesObject Also known as: attr



80
81
82
83
84
85
86
87
# File 'lib/coaster/core_ext/standard_error.rb', line 80

def attributes
  @attributes ||= HashWithIndifferentAccess.new
  if cause && cause.respond_to?(:attributes) && cause.attributes.is_a?(Hash)
    cause.attributes.merge(@attributes)
  else
    @attributes
  end
end

#capture(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 40

def capture(options = {})
  return if options.key?(:report) && !options[:report]
  return if attributes.key?(:report) && !attributes[:report]
  Raven.annotate_exception(self, notes(options))
  Raven.capture_exception(self)
rescue => e
  msg = "#{e.class.name}: #{e.message}"
  msg += "\n\t" + e.backtrace.join("\n\t")
  Raven.logger.error(msg)
end

#codeObject



94
95
96
# File 'lib/coaster/core_ext/standard_error.rb', line 94

def code
  attributes[:code] || status
end

#descriptionObject Also known as: desc

description is user friendly messages, do not use error’s message error message is not user friendly in many cases.



100
101
102
103
104
105
106
# File 'lib/coaster/core_ext/standard_error.rb', line 100

def description
  dsc = attributes[:description] || attributes[:desc]
  return dsc if dsc
  msg = safe_message.dup
  msg.instance_variable_set(:@raw, true)
  msg
end

#http_statusObject



90
91
92
# File 'lib/coaster/core_ext/standard_error.rb', line 90

def http_status
  attributes[:http_status] || self.class.http_status
end

#initialize_originalStandardError

Returns a new instance of StandardError.

Returns:



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 4

def initialize(message = nil, cause = $!)
  @fingerprint = Coaster.default_fingerprint.dup
  @tags = {}
  @level = 'error'
  @attributes = HashWithIndifferentAccess.new
  @tkey = nil

  case message
    when Exception
      msg = message
      set_backtrace(message.backtrace)
    when StandardError
      @fingerprint = message.fingerprint
      @tags = message.tags
      @level = message.level
      @tkey = message.tkey
      @attributes = message.attributes
      msg = message
      set_backtrace(message.backtrace)
    when Hash then
      hash = message.with_indifferent_access rescue message
      msg = hash.delete(:m)
      msg = hash.delete(:msg) || msg
      msg = hash.delete(:message) || msg
      @fingerprint = hash.delete(:fingerprint) || hash.delete(:fingerprints)
      @tags = hash.delete(:tags) || hash.delete(:tag)
      @level = hash.delete(:level) || hash.delete(:severity) || @level
      @tkey = hash.delete(:tkey)
      msg = cause.message if msg.nil? && cause
      @attributes.merge!(hash)
    when String then
      msg = message
    when FalseClass, NilClass then
      msg = ''
    else
      msg = message
  end

  @fingerprint = [] unless @fingerprint.is_a?(Array)
  @tags = {} unless @tags.is_a?(Hash)
  msg ||= self.class.title
  super(msg)
end

#just_loggingObject

options

:logger
:cleaner
:fingerprint
:tags
:level
:extra
:report
and others are merged to extra


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 60

def logging(options = {})
  logger = options[:logger]
  logger = Rails.logger if logger.nil? && defined?(Rails)
  return nil unless logger

  cl = options[:cleaner] || cleaner
  msg = to_detail

  if cl && backtrace
    msg += "\tBACKTRACE:\n\t"
    msg += cl.clean(backtrace).join("\n\t")
  end

  logger.tagged(*rails_tag) do
    if level && logger.respond_to?(level)
      logger.send(level, msg)
    else
      logger.error(msg)
    end
  end
end

#logging(options = {}) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/coaster/core_ext/standard_error.rb', line 184

def logging(options = {})
  logger = options[:logger]
  logger = Rails.logger if logger.nil? && defined?(Rails)
  return nil unless logger

  cl = options[:cleaner] || cleaner
  msg = to_detail

  if cl && backtrace
    msg += "\tBACKTRACE:\n\t"
    msg += cl.clean(backtrace).join("\n\t")
  end

  logger.tagged(*rails_tag) do
    if level && logger.respond_to?(level)
      logger.send(level, msg)
    else
      logger.error(msg)
    end
  end
end

#notes(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 26

def notes(options = {})
  opts = options ? options.dup : {}
  extra_opts = opts.slice!(:fingerprint, :tags, :level, :extra)
  opts[:extra] = extra_opts.merge(opts[:extra] || {})
  nt = raven.merge(opts)

  nt[:fingerprint] ||= raven_fingerprint
  nt[:tags] ||= (tags && tags.merge(nt[:tags] || {})) || {}
  nt[:tags] = nt[:tags].merge(environment: Rails.env) if defined?(Rails)
  nt[:level] ||= self.level
  nt[:extra] = attributes.merge(nt[:extra])
  nt
end

#objectObject Also known as: obj



109
110
111
# File 'lib/coaster/core_ext/standard_error.rb', line 109

def object
  attributes[:object] || attributes[:obj]
end

#rails_tagObject



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/coaster/core_ext/standard_error.rb', line 172

def rails_tag
  (fingerprint || Coaster.default_fingerprint).flatten.map do |fp|
    if fp == true || fp == :class
      self.class.name
    elsif fp == :default || fp == '{{ default }}'
      nil
    else
      fp
    end
  end.compact
end

#raven_fingerprintObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 14

def raven_fingerprint
  (fingerprint || Coaster.default_raven_fingerprint).flatten.map do |fp|
    if fp == true || fp == :class
      self.class.name
    elsif fp == :default
      '{{ default }}'
    else
      fp
    end
  end.flatten
end

#root_causeObject



114
115
116
# File 'lib/coaster/core_ext/standard_error.rb', line 114

def root_cause
  cause.respond_to?(:root_cause) ? cause.root_cause : self
end

#safe_messageObject



68
69
70
# File 'lib/coaster/core_ext/standard_error.rb', line 68

def safe_message
  message || ''
end

#statusObject



72
73
74
# File 'lib/coaster/core_ext/standard_error.rb', line 72

def status
  self.class.status
end

#titleObject



76
77
78
# File 'lib/coaster/core_ext/standard_error.rb', line 76

def title
  attributes[:title] || self.class.title
end

#to_detailObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/coaster/core_ext/standard_error.rb', line 144

def to_detail
  lg = "[#{self.class.name}] status:#{status}"
  lg += "\n\tMESSAGE: #{safe_message.gsub(/\n/, "\n\t\t")}"
  instance_variables.each do |var|
    if var.to_s.start_with?('@_')
      next
    elsif var.to_s == '@spell_checker'
      next
    else
      val = instance_variable_get(var)
      val = val.inspect rescue val.to_s
      lg += "\n\t#{var}: #{val}"
    end
  end
  if cause
    if cause.respond_to?(:to_detail)
      lg += "\n\tCAUSE: "
      lg += cause.to_detail.strip.gsub(/\n/, "\n\t")
    else
      lg += "\n\tCAUSE: #{cause.class.name}: #{cause.message.gsub(/\n/, "\n\t\t")}"
    end
    if cause_cleaner && cause.backtrace
      lg += cause_cleaner.clean(cause.backtrace).join("\n\t\t")
    end
  end
  lg << "\n"
end

#to_hashObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/coaster/core_ext/standard_error.rb', line 118

def to_hash
  hash = @attributes.merge(
    type: self.class.name, status: status,
    http_status: http_status, message: message
  )
  if cause
    if cause.respond_to?(:to_hash)
      hash[:cause] = cause.to_hash
    else
      hash[:cause] = cause
    end
  end
  hash
end

#to_jsonObject



140
141
142
# File 'lib/coaster/core_ext/standard_error.rb', line 140

def to_json
  Oj.dump(to_hash.with_indifferent_access, mode: :compat)
end