coaster 1.0.2 → 1.0.7

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: 388b2f2f69156f7344ddd198dc1a68a6a41c886db2dc23b881576ca06e48ad80
4
- data.tar.gz: 45e1be2338d2fb185bfa6c7652d0e82a0342a961911503f6af133b0bfbcf6c66
3
+ metadata.gz: ca45b5184489708bdeeb0381f2d540576f1b58cc324b3dca4f5bd698f021c687
4
+ data.tar.gz: 69ab2837c8159634389fefc993503c130bb8e14efcda3a2621646d909a3f9a4f
5
5
  SHA512:
6
- metadata.gz: d35ee060873032921e4c165e529b99314e1115c083c3d616b5250975819c536f47df1b3c0de40272a990414273910f427013e5ff2d2705f3d807c87fd7cbf617
7
- data.tar.gz: ccbd4fca13b51857c45ea9e5772375bc2019ba3cfcb5c730c54981618205a50f6e2f6bbfd2f51c2582977ad61703d19a70cd7ca71a0d4c382c7d5790c15f8c3c
6
+ metadata.gz: 417805608b285316128c95f4aadaa83d26ab9f2666e0273e1ee987baca5b4d346596dd0d1d8de925f7273bf6a4ee3ac057ceaf43c6213874b025e9df51533c5f
7
+ data.tar.gz: 795b2a27aaadf52aad1e6f723397dc13be68e3cd7028ca5c1a982cd9dda6cbe92d688ea4324ed72fd609274993c91c29978bf0d2ecc52bad7b4d6a82c68b4341
@@ -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,25 @@ 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 it_might_happen?; attributes[:it] == :might_happen end
106
+ def it_should_not_happen?; attributes[:it] == :should_not_happen end
107
+ def report?
108
+ return attributes[:report] if attributes.key?(:report)
109
+ return false if it_might_happen?
110
+ self.class.report?
111
+ end
112
+ def intentional? # not logging in test
113
+ return attributes[:intentional] if attributes.key?(:intentional)
114
+ return true if it_should_not_happen?
115
+ self.class.intentional?
116
+ end
117
+ def object; attributes[:object] || attributes[:obj] end
118
+ alias_method :obj, :object
106
119
 
107
120
  # description is user friendly messages, do not use error's message
108
121
  # error message is not user friendly in many cases.
@@ -115,13 +128,11 @@ class StandardError
115
128
  end
116
129
  alias_method :desc, :description
117
130
 
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
131
+ # more user friendly messages
132
+ def descriptions
133
+ return attributes[:descriptions] if attributes[:descriptions]
134
+ attributes[:descriptions] = {}
135
+ attributes[:descriptions]
125
136
  end
126
137
 
127
138
  def to_hash
@@ -191,16 +202,25 @@ class StandardError
191
202
  end
192
203
 
193
204
  def logging(options = {})
194
- logger = options[:logger] || Coaster.logger
195
- logger = Rails.logger if logger.nil? && defined?(Rails)
196
- return nil unless logger
205
+ before_logging_blocks.values.each { |blk| instance_exec &blk }
206
+
207
+ return unless report?
208
+ logger = nil
209
+ if defined?(Rails)
210
+ return if Rails.env.test? && intentional?
211
+ logger = Rails.logger
212
+ end
213
+ logger = options[:logger] || Coaster.logger || logger
214
+ return unless logger
197
215
 
198
216
  cl = options[:cleaner] || cleaner
199
217
  msg = to_detail
200
218
 
201
219
  if cl && backtrace
220
+ bt = cl.clean(backtrace)
221
+ bt = bt[0..2] if intentional?
202
222
  msg += "\tBACKTRACE:\n\t"
203
- msg += cl.clean(backtrace).join("\n\t")
223
+ msg += bt.join("\n\t")
204
224
  end
205
225
 
206
226
  if level && logger.respond_to?(level)
@@ -208,5 +228,7 @@ class StandardError
208
228
  else
209
229
  logger.error(msg)
210
230
  end
231
+
232
+ after_logging_blocks.values.each { |blk| instance_exec &blk }
211
233
  end
212
234
  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.2'
2
+ VERSION = '1.0.7'
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
@@ -148,5 +148,33 @@ LOG
148
148
  assert_equal e.to_hash['http_status'], 500
149
149
  assert e.to_hash['message'] =~ /undefined local variable or method `aa'/
150
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) do
162
+ self.attributes[:abc] = 100
163
+ @blah = 101
164
+ end
165
+ def test_before_logging
166
+ e = SampleErrorSubSub.new(m: 'foo')
167
+ assert !e.after_logging_blocks[:blah].nil?
168
+ e.logging
169
+ assert_equal e.attributes[:abc], 100
170
+ assert_equal e.instance_variable_get(:@blah), 101
171
+ end
172
+ class SampleErrorMightHappen < SampleErrorSub
173
+ def it_might_happen?; true end
174
+ end
175
+ def test_might_happen
176
+ e = SampleErrorMightHappen.new('fbar')
177
+ assert !e.report?
178
+ end
151
179
  end
152
180
  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.2
4
+ version: 1.0.7
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-10 00:00:00.000000000 Z
11
+ date: 2020-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -226,7 +226,7 @@ signing_key:
226
226
  specification_version: 4
227
227
  summary: A little convenient feature for standard library
228
228
  test_files:
229
+ - test/test_object_translation.rb
229
230
  - test/locales/en.yml
230
231
  - test/test_standard_error.rb
231
232
  - test/test_helper.rb
232
- - test/test_object_translation.rb