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.



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
# File 'lib/coaster/core_ext/standard_error.rb', line 26

def initialize(message = nil, cause = $!)
  @tags = []
  @level = 'error'
  @attributes = HashWithIndifferentAccess.new
  @tkey = nil

  case message
    when Exception
      msg = message
      set_backtrace(message.backtrace)
    when StandardError
      @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[:message]
      msg = hash[:msg] if msg.nil?
      msg = hash[:m] if msg.nil?
      @tags = Array(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, NilClass then
      msg = message
    when FalseClass then
      msg = false
    else
      msg = message
      @attributes[:object] = message
  end

  msg = nil if msg == false
  super(msg)
  set_backtrace(cause.backtrace) if cause
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



24
25
26
# File 'lib/coaster/core_ext/standard_error.rb', line 24

def level
  @level
end

#ravenObject

Returns the value of attribute raven.



2
3
4
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 2

def raven
  @raven
end

#tagsObject

Returns the value of attribute tags.



24
25
26
# File 'lib/coaster/core_ext/standard_error.rb', line 24

def tags
  @tags
end

#tkeyObject

Returns the value of attribute tkey.



24
25
26
# File 'lib/coaster/core_ext/standard_error.rb', line 24

def tkey
  @tkey
end

Class Method Details

.http_statusObject



14
15
16
# File 'lib/coaster/core_ext/standard_error.rb', line 14

def http_status
  500
end

.statusObject



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

def status
  999999 # Unknown
end

.titleObject



18
19
20
21
# File 'lib/coaster/core_ext/standard_error.rb', line 18

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

Instance Method Details

#attributesObject Also known as: attr



75
76
77
78
79
80
81
# File 'lib/coaster/core_ext/standard_error.rb', line 75

def attributes
  if cause && cause.respond_to?(:attributes)
    cause.attributes.merge(@attributes)
  else
    @attributes
  end
end

#capture(options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 19

def capture(options = {})
  notes = raven.merge(options || {})

  notes[:fingerprint] = notes[:fingerprint].flatten.map do |fp|
    if fp == true || fp == :class
      self.class.name
    elsif fp == :default
      '{{ default }}'
    else
      fp
    end
  end
  notes[:tags] ||= {}
  notes[:tags] = notes[:tags].merge(environment: Rails.env) if defined?(Rails)
  notes[:level] ||= self.level
  notes[:extra] = (notes[:extra] || {}).merge(attributes)

  Raven.annotate_exception(self, notes)
  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



88
89
90
# File 'lib/coaster/core_ext/standard_error.rb', line 88

def code
  attributes[:code] || http_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.



94
95
96
97
98
99
100
# File 'lib/coaster/core_ext/standard_error.rb', line 94

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

#fingerprintObject



15
16
17
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 15

def fingerprint
  raven[:fingerprint]
end

#fingerprint=(*fp) ⇒ Object



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

def fingerprint=(*fp)
  raven[:fingerprint] = fp
end

#http_statusObject



84
85
86
# File 'lib/coaster/core_ext/standard_error.rb', line 84

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
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 4

def initialize(message = nil, cause = $!)
  @tags = []
  @level = 'error'
  @attributes = HashWithIndifferentAccess.new
  @tkey = nil

  case message
    when Exception
      msg = message
      set_backtrace(message.backtrace)
    when StandardError
      @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[:message]
      msg = hash[:msg] if msg.nil?
      msg = hash[:m] if msg.nil?
      @tags = Array(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, NilClass then
      msg = message
    when FalseClass then
      msg = false
    else
      msg = message
      @attributes[:object] = message
  end

  msg = nil if msg == false
  super(msg)
  set_backtrace(cause.backtrace) if cause
end

#just_loggingObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 44

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 += "\t\t"
    msg += cleaner.clean(backtrace).join("\n\t\t")
  end

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

#logging(options = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/coaster/core_ext/standard_error.rb', line 153

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 += "\t\t"
    msg += cleaner.clean(backtrace).join("\n\t\t")
  end

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

#objectObject Also known as: obj



103
104
105
# File 'lib/coaster/core_ext/standard_error.rb', line 103

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

#statusObject



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

def status
  self.class.status
end

#titleObject



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

def title
  self.class.title
end

#to_detailObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/coaster/core_ext/standard_error.rb', line 134

def to_detail
  lg = "[#{self.class.name}] status:#{status}"
  lg += "\n\tMESSAGE: #{message.gsub(/\n/, "\n\t\t")}"
  instance_variables.each do |var|
    unless var.to_s.start_with?('@_')
      lg += "\n\t#{var}: #{instance_variable_get(var).inspect}"
    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
  end
  lg << "\n"
end

#to_hashObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/coaster/core_ext/standard_error.rb', line 108

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



130
131
132
# File 'lib/coaster/core_ext/standard_error.rb', line 130

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

#translate_paramsObject



123
124
125
126
127
128
# File 'lib/coaster/core_ext/standard_error.rb', line 123

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