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 +4 -4
- data/lib/services/base.rb +0 -5
- data/lib/services/modules/call_logger.rb +22 -11
- data/lib/services/version.rb +1 -1
- data/spec/services/base_spec.rb +16 -0
- data/spec/support/test_services.rb +17 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad10bcd2778a3534fbc648b1dd2610a05037e7ce
|
4
|
+
data.tar.gz: 052a46223424bb49023619f47ba7a5a031ce49c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8ef850720eee935e3009f2926437ae2639e8145cbde40498a616d3fcd813093f894b663b3c3956516f5cc183745fe578f07e1174b5bae7765aa2b0663fc052b
|
7
|
+
data.tar.gz: 1837efa6d130430ef7420b3cd4fdbeb0d9c6936b73840734adc242270aa9b336b4450bde0ceaf13f9c7d8af2121ecfc8e6c223ab0e302de6bdc8091636297aa1
|
data/lib/services/base.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/services/version.rb
CHANGED
data/spec/services/base_spec.rb
CHANGED
@@ -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
|