services 0.2.1 → 0.2.2

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
  SHA1:
3
- metadata.gz: 5351a6064b1da2a70dd0ce2fba47810bd7deb0f5
4
- data.tar.gz: d73f3b3835195a720b4c370d15a67a017ca49a2e
3
+ metadata.gz: ad10bcd2778a3534fbc648b1dd2610a05037e7ce
4
+ data.tar.gz: 052a46223424bb49023619f47ba7a5a031ce49c4
5
5
  SHA512:
6
- metadata.gz: cdeb74e291c384e338a04acf1c0036788a84bf87813ff25cae0c19b83d3157cb2b749fb09949f4f14a413fe6e90d6c1045516f86f776c176c45a52bbb147e310
7
- data.tar.gz: 1ef404e5414c1fb352336b038861884404fac189dece2ce97d2261b81aee00453341e64ad85f1cb256e79e2adffcc0759ca9758de3aa2d1772877a10dd03d23c
6
+ metadata.gz: b8ef850720eee935e3009f2926437ae2639e8145cbde40498a616d3fcd813093f894b663b3c3956516f5cc183745fe578f07e1174b5bae7765aa2b0663fc052b
7
+ data.tar.gz: 1837efa6d130430ef7420b3cd4fdbeb0d9c6936b73840734adc242270aa9b336b4450bde0ceaf13f9c7d8af2121ecfc8e6c223ab0e302de6bdc8091636297aa1
@@ -17,7 +17,6 @@ module Services
17
17
 
18
18
  def initialize
19
19
  @id = SecureRandom.hex(6)
20
- @logger = Logger.new
21
20
  end
22
21
 
23
22
  def call(*args)
@@ -49,10 +48,6 @@ module Services
49
48
  end
50
49
  end
51
50
 
52
- def log(message, severity = :info)
53
- @logger.log [self.class, @id], message, severity
54
- end
55
-
56
51
  def controller
57
52
  @controller ||= begin
58
53
  raise 'Please configure host.' if Services.configuration.host.nil?
@@ -7,23 +7,34 @@ module Services
7
7
  begin
8
8
  result = super
9
9
  rescue StandardError => e
10
- log "#{e.class}: #{e.message}"
11
- if e.respond_to?(:cause) && !e.cause.nil?
12
- log "caused by: #{e.cause.class}: #{e.cause.message}"
13
- e.cause.backtrace.each do |line|
14
- log line
15
- end
16
- else
17
- e.backtrace.each do |line|
18
- log line
19
- end
20
- end
10
+ log_exception e
21
11
  raise e
22
12
  ensure
23
13
  log "END after #{(Time.now - start).round(2)} seconds"
24
14
  result
25
15
  end
26
16
  end
17
+
18
+ private
19
+
20
+ def log(message, severity = :info)
21
+ @logger ||= Logger.new
22
+ @logger.log [self.class, @id], message, severity
23
+ end
24
+
25
+ def log_exception(e, cause = false)
26
+ log "#{'caused by: ' if cause}#{e.class}: #{e.message}"
27
+ if e.respond_to?(:cause) && !e.cause.nil?
28
+ e.backtrace.take(5).each do |line|
29
+ log " #{line}"
30
+ end
31
+ log_exception(e.cause, true)
32
+ else
33
+ e.backtrace.each do |line|
34
+ log " #{line}"
35
+ end
36
+ end
37
+ end
27
38
  end
28
39
  end
29
40
  end
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
@@ -89,6 +89,22 @@ describe Services::Base do
89
89
  end
90
90
  end
91
91
 
92
+ context 'logging exceptions' do
93
+ if RUBY_VERSION > '2.1'
94
+ it 'logs the exception cause(s) as well' do
95
+ service = NestedExceptionService.new
96
+ logs = []
97
+ allow(service).to receive(:log) do |message, *|
98
+ logs << message
99
+ end
100
+ expect { service.call }.to raise_error(NestedExceptionService::Error)
101
+ %w(NestedError1 NestedError2).each do |error|
102
+ expect(logs).to include(/\Acaused by: NestedExceptionService::#{error}/)
103
+ end
104
+ end
105
+ end
106
+ end
107
+
92
108
  context 'when executed asynchronously' do
93
109
  it 'finds its own worker' do
94
110
  3.times { OwnWorkerService.perform_async }
@@ -39,3 +39,20 @@ class SiblingWorkersService < Services::Base
39
39
  sleep 0.5
40
40
  end
41
41
  end
42
+
43
+ class NestedExceptionService < Services::Base
44
+ NestedError1 = Class.new(Error)
45
+ NestedError2 = Class.new(Error)
46
+
47
+ def call
48
+ begin
49
+ begin
50
+ raise NestedError2
51
+ rescue NestedError2
52
+ raise NestedError1
53
+ end
54
+ rescue NestedError1
55
+ raise Error
56
+ end
57
+ end
58
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: services
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Meurer