startback 0.10.0 → 0.11.3

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: 10937b9e7877dd7393837c70185fe245493a35234d66a899efee5fbebb539b75
4
- data.tar.gz: 5fd7234b34b09f875dcf9b21ad819eb5072ffed902ddd08ca6c2e6a68e3ec1df
3
+ metadata.gz: 83733eb966d9d9679a15840dfa76f717aacaacc5c3eb417c6bf518a3eea1e3cd
4
+ data.tar.gz: f13815553606f4567e136beb86fc19a066720a3bcdf520c3781c12a732808f47
5
5
  SHA512:
6
- metadata.gz: 37df032165dd22b0680f8eaf874bada4810707253e06677c6a80b5d21afc87062643143e5f2b50e0f3adbe48d9df00368bb5f7070068e8458565e0d0fb8f1112
7
- data.tar.gz: 754c4703f78bca862a4a7b2f8d973609ec44fab70368fe6d6d371050c140592a5706720ef0208ac4002ce434d85ad8e4d095d07840cd7993e2cb698cd116da5d
6
+ metadata.gz: 0a2c80c7c65fbe845784a6ee1326bed2a1d6f89eed591580800e1ee9f403fea24b61e92b1f1ad13356f612308a30c6ecc17f6c6a16fadb9c82ae6dec61b68d31
7
+ data.tar.gz: 18dc6c715ba6ed1715c6d838421476959942611a120ba04eed7a72d40d505e53c60b64ecbf4e877d2a99d2c46300cfa8e83f6cb70960bb72ad599d7d5802509f
@@ -1,3 +1,4 @@
1
+ require_relative 'shared'
1
2
  require 'prometheus/client'
2
3
 
3
4
  module Startback
@@ -23,6 +24,7 @@ module Startback
23
24
  # input at construction time.
24
25
  #
25
26
  class Prometheus
27
+ include Shared
26
28
 
27
29
  def initialize(options = {})
28
30
  @prefix = options[:prefix] || "startback"
@@ -62,7 +64,6 @@ module Startback
62
64
  def ignore_safely
63
65
  yield
64
66
  rescue => ex
65
- puts ex.class.to_s + "\n" + ex.message + "\n" + ex.backtrace.join("\n")
66
67
  nil
67
68
  end
68
69
 
@@ -81,14 +82,6 @@ module Startback
81
82
  Startback::VERSION
82
83
  end
83
84
 
84
- def op_name(op)
85
- case op
86
- when String then op
87
- when Class then op.name
88
- else op.class.name
89
- end
90
- end
91
-
92
85
  end # class Prometheus
93
86
  end # module Audit
94
87
  end # module Startback
@@ -0,0 +1,17 @@
1
+ module Startback
2
+ module Audit
3
+ module Shared
4
+
5
+ def op_name(op)
6
+ return op.op_name if op.respond_to?(:op_name)
7
+
8
+ case op
9
+ when String then op
10
+ when Class then op.name
11
+ else op.class.name
12
+ end
13
+ end
14
+
15
+ end # module Shared
16
+ end # module Audit
17
+ end # module Startback
@@ -1,3 +1,4 @@
1
+ require_relative 'shared'
1
2
  require 'forwardable'
2
3
  module Startback
3
4
  module Audit
@@ -45,6 +46,7 @@ module Startback
45
46
  # input at construction time.
46
47
  #
47
48
  class Trailer
49
+ include Shared
48
50
  extend Forwardable
49
51
  def_delegators :@logger, :debug, :info, :warn, :error, :fatal
50
52
 
@@ -85,20 +87,14 @@ module Startback
85
87
  log_msg
86
88
  end
87
89
 
88
- def op_name(op)
89
- case op
90
- when String then op
91
- when Class then op.name
92
- else op.class.name
93
- end
94
- end
95
-
96
90
  def op_context(op)
97
91
  sanitize(op.respond_to?(:context, false) ? op.context.to_h : {})
98
92
  end
99
93
 
100
94
  def op_data(op)
101
- data = if op.respond_to?(:to_trail, false)
95
+ data = if op.respond_to?(:op_data, false)
96
+ op.op_data
97
+ elsif op.respond_to?(:to_trail, false)
102
98
  op.to_trail
103
99
  elsif op.respond_to?(:input, false)
104
100
  op.input
@@ -1,2 +1,3 @@
1
+ require_relative 'audit/shared'
1
2
  require_relative 'audit/trailer'
2
3
  require_relative 'audit/prometheus'
@@ -2,8 +2,13 @@ module Startback
2
2
  module Errors
3
3
 
4
4
  class Error < StandardError
5
- class << self
5
+ def initialize(message = nil, causes = nil)
6
+ super(message)
7
+ @causes = Array(causes)
8
+ end
9
+ attr_reader :causes
6
10
 
11
+ class << self
7
12
  def status(code = nil)
8
13
  if code.nil?
9
14
  @code || (superclass.respond_to?(:status) ? superclass.status : 500)
@@ -22,11 +27,19 @@ module Startback
22
27
  msg = super
23
28
  return msg unless msg == self.class.name
24
29
  parts = self.class.name.split('::').last.gsub(/[A-Z]/){|x|
25
- " #{x.downcase}"
30
+ " #{x.downcase}"
26
31
  }.strip.split(" ")
27
32
  parts = parts[0...-1] unless self.class.keep_error
28
33
  parts.join(" ").capitalize
29
34
  end
35
+
36
+ def has_causes?
37
+ causes && !causes.empty?
38
+ end
39
+
40
+ def cause
41
+ causes&.first
42
+ end
30
43
  end
31
44
 
32
45
  class BadRequestError < Error
@@ -3,14 +3,28 @@ module Startback
3
3
  class LogFormatter
4
4
 
5
5
  def call(severity, time, progname, msg)
6
- if msg[:error] && msg[:error].respond_to?(:message, true)
7
- msg[:backtrace] = msg[:error].backtrace[0..25] if severity == "FATAL"
8
- msg[:error] = msg[:error].message
9
- end
10
6
  {
11
7
  severity: severity,
12
- time: time,
13
- }.merge(msg).to_json << "\n"
8
+ time: time
9
+ }.merge(msg)
10
+ .merge(error: error_to_json(msg[:error], severity))
11
+ .compact
12
+ .to_json << "\n"
13
+ end
14
+
15
+ def error_to_json(error, severity = nil)
16
+ return error if error.nil?
17
+ return error if error.is_a?(String)
18
+ return error.to_s unless error.is_a?(Exception)
19
+
20
+ backtrace = error.backtrace[0..25] if severity == "FATAL"
21
+ causes = error.causes.map{|c| error_to_json(c) } if error.respond_to?(:causes)
22
+ causes = nil if causes && causes.empty?
23
+ {
24
+ message: error.message,
25
+ backtrace: backtrace,
26
+ causes: causes
27
+ }.compact
14
28
  end
15
29
 
16
30
  end # class LogFormatter
@@ -1,8 +1,8 @@
1
1
  module Startback
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 10
5
- TINY = 0
4
+ MINOR = 11
5
+ TINY = 3
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
@@ -6,6 +6,7 @@ module Startback
6
6
 
7
7
  set :raise_errors, true
8
8
  set :show_exceptions, false
9
+ set :dump_errors, false
9
10
 
10
11
  protected
11
12
 
@@ -34,10 +34,25 @@ module Startback
34
34
  # A bit of logic to choose the best error message for the user
35
35
  # according to the error class
36
36
  self.body{|ex|
37
- ex = ex.root_cause if ex.is_a?(Finitio::TypeError)
38
- { code: ex.class.name, description: ex.message }.to_json
37
+ body_for(ex).to_json
39
38
  }
40
39
 
40
+ def body_for(ex)
41
+ ex = ex.root_cause if ex.is_a?(Finitio::TypeError)
42
+ body = { code: ex.class.name, description: ex.message }
43
+ return body unless ex.is_a?(Startback::Errors::Error)
44
+ return body unless ex.has_causes?
45
+
46
+ body[:causes] = ex.causes
47
+ .filter{|cause|
48
+ cause.is_a?(Startback::Errors::Error)
49
+ }
50
+ .map{|cause|
51
+ body_for(cause)
52
+ }
53
+ body
54
+ end
55
+
41
56
  end # class Shield
42
57
  end # module Web
43
58
  end # module Startback
@@ -8,13 +8,30 @@ module Startback
8
8
  Trailer.new("/tmp/trail.log")
9
9
  }
10
10
 
11
+ describe "op_name" do
12
+
13
+ def op_name(op, trailer = self.trailer)
14
+ trailer.send(:op_name, op)
15
+ end
16
+
17
+ it 'uses op_name in priority if provided' do
18
+ op = OpenStruct.new(op_name: "foo")
19
+ expect(op_name(op)).to eql("foo")
20
+ end
21
+ end
22
+
11
23
  describe "op_data" do
12
24
 
13
25
  def op_data(op, trailer = self.trailer)
14
26
  trailer.send(:op_data, op)
15
27
  end
16
28
 
17
- it 'uses to_trail in priority if provided' do
29
+ it 'uses op_data in priority if provided' do
30
+ op = OpenStruct.new(op_data: { foo: "bar" }, input: 12, request: 13)
31
+ expect(op_data(op)).to eql({ foo: "bar" })
32
+ end
33
+
34
+ it 'uses to_trail then' do
18
35
  op = OpenStruct.new(to_trail: { foo: "bar" }, input: 12, request: 13)
19
36
  expect(op_data(op)).to eql({ foo: "bar" })
20
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: startback
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-22 00:00:00.000000000 Z
11
+ date: 2022-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -270,20 +270,20 @@ dependencies:
270
270
  requirements:
271
271
  - - ">="
272
272
  - !ruby/object:Gem::Version
273
- version: 0.18.0
273
+ version: 0.19.0
274
274
  - - "<"
275
275
  - !ruby/object:Gem::Version
276
- version: 0.19.0
276
+ version: 0.20.0
277
277
  type: :runtime
278
278
  prerelease: false
279
279
  version_requirements: !ruby/object:Gem::Requirement
280
280
  requirements:
281
281
  - - ">="
282
282
  - !ruby/object:Gem::Version
283
- version: 0.18.0
283
+ version: 0.19.0
284
284
  - - "<"
285
285
  - !ruby/object:Gem::Version
286
- version: 0.19.0
286
+ version: 0.20.0
287
287
  - !ruby/object:Gem::Dependency
288
288
  name: tzinfo
289
289
  requirement: !ruby/object:Gem::Requirement
@@ -383,6 +383,7 @@ files:
383
383
  - lib/startback.rb
384
384
  - lib/startback/audit.rb
385
385
  - lib/startback/audit/prometheus.rb
386
+ - lib/startback/audit/shared.rb
386
387
  - lib/startback/audit/trailer.rb
387
388
  - lib/startback/bus.rb
388
389
  - lib/startback/bus/bunny.rb
@@ -472,7 +473,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
472
473
  - !ruby/object:Gem::Version
473
474
  version: '0'
474
475
  requirements: []
475
- rubygems_version: 3.2.32
476
+ rubygems_version: 3.3.7
476
477
  signing_key:
477
478
  specification_version: 4
478
479
  summary: Got Your Ruby Back