Exception: StandardError
- Inherits:
-
Exception
- Object
- Exception
- StandardError
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.
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
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/coaster/core_ext/standard_error.rb', line 35
def initialize(message = nil, cause = $!)
@fingerprint = Coaster.default_fingerprint.dup
@tags = {}
@level = 'error'
@attributes = HashWithIndifferentAccess.new
@attributes.merge!(cause.attributes || {}) if cause && cause.respond_to?(:attributes)
@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
hash[:description] ||= hash.delete(:desc) if hash[:desc].present?
@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)
@attributes.merge!(hash)
when String then
msg = message
when FalseClass, NilClass then
msg = nil
else
msg = message
end
@fingerprint = [] unless @fingerprint.is_a?(Array)
@tags = {} unless @tags.is_a?(Hash)
msg = cause.message if msg.blank? && cause
super(msg)
end
|
Instance Attribute Details
#fingerprint ⇒ Object
Returns the value of attribute fingerprint.
33
34
35
|
# File 'lib/coaster/core_ext/standard_error.rb', line 33
def fingerprint
@fingerprint
end
|
Returns the value of attribute level.
33
34
35
|
# File 'lib/coaster/core_ext/standard_error.rb', line 33
def level
@level
end
|
10
11
12
|
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 10
def raven
@raven ||= {}.with_indifferent_access
end
|
Returns the value of attribute tags.
33
34
35
|
# File 'lib/coaster/core_ext/standard_error.rb', line 33
def tags
@tags
end
|
Returns the value of attribute tkey.
33
34
35
|
# File 'lib/coaster/core_ext/standard_error.rb', line 33
def tkey
@tkey
end
|
Class Method Details
.after_logging(name, &block) ⇒ Object
23
24
25
26
|
# File 'lib/coaster/core_ext/standard_error.rb', line 23
def after_logging(name, &block)
@after_logging_blocks ||= {}
@after_logging_blocks[name] = block
end
|
.after_logging_blocks ⇒ Object
27
28
29
30
|
# File 'lib/coaster/core_ext/standard_error.rb', line 27
def after_logging_blocks
@after_logging_blocks ||= {}
superclass <= StandardError ? superclass.after_logging_blocks.merge(@after_logging_blocks) : @after_logging_blocks
end
|
.before_logging(name, &block) ⇒ Object
14
15
16
17
|
# File 'lib/coaster/core_ext/standard_error.rb', line 14
def before_logging(name, &block)
@before_logging_blocks ||= {}
@before_logging_blocks[name] = block
end
|
.before_logging_blocks ⇒ Object
18
19
20
21
|
# File 'lib/coaster/core_ext/standard_error.rb', line 18
def before_logging_blocks
@before_logging_blocks ||= {}
superclass <= StandardError ? superclass.before_logging_blocks.merge(@before_logging_blocks) : @before_logging_blocks
end
|
8
|
# File 'lib/coaster/core_ext/standard_error.rb', line 8
def status; 999999 end
|
.http_status ⇒ Object
9
|
# File 'lib/coaster/core_ext/standard_error.rb', line 9
def http_status; 500 end
|
.intentional? ⇒ Boolean
11
|
# File 'lib/coaster/core_ext/standard_error.rb', line 11
def intentional?; false end
|
.report? ⇒ Boolean
10
|
# File 'lib/coaster/core_ext/standard_error.rb', line 10
def report?; true end
|
7
|
# File 'lib/coaster/core_ext/standard_error.rb', line 7
def status; 999999 end
|
12
|
# File 'lib/coaster/core_ext/standard_error.rb', line 12
def title; _translate('.title') end
|
Instance Method Details
#_translate(*args) ⇒ Object
123
124
125
126
|
# File 'lib/coaster/core_ext/standard_error.rb', line 123
def _translate(*args)
return description if description.present?
super
end
|
#_translate_params ⇒ Object
128
129
130
|
# File 'lib/coaster/core_ext/standard_error.rb', line 128
def _translate_params
attributes
end
|
#after_logging_blocks ⇒ Object
83
|
# File 'lib/coaster/core_ext/standard_error.rb', line 83
def after_logging_blocks; self.class.after_logging_blocks end
|
#attributes ⇒ Object
Also known as:
attr
86
87
88
89
90
91
92
93
|
# File 'lib/coaster/core_ext/standard_error.rb', line 86
def attributes
return @attributes if defined?(@attributes)
@attributes = HashWithIndifferentAccess.new
if cause && cause.respond_to?(:attributes) && cause.attributes.is_a?(Hash)
@attributes = @attributes.merge(cause.attributes)
end
@attributes
end
|
#before_logging_blocks ⇒ Object
82
|
# File 'lib/coaster/core_ext/standard_error.rb', line 82
def before_logging_blocks; self.class.before_logging_blocks end
|
#capture(options = {}) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 39
def capture(options = {})
return if options.key?(:report) && !options[:report]
return unless report?
nt = notes(options)
Raven.capture_exception(self, level: nt[:level]) do |event|
event.user.merge!(nt[:user] || {})
event.tags.merge!(nt[:tags])
event..merge!(nt[:extra])
event.fingerprint = raven_fingerprint
end
rescue => e
msg = "#{e.class.name}: #{e.message}"
msg += "\n\t" + e.backtrace.join("\n\t")
Raven.logger.error(msg)
end
|
98
|
# File 'lib/coaster/core_ext/standard_error.rb', line 98
def code; attributes[:code] || status end
|
#code=(value) ⇒ Object
99
|
# File 'lib/coaster/core_ext/standard_error.rb', line 99
def code=(value); attributes[:code] = value end
|
#description ⇒ Object
Also known as:
desc
description is user friendly message as a attribute, do not use error’s message error message is not user friendly in many cases.
118
119
120
|
# File 'lib/coaster/core_ext/standard_error.rb', line 118
def description
attributes[:description] || attributes[:desc]
end
|
#descriptions ⇒ Object
another user friendly messages
142
143
144
145
146
|
# File 'lib/coaster/core_ext/standard_error.rb', line 142
def descriptions
return attributes[:descriptions] if attributes[:descriptions]
attributes[:descriptions] = {}
attributes[:descriptions]
end
|
#http_status ⇒ Object
96
|
# File 'lib/coaster/core_ext/standard_error.rb', line 96
def http_status; attributes[:http_status] || self.class.http_status end
|
#http_status=(value) ⇒ Object
97
|
# File 'lib/coaster/core_ext/standard_error.rb', line 97
def http_status=(value); attributes[:http_status] = value end
|
Returns a new instance of StandardError.
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
47
|
# 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
@attributes.merge!(cause.attributes || {}) if cause && cause.respond_to?(:attributes)
@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
hash[:description] ||= hash.delete(:desc) if hash[:desc].present?
@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)
@attributes.merge!(hash)
when String then
msg = message
when FalseClass, NilClass then
msg = nil
else
msg = message
end
@fingerprint = [] unless @fingerprint.is_a?(Array)
@tags = {} unless @tags.is_a?(Hash)
msg = cause.message if msg.blank? && cause
super(msg)
end
|
#intentional? ⇒ Boolean
108
109
110
111
112
|
# File 'lib/coaster/core_ext/standard_error.rb', line 108
def intentional?
return attributes[:intentional] if attributes.key?(:intentional)
return true if it_should_not_happen?
self.class.intentional?
end
|
#it_might_happen? ⇒ Boolean
101
|
# File 'lib/coaster/core_ext/standard_error.rb', line 101
def it_might_happen?; attributes[:it] == :might_happen end
|
#it_should_not_happen? ⇒ Boolean
102
|
# File 'lib/coaster/core_ext/standard_error.rb', line 102
def it_should_not_happen?; attributes[:it] == :should_not_happen end
|
#just_logging ⇒ Object
options
:logger
:cleaner
:fingerprint
:tags
:level
:extra
:report
and others are merged to
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 64
def logging(options = {})
before_logging_blocks.values.each { |blk| instance_exec &blk }
if !report? || intentional?
if defined?(Rails)
return if Rails.env.test?
else
return
end
end
logger = options[:logger] || Coaster.logger
return unless logger
cl = options[:cleaner] || cleaner
msg = to_detail
if cl && backtrace
bt = cl.clean(backtrace)
bt = bt[0..2] if intentional?
msg += "\tBACKTRACE:\n\t"
msg += bt.join("\n\t")
end
if level && logger.respond_to?(level)
logger.send(level, msg)
else
logger.error(msg)
end
msg
ensure
after_logging_blocks.values.each { |blk| instance_exec &blk }
end
|
#logging(options = {}) ⇒ Object
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/coaster/core_ext/standard_error.rb', line 216
def logging(options = {})
before_logging_blocks.values.each { |blk| instance_exec &blk }
if !report? || intentional?
if defined?(Rails)
return if Rails.env.test?
else
return
end
end
logger = options[:logger] || Coaster.logger
return unless logger
cl = options[:cleaner] || cleaner
msg = to_detail
if cl && backtrace
bt = cl.clean(backtrace)
bt = bt[0..2] if intentional?
msg += "\tBACKTRACE:\n\t"
msg += bt.join("\n\t")
end
if level && logger.respond_to?(level)
logger.send(level, msg)
else
logger.error(msg)
end
msg
ensure
after_logging_blocks.values.each { |blk| instance_exec &blk }
end
|
#notes(options = {}) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 26
def notes(options = {})
opts = options ? options.dup : {}
= opts.slice!(:fingerprint, :tags, :level, :extra)
opts[:extra] = .merge(opts[:extra] || {})
nt = raven.merge(opts)
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
|
#object ⇒ Object
Also known as:
obj
113
|
# File 'lib/coaster/core_ext/standard_error.rb', line 113
def object; attributes[:object] || attributes[:obj] end
|
#rails_tag ⇒ Object
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/coaster/core_ext/standard_error.rb', line 204
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_fingerprint ⇒ Object
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_fingerprint).flatten.map do |fp|
if fp == true || fp == :class
self.class.name
elsif fp == :default
'{{ default }}'
else
fp
end
end.flatten
end
|
#report? ⇒ Boolean
103
104
105
106
107
|
# File 'lib/coaster/core_ext/standard_error.rb', line 103
def report?
return attributes[:report] if attributes.key?(:report)
return false if it_might_happen?
self.class.report?
end
|
#root_cause ⇒ Object
84
|
# File 'lib/coaster/core_ext/standard_error.rb', line 84
def root_cause; cause.respond_to?(:root_cause) ? cause.root_cause : self end
|
#safe_message ⇒ Object
80
|
# File 'lib/coaster/core_ext/standard_error.rb', line 80
def safe_message; message || '' end
|
81
|
# File 'lib/coaster/core_ext/standard_error.rb', line 81
def status; self.class.status end
|
100
|
# File 'lib/coaster/core_ext/standard_error.rb', line 100
def title; attributes[:title] || self.class.title end
|
#to_detail ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/coaster/core_ext/standard_error.rb', line 176
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
|
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/coaster/core_ext/standard_error.rb', line 157
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
|
172
173
174
|
# File 'lib/coaster/core_ext/standard_error.rb', line 172
def to_json
Oj.dump(to_hash.with_indifferent_access, mode: :compat)
end
|
#to_origin_s ⇒ Object
151
|
# File 'lib/coaster/core_ext/standard_error.rb', line 151
alias to_origin_s to_s
|
153
154
155
|
# File 'lib/coaster/core_ext/standard_error.rb', line 153
def to_s
"#{_translate} (#{to_origin_s})"
end
|
#user_message ⇒ Object
user friendly message, for overid
133
134
135
136
137
138
139
|
# File 'lib/coaster/core_ext/standard_error.rb', line 133
def user_message
return description if description.present?
if tkey.present?
return _translate
end
message
end
|