Exception: StandardError
- Defined in:
- lib/coaster/core_ext/standard_error.rb,
lib/coaster/core_ext/standard_error/raven.rb
Instance Attribute Summary collapse
-
#fingerprint ⇒ Object
Returns the value of attribute fingerprint.
-
#level ⇒ Object
Returns the value of attribute level.
-
#raven ⇒ Object
Returns the value of attribute raven.
-
#tags ⇒ Object
Returns the value of attribute tags.
-
#tkey ⇒ Object
Returns the value of attribute tkey.
Class Method Summary collapse
Instance Method Summary collapse
- #_translate_params ⇒ Object
- #attributes ⇒ Object (also: #attr)
- #capture(options = {}) ⇒ Object
- #code ⇒ Object
-
#description ⇒ Object
(also: #desc)
description is user friendly messages, do not use error’s message error message is not user friendly in many cases.
- #http_status ⇒ Object
-
#initialize(message = nil, cause = $!) ⇒ StandardError
constructor
A new instance of StandardError.
-
#initialize_original ⇒ StandardError
A new instance of StandardError.
-
#just_logging ⇒ Object
options :logger :cleaner :fingerprint :tags :level :extra and others are merged to extra.
- #logging(options = {}) ⇒ Object
- #notes(options = {}) ⇒ Object
- #object ⇒ Object (also: #obj)
- #rails_tag ⇒ Object
- #raven_fingerprint ⇒ Object
- #root_cause ⇒ Object
- #status ⇒ Object
- #title ⇒ Object
- #to_detail ⇒ Object
- #to_hash ⇒ Object
- #to_json ⇒ Object
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 67 68 |
# File 'lib/coaster/core_ext/standard_error.rb', line 24 def initialize( = nil, cause = $!) @fingerprint = Coaster.default_fingerprint = {} @level = 'error' @attributes = HashWithIndifferentAccess.new @tkey = nil case when Exception msg = set_backtrace(.backtrace) when StandardError @fingerprint = .fingerprint = . @level = .level @tkey = .tkey @attributes = .attributes msg = set_backtrace(.backtrace) when Hash then hash = .with_indifferent_access rescue msg = hash.delete(:m) msg = hash.delete(:msg) || msg msg = hash.delete(:message) || msg @fingerprint = hash.delete(:fingerprint) || hash.delete(:fingerprints) = hash.delete(:tags) || hash.delete(:tag) @level = hash.delete(:level) || hash.delete(:severity) || @level @tkey = hash.delete(:tkey) msg = cause. if msg.nil? && cause @attributes.merge!(hash) when String, NilClass then msg = when FalseClass then msg = false else msg = @attributes[:object] = end @fingerprint = [] unless @fingerprint.is_a?(Array) = {} unless .is_a?(Hash) msg = nil if msg == false super(msg) set_backtrace(cause.backtrace) if cause end |
Instance Attribute Details
#fingerprint ⇒ Object
Returns the value of attribute fingerprint.
22 23 24 |
# File 'lib/coaster/core_ext/standard_error.rb', line 22 def fingerprint @fingerprint end |
#level ⇒ Object
Returns the value of attribute level.
22 23 24 |
# File 'lib/coaster/core_ext/standard_error.rb', line 22 def level @level end |
#raven ⇒ Object
Returns the value of attribute raven.
2 3 4 |
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 2 def raven @raven end |
#tags ⇒ Object
Returns the value of attribute tags.
22 23 24 |
# File 'lib/coaster/core_ext/standard_error.rb', line 22 def end |
#tkey ⇒ Object
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
.code ⇒ Object
10 11 12 |
# File 'lib/coaster/core_ext/standard_error.rb', line 10 def status 999999 # Unknown end |
.http_status ⇒ Object
12 13 14 |
# File 'lib/coaster/core_ext/standard_error.rb', line 12 def http_status 500 end |
.status ⇒ Object
7 8 9 |
# File 'lib/coaster/core_ext/standard_error.rb', line 7 def status 999999 # Unknown end |
.title ⇒ Object
16 17 18 19 |
# File 'lib/coaster/core_ext/standard_error.rb', line 16 def title t = _translate('.title') t.instance_variable_get(:@missing) ? nil : t end |
Instance Method Details
#_translate_params ⇒ Object
131 132 133 134 135 136 |
# File 'lib/coaster/core_ext/standard_error.rb', line 131 def _translate_params attributes.merge( type: self.class.name, status: status, http_status: http_status, message: ) end |
#attributes ⇒ Object Also known as: attr
78 79 80 81 82 83 84 85 |
# File 'lib/coaster/core_ext/standard_error.rb', line 78 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
36 37 38 39 40 41 42 43 |
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 36 def capture( = {}) 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 |
#code ⇒ Object
92 93 94 |
# File 'lib/coaster/core_ext/standard_error.rb', line 92 def code attributes[:code] || status end |
#description ⇒ Object Also known as: desc
description is user friendly messages, do not use error’s message error message is not user friendly in many cases.
98 99 100 101 102 103 104 |
# File 'lib/coaster/core_ext/standard_error.rb', line 98 def description dsc = attributes[:description] || attributes[:desc] return dsc if dsc msg = .dup msg.instance_variable_set(:@raw, true) msg end |
#http_status ⇒ Object
88 89 90 |
# File 'lib/coaster/core_ext/standard_error.rb', line 88 def http_status attributes[:http_status] || self.class.http_status end |
#initialize_original ⇒ StandardError
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 48 |
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 4 def initialize( = nil, cause = $!) @fingerprint = Coaster.default_fingerprint = {} @level = 'error' @attributes = HashWithIndifferentAccess.new @tkey = nil case when Exception msg = set_backtrace(.backtrace) when StandardError @fingerprint = .fingerprint = . @level = .level @tkey = .tkey @attributes = .attributes msg = set_backtrace(.backtrace) when Hash then hash = .with_indifferent_access rescue msg = hash.delete(:m) msg = hash.delete(:msg) || msg msg = hash.delete(:message) || msg @fingerprint = hash.delete(:fingerprint) || hash.delete(:fingerprints) = hash.delete(:tags) || hash.delete(:tag) @level = hash.delete(:level) || hash.delete(:severity) || @level @tkey = hash.delete(:tkey) msg = cause. if msg.nil? && cause @attributes.merge!(hash) when String, NilClass then msg = when FalseClass then msg = false else msg = @attributes[:object] = end @fingerprint = [] unless @fingerprint.is_a?(Array) = {} unless .is_a?(Hash) msg = nil if msg == false super(msg) set_backtrace(cause.backtrace) if cause end |
#just_logging ⇒ Object
options
:logger
:cleaner
:fingerprint
:tags
:level
:extra
and others are merged to extra
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 53 def logging( = {}) logger = [:logger] logger = Rails.logger if logger.nil? && defined?(Rails) return nil unless logger cl = [:cleaner] || cleaner msg = to_detail if cl && backtrace msg += "\t\t" msg += cleaner.clean(backtrace).join("\n\t\t") end logger.tagged(*rails_tag) do if logger.respond_to?(level) logger.send(level, msg) else logger.error(msg) end end end |
#logging(options = {}) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/coaster/core_ext/standard_error.rb', line 179 def logging( = {}) logger = [:logger] logger = Rails.logger if logger.nil? && defined?(Rails) return nil unless logger cl = [:cleaner] || cleaner msg = to_detail if cl && backtrace msg += "\t\t" msg += cleaner.clean(backtrace).join("\n\t\t") end logger.tagged(*rails_tag) do if logger.respond_to?(level) logger.send(level, msg) else logger.error(msg) end end end |
#notes(options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 22 def notes( = {}) opts = ? .dup : {} extra_opts = opts.slice!(:fingerprint, :tags, :level, :extra) opts[:extra] = extra_opts.merge(opts[:extra] || {}) notes = raven.merge(opts) notes[:fingerprint] ||= raven_fingerprint notes[:tags] ||= ( && .merge(notes[:tags] || {})) || {} notes[:tags] = notes[:tags].merge(environment: Rails.env) if defined?(Rails) notes[:level] ||= self.level notes[:extra] = attributes.merge(notes[:extra]) notes end |
#object ⇒ Object Also known as: obj
107 108 109 |
# File 'lib/coaster/core_ext/standard_error.rb', line 107 def object attributes[:object] || attributes[:obj] end |
#rails_tag ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/coaster/core_ext/standard_error.rb', line 167 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
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/coaster/core_ext/standard_error/raven.rb', line 10 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_cause ⇒ Object
112 113 114 |
# File 'lib/coaster/core_ext/standard_error.rb', line 112 def root_cause cause.respond_to?(:root_cause) ? cause.root_cause : self end |
#status ⇒ Object
70 71 72 |
# File 'lib/coaster/core_ext/standard_error.rb', line 70 def status self.class.status end |
#title ⇒ Object
74 75 76 |
# File 'lib/coaster/core_ext/standard_error.rb', line 74 def title attributes[:title] || self.class.title end |
#to_detail ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/coaster/core_ext/standard_error.rb', line 142 def to_detail lg = "[#{self.class.name}] status:#{status}" lg += "\n\tMESSAGE: #{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 end lg << "\n" end |
#to_hash ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/coaster/core_ext/standard_error.rb', line 116 def to_hash hash = @attributes.merge( type: self.class.name, status: status, http_status: http_status, message: ) if cause if cause.respond_to?(:to_hash) hash[:cause] = cause.to_hash else hash[:cause] = cause end end hash end |
#to_json ⇒ Object
138 139 140 |
# File 'lib/coaster/core_ext/standard_error.rb', line 138 def to_json Oj.dump(to_hash.with_indifferent_access, mode: :compat) end |