coaster 1.0.1 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7bb245ac61cf472e066e9f33934edfb00b715b07a2745ba30dcee503d973fbee
4
- data.tar.gz: 133b5ae2c21130ff0b9ed27d03388b5f682b7ff95f31c2c455f2a36b62084cf4
3
+ metadata.gz: cb23e4c7362906cc9877bde117d7f1027adc08726b718526dd16bfe967355553
4
+ data.tar.gz: f23ecb00881002158f99bc56daa261317f54b0620de3c6766b4fab93dc0d1e31
5
5
  SHA512:
6
- metadata.gz: c26dfe88f571581a60fac9c5f7a4594ce6929e7dbee7aab4bd3b134e29e595ab8f3ee2dd912110625a810deef1bf5ef0c67e8935c4b9d9a5e1fd459a8a23b2ca
7
- data.tar.gz: 983374e824e12b9da051ec0a246e65a6aaa0fe35c1bb75561aaff3398f4ffcd9f63009b6db97f23ff163ba073563400be07368103f9595b28049e489a7f39e3c
6
+ metadata.gz: b95bdef229ab03c481d31bf5f768ab9e1dec2a7d4d638d3bf1418da33e0665759030de2ef621fad94860b697373b8988dbe61b7955c479e7c9c1e3288c4ac656
7
+ data.tar.gz: 57982252eddf851fe0245a8b141aeaab2e3213219ef49688ef59004cf9478054a1849e016f80289b5f3e93e2ec84f8b022fb3e8991bfc602aa1401709eebf481
@@ -39,13 +39,13 @@ class Object
39
39
  end
40
40
 
41
41
  if result.is_a?(I18n::MissingTranslation)
42
- Coaster.logger && Coaster.logger.warn(result)
43
42
  unless options.key?(:original_missing)
44
43
  options.merge!(original_missing: result)
45
44
  end
46
45
 
47
46
  if key_class.superclass == Object || key_class == Object
48
47
  return options[:description] if options[:description].present?
48
+ Coaster.logger && Coaster.logger.warn(result)
49
49
  throw :exception, result if options[:original_throw]
50
50
  missing = options[:original_missing] || result
51
51
  msg = missing.message
@@ -4,19 +4,34 @@ class StandardError
4
4
  cattr_accessor :cleaner, :cause_cleaner
5
5
 
6
6
  class << self
7
- def status
8
- 999999 # Unknown
9
- end
7
+ def status; 999999 end # Unknown
10
8
  alias_method :code, :status
11
-
12
- def http_status
13
- 500
14
- end
9
+ def http_status; 500 end
10
+ def report?; true end
11
+ def intentional?; false end
15
12
 
16
13
  def title
17
14
  t = _translate('.title')
18
15
  t.instance_variable_defined?(:@missing) ? nil : t
19
16
  end
17
+
18
+ def before_logging(name, &block)
19
+ @before_logging_blocks ||= {}
20
+ @before_logging_blocks[name] = block
21
+ end
22
+ def before_logging_blocks
23
+ @before_logging_blocks ||= {}
24
+ superclass <= StandardError ? superclass.after_logging_blocks.merge(@before_logging_blocks) : @before_logging_blocks
25
+ end
26
+
27
+ def after_logging(name, &block)
28
+ @after_logging_blocks ||= {}
29
+ @after_logging_blocks[name] = block
30
+ end
31
+ def after_logging_blocks
32
+ @after_logging_blocks ||= {}
33
+ superclass <= StandardError ? superclass.after_logging_blocks.merge(@after_logging_blocks) : @after_logging_blocks
34
+ end
20
35
  end
21
36
 
22
37
  attr_accessor :tags, :level, :tkey, :fingerprint
@@ -66,17 +81,11 @@ class StandardError
66
81
  super(msg)
67
82
  end
68
83
 
69
- def safe_message
70
- message || ''
71
- end
72
-
73
- def status
74
- self.class.status
75
- end
76
-
77
- def title
78
- attributes[:title] || self.class.title
79
- end
84
+ def safe_message; message || '' end
85
+ def status; self.class.status end
86
+ def before_logging_blocks; self.class.before_logging_blocks end
87
+ def after_logging_blocks; self.class.after_logging_blocks end
88
+ def root_cause; cause.respond_to?(:root_cause) ? cause.root_cause : self end
80
89
 
81
90
  def attributes
82
91
  return @attributes if defined?(@attributes)
@@ -88,21 +97,15 @@ class StandardError
88
97
  end
89
98
  alias_method :attr, :attributes
90
99
 
91
- def http_status
92
- attributes[:http_status] || self.class.http_status
93
- end
94
-
95
- def http_status=(value)
96
- attributes[:http_status] = value
97
- end
98
-
99
- def code
100
- attributes[:code] || status
101
- end
102
-
103
- def code=(value)
104
- attributes[:code] = value
105
- end
100
+ def http_status; attributes[:http_status] || self.class.http_status end
101
+ def http_status=(value); attributes[:http_status] = value end
102
+ def code; attributes[:code] || status end
103
+ def code=(value); attributes[:code] = value end
104
+ def title; attributes[:title] || self.class.title end
105
+ def report?; attributes.key?(:report) ? attributes[:report] : self.class.report? end
106
+ def intentional?; attributes.key?(:intentional) ? attributes[:intentional] : self.class.intentional? end
107
+ def object; attributes[:object] || attributes[:obj] end
108
+ alias_method :obj, :object
106
109
 
107
110
  # description is user friendly messages, do not use error's message
108
111
  # error message is not user friendly in many cases.
@@ -115,17 +118,15 @@ class StandardError
115
118
  end
116
119
  alias_method :desc, :description
117
120
 
118
- def object
119
- attributes[:object] || attributes[:obj]
120
- end
121
- alias_method :obj, :object
122
-
123
- def root_cause
124
- cause.respond_to?(:root_cause) ? cause.root_cause : self
121
+ # more user friendly messages
122
+ def descriptions
123
+ return attributes[:descriptions] if attributes[:descriptions]
124
+ attributes[:descriptions] = {}
125
+ attributes[:descriptions]
125
126
  end
126
127
 
127
128
  def to_hash
128
- hash = @attributes.merge(
129
+ hash = attributes.merge(
129
130
  type: self.class.name, status: status,
130
131
  http_status: http_status, message: message
131
132
  )
@@ -191,16 +192,25 @@ class StandardError
191
192
  end
192
193
 
193
194
  def logging(options = {})
194
- logger = options[:logger] || Coaster.logger
195
- logger = Rails.logger if logger.nil? && defined?(Rails)
196
- return nil unless logger
195
+ before_logging_blocks.values.each { |blk| instance_exec &blk }
196
+
197
+ return unless report?
198
+ logger = nil
199
+ if defined?(Rails)
200
+ return if Rails.env.test? && intentional?
201
+ logger = Rails.logger
202
+ end
203
+ logger = options[:logger] || Coaster.logger || logger
204
+ return unless logger
197
205
 
198
206
  cl = options[:cleaner] || cleaner
199
207
  msg = to_detail
200
208
 
201
209
  if cl && backtrace
210
+ bt = cl.clean(backtrace)
211
+ bt = bt[0..2] if intentional?
202
212
  msg += "\tBACKTRACE:\n\t"
203
- msg += cl.clean(backtrace).join("\n\t")
213
+ msg += bt.join("\n\t")
204
214
  end
205
215
 
206
216
  if level && logger.respond_to?(level)
@@ -208,5 +218,7 @@ class StandardError
208
218
  else
209
219
  logger.error(msg)
210
220
  end
221
+
222
+ after_logging_blocks.values.each { |blk| instance_exec &blk }
211
223
  end
212
224
  end
@@ -38,7 +38,7 @@ class StandardError
38
38
 
39
39
  def capture(options = {})
40
40
  return if options.key?(:report) && !options[:report]
41
- return if attributes.key?(:report) && !attributes[:report]
41
+ return unless report?
42
42
  nt = notes(options)
43
43
  Raven.capture_exception(self, level: nt[:level]) do |event|
44
44
  event.user.merge!(nt[:user] || {})
@@ -1,3 +1,3 @@
1
1
  module Coaster
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.6'
3
3
  end
@@ -6,3 +6,8 @@ require 'pry'
6
6
  require 'rubygems'
7
7
  require 'bundler/setup'
8
8
  require 'coaster'
9
+
10
+ class Raven
11
+ def self.capture_exception(*args)
12
+ end
13
+ end
@@ -139,5 +139,31 @@ LOG
139
139
  assert_equal e.notes(the_other: 'something')[:extra][:something], 'other'
140
140
  assert_equal e.notes(the_other: 'something')[:extra][:the_other], 'something'
141
141
  end
142
+
143
+ def test_to_hash
144
+ aa # raise NameError
145
+ rescue => e
146
+ assert_equal e.to_hash['type'], 'NameError'
147
+ assert_equal e.to_hash['status'], 999999
148
+ assert_equal e.to_hash['http_status'], 500
149
+ assert e.to_hash['message'] =~ /undefined local variable or method `aa'/
150
+ end
151
+
152
+ def test_descriptions
153
+ raise SampleError
154
+ rescue => e
155
+ e.descriptions.merge!(a: 1)
156
+ assert_equal e.descriptions['a'], 1
157
+ end
158
+
159
+ class SampleErrorSub < SampleError; end
160
+ class SampleErrorSubSub < SampleErrorSub; end
161
+ SampleError.after_logging(:blah) { @blah = 101 }
162
+ def test_before_logging
163
+ e = SampleErrorSubSub.new(m: 'foo')
164
+ assert !e.after_logging_blocks[:blah].nil?
165
+ e.logging
166
+ assert_equal e.instance_variable_get(:@blah), 101
167
+ end
142
168
  end
143
169
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - buzz jung
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-04 00:00:00.000000000 Z
11
+ date: 2020-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n