escalate 0.2.0.pre.2 → 0.2.0.pre.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e6dc6ba580b590c324c351001687805582d7a2d757cb7aebeaed1a6ee50d07b
4
- data.tar.gz: '02439935ba7a19a8c68b5ff0adcad892d658b29a2f843311a79a798fa090fac2'
3
+ metadata.gz: 67b615521fd3e15a59bb8401486496175afa19be36a98c415d9eccf52dd0815f
4
+ data.tar.gz: 951d5afc0052cde73fca383e23dc38aa63e759fce7f4863b2093e9bf9b2463b4
5
5
  SHA512:
6
- metadata.gz: 39d9cb26ed59b9eca7f0a234ed2ead5375ae2144128098b6cd74bbabe54cdd6281278baaf12cf186a4873c9bf50092f5440e4e10931da33f606bca344f76edd6
7
- data.tar.gz: d9608ff9899f52a7a99a2e26937340b3909d4715e9f5dbd68bdb7474fbe2782c7419f277ef3ab3d21d931a4fa9ad656b877929796707003fb01b771e8910370f
6
+ metadata.gz: 7979594ec79d527e123efb08114d602ab11446d8e822a9a716b582150d7e0f4b97840443974270177d861d874fbf4544f7e14eb8bd5e98e66acc86d49b51e19a
7
+ data.tar.gz: 2ab19d1367350559290fb9603e7af73934fcf1085121bf5a1cebe03059d450d19e53f76dc9947d92fac8a06f2a08fc0bb6ed12d0135a1e09617c87c6835dc158
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- escalate (0.2.0.pre.2)
4
+ escalate (0.2.0.pre.3)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -58,8 +58,34 @@ end
58
58
  When `SomeGem.escalate` above is triggered, it will use the logger returned by `SomeGem.logger` or
59
59
  default to a `STDERR` logger and do the following:
60
60
 
61
- 1. Log an error containing the exception and any additional information about the current environment that is specified
62
- 2. Trigger any `escalation_callbacks` configured on the `Escalate` gem
61
+ 1. [optional] Log an error containing the exception, location_message, and context hash
62
+ 2. Trigger any `on_escalate_callbacks` configured on the `Escalate` gem
63
+
64
+ Step (1) is optional. It will happen if either of these is true:
65
+ - by default if no `on_escalate_callbacks` have been registered; or
66
+ - if any of the `on_escalate_callbacks` was registered with `on_escalate(log_first: true)`.
67
+
68
+ ### Registering an Escalate Callback
69
+
70
+ If you are using an error reporting service, you can register an `on_escalate` callback to escalate exceptions.
71
+ You have the option to handle logging yourself, or to let `escalate` log first, before calling your callback.
72
+
73
+ #### Leave the Logging to the Escalate Gem
74
+ Here is an example that uses the default `log_first: true` so that logging is handled by the `Escalate` gem first:
75
+ ```
76
+ Escalate.on_escalate do |exception, location_message, **context|
77
+ # send exception, location_message, **context to the error reporting service here
78
+ end
79
+ ```
80
+
81
+ #### Handle the Logging in the `on_escalate` Callback
82
+ Here is an example that handles logging itself with `log_first: false`:
83
+ ```
84
+ Escalate.on_escalate(log_first: false) do |exception, location_message, **context|
85
+ # log here first
86
+ # send exception, location_message, **context to the error reporting service here
87
+ end
88
+ ```
63
89
  ## Development
64
90
 
65
91
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/escalate.rb CHANGED
@@ -11,7 +11,11 @@ module Escalate
11
11
 
12
12
  LOG_FIRST_INSTANCE_VARIABLE = :@_escalate_log_first
13
13
 
14
+ @on_escalate_callbacks = Set.new
15
+
14
16
  class << self
17
+ attr_reader :on_escalate_callbacks
18
+
15
19
  # Logs and escalated an exception
16
20
  #
17
21
  # @param [Exception] exception
@@ -27,21 +31,22 @@ module Escalate
27
31
  # Any additional context to be tied to the escalation
28
32
  def escalate(exception, location_message, logger, **context)
29
33
  ensure_failsafe("Exception rescued while escalating #{exception.inspect}") do
30
- if on_escalate_blocks.none? || on_escalate_blocks.any? { |block| block.instance_variable_get(LOG_FIRST_INSTANCE_VARIABLE) }
34
+ if on_escalate_callbacks.none? || on_escalate_callbacks.any? { |block| block.instance_variable_get(LOG_FIRST_INSTANCE_VARIABLE) }
35
+ logger_allows_added_context?(logger) or context_string = " (#{context.inspect})"
31
36
  error_message = <<~EOS
32
- [Escalate] #{location_message} (#{context.inspect})
37
+ [Escalate] #{location_message}#{context_string}
33
38
  #{exception.class.name}: #{exception.message}
34
39
  #{exception.backtrace.join("\n")}
35
40
  EOS
36
41
 
37
- if logger_allows_added_context?(logger)
38
- logger.error(error_message, **context)
39
- else
42
+ if context_string
40
43
  logger.error(error_message)
44
+ else
45
+ logger.error(error_message, **context)
41
46
  end
42
47
  end
43
48
 
44
- on_escalate_blocks.each do |block|
49
+ on_escalate_callbacks.each do |block|
45
50
  ensure_failsafe("Exception rescued while escalating #{exception.inspect} to #{block.inspect}") do
46
51
  block.call(exception, location_message, **context)
47
52
  end
@@ -90,11 +95,11 @@ module Escalate
90
95
  # whether escalate should log first before escalating, or leave the logging to the escalate block
91
96
  def on_escalate(log_first: true, &block)
92
97
  block.instance_variable_set(LOG_FIRST_INSTANCE_VARIABLE, log_first)
93
- on_escalate_blocks.add(block)
98
+ on_escalate_callbacks.add(block)
94
99
  end
95
100
 
96
101
  def clear_on_escalate_callbacks
97
- on_escalate_blocks.clear
102
+ on_escalate_callbacks.clear
98
103
  end
99
104
 
100
105
  private
@@ -109,9 +114,5 @@ module Escalate
109
114
  defined?(ContextualLogger::LoggerMixin) &&
110
115
  logger.is_a?(ContextualLogger::LoggerMixin)
111
116
  end
112
-
113
- def on_escalate_blocks
114
- @on_escalate_blocks ||= Set.new
115
- end
116
117
  end
117
118
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Escalate
4
- VERSION = "0.2.0.pre.2"
4
+ VERSION = "0.2.0.pre.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: escalate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.pre.2
4
+ version: 0.2.0.pre.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Invoca Development
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-02-25 00:00:00.000000000 Z
12
+ date: 2021-03-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport