openhab-scripting 2.25.2 → 2.26.0

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: f3deb9da52443a92a787b19c30a5e540bf45ffa1f2ce69e7b47554190468e311
4
- data.tar.gz: eae005970cf7f0c47f896fa9e2ab194c1a8c23b8901b535cde8b66efcc864e1c
3
+ metadata.gz: ad2fe8201d5bb1fb3a8255e8b371f3413829dcf1218af8448d31fc5e0eab5c82
4
+ data.tar.gz: fed6a6f164eea7897f13e8bc491af2875ec7317eaf727157268c6c9a966bcc7c
5
5
  SHA512:
6
- metadata.gz: 5fc8e075065e222e388a0c8b83a53644ea0b6d909f7c7c1109450f4be535a91e77319fa4d73e950df6ad9023bfc8051a242dcafcf51a1ff96934c6a43a9a8b51
7
- data.tar.gz: b869685445af147f35ce27a793c8f533fdc39af25842935cd1c3265610383d0332830fae97edb4746a49dc96fd7f351debcb7e1eb28646c174dff03cef5e1c7f
6
+ metadata.gz: 5002bb1c6e24fd9b42ff90baa7c752a1e40df5a845754f2dc911b5afbc535b15f523a1bd5ca3fcb885b3d0a1a348b1a804adcab8b226ee9ba1697ca6a9cff36a
7
+ data.tar.gz: df3ed1db10d9f660682e6cc600019546dbc4fb7b860cefcf27e8c74c2381dd183c8afd0e80aeafa628af8400cba4af01e1f380ebd18c7b0fba5e5068ea4e7213
@@ -255,13 +255,28 @@ module OpenHAB
255
255
  event = inputs&.dig('event')
256
256
 
257
257
  while (task = run_queue.shift)
258
- case task
259
- when RuleConfig::Run then process_run_task(event, task)
260
- when RuleConfig::Trigger then process_trigger_task(event, task)
261
- when RuleConfig::Delay then process_delay_task(inputs, mod, run_queue, task)
262
- when RuleConfig::Otherwise then process_otherwise_task(event, task)
258
+ if task.is_a? RuleConfig::Delay
259
+ process_delay_task(inputs, mod, run_queue, task)
260
+ else
261
+ process_task(event, task)
263
262
  end
264
263
  end
264
+ rescue StandardError => e
265
+ print_backtrace(e)
266
+ end
267
+
268
+ #
269
+ # Dispatch execution block tasks to different methods
270
+ #
271
+ # @param [OpenHab Event] event that triggered the rule
272
+ # @param [Task] task task containing otherwise block to execute
273
+ #
274
+ def process_task(event, task)
275
+ case task
276
+ when RuleConfig::Run then process_run_task(event, task)
277
+ when RuleConfig::Trigger then process_trigger_task(event, task)
278
+ when RuleConfig::Otherwise then process_otherwise_task(event, task)
279
+ end
265
280
  end
266
281
 
267
282
  #
@@ -316,6 +331,16 @@ module OpenHAB
316
331
  task.block.call(event)
317
332
  end
318
333
 
334
+ #
335
+ # Print error and stack trace without calls to internal classes
336
+ #
337
+ # @param [Exception] error A rescued error
338
+ #
339
+ def print_backtrace(error)
340
+ error = logger.clean_backtrace(error)
341
+ logger.error { "#{error.message} (#{error.class})\nIn rule: #{name}\n#{error.backtrace&.join("\n")}" }
342
+ end
343
+
319
344
  #
320
345
  # Create a new hash in which all elements are converted to strings
321
346
  #
@@ -27,6 +27,8 @@ module OpenHAB
27
27
  config.guard = Guard::Guard.new(only_if: config.only_if, not_if: config.not_if)
28
28
  logger.trace { config.inspect }
29
29
  process_rule_config(config)
30
+ rescue StandardError => e
31
+ re_raise_with_backtrace(e)
30
32
  end
31
33
 
32
34
  #
@@ -44,6 +46,16 @@ module OpenHAB
44
46
 
45
47
  private
46
48
 
49
+ #
50
+ # Re-raises a rescued error to OpenHAB with added rule name and stack trace
51
+ #
52
+ # @param [Exception] error A rescued error
53
+ #
54
+ def re_raise_with_backtrace(error)
55
+ error = logger.clean_backtrace(error)
56
+ raise error, "#{error.message}\nIn rule: #{@rule_name}\n#{error.backtrace.join("\n")}"
57
+ end
58
+
47
59
  #
48
60
  # Process a rule based on the supplied configuration
49
61
  #
@@ -16,7 +16,14 @@ module OpenHAB
16
16
  java_import org.slf4j.LoggerFactory
17
17
 
18
18
  # @return [Array] Supported logging levels
19
- LEVELS = %i[TRACE DEBUG WARN INFO ERROR].freeze
19
+ LEVELS = %i[trace debug warn info error].freeze
20
+ private_constant :LEVELS
21
+
22
+ #
23
+ # Regex for matching internal calls in a stack trace
24
+ #
25
+ INTERNAL_CALL_REGEX = %r{(openhab-scripting-.*/lib)|(org/jruby/)}.freeze
26
+ private_constant :INTERNAL_CALL_REGEX
20
27
 
21
28
  #
22
29
  # Create a new logger
@@ -32,11 +39,30 @@ module OpenHAB
32
39
  # def <level>(msg=nil, &block)
33
40
  # log(severity: <level>, msg: msg, &block)
34
41
  # end
42
+ #
43
+ # Also creates methods to check if the different logging levels are enabled
44
+ #
35
45
  LEVELS.each do |level|
36
- method = level.to_s.downcase
37
- define_method(method.to_s) do |msg = nil, &block|
46
+ define_method(level) do |msg = nil, &block|
38
47
  log(severity: level, msg: msg, &block)
39
48
  end
49
+ define_method("#{level}_enabled?") { @sl4fj_logger.send("is_#{level}_enabled") }
50
+ end
51
+
52
+ #
53
+ # Cleans the backtrace of an error to remove internal calls. If logging is set
54
+ # to debug or lower, the full backtrace is kept
55
+ #
56
+ # @param [Exception] error An exception to be cleaned
57
+ #
58
+ # @return [Exception] the exception, potentially with a cleaned backtrace.
59
+ #
60
+ def clean_backtrace(error)
61
+ return error if debug_enabled?
62
+
63
+ backtrace = error.backtrace_locations.reject { |line| INTERNAL_CALL_REGEX.match? line.to_s }
64
+ error.set_backtrace(backtrace.map(&:to_s))
65
+ error
40
66
  end
41
67
 
42
68
  private
@@ -54,7 +80,7 @@ module OpenHAB
54
80
  raise ArgumentError, "Unknown Severity #{severity}" unless LEVELS.include? severity
55
81
 
56
82
  # Dynamically check enablement of underlying logger, this expands to "is_<level>_enabled"
57
- return unless @sl4fj_logger.send("is_#{severity.to_s.downcase}_enabled")
83
+ return unless send("#{severity}_enabled?")
58
84
 
59
85
  # Process block if no message provided
60
86
  msg = yield if msg.nil? && block_given?
@@ -62,7 +88,7 @@ module OpenHAB
62
88
  msg = message_to_string(msg: msg)
63
89
 
64
90
  # Dynamically invoke underlying logger, this expands to "<level>(message)"
65
- @sl4fj_logger.send(severity.to_s.downcase, msg)
91
+ @sl4fj_logger.send(severity, msg)
66
92
  end
67
93
 
68
94
  #
@@ -121,7 +147,7 @@ module OpenHAB
121
147
  configure_logger_for(classname)
122
148
  end
123
149
 
124
- private
150
+ private
125
151
 
126
152
  #
127
153
  # Configure a logger for the supplied classname
@@ -153,7 +179,7 @@ module OpenHAB
153
179
  .first
154
180
  .yield_self { |caller| File.basename(caller, '.*') }
155
181
  end
156
- end
182
+ end
157
183
 
158
184
  #
159
185
  # Add logger method to the object that includes this module
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '2.25.2'
8
+ VERSION = '2.26.0'
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.25.2
4
+ version: 2.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-28 00:00:00.000000000 Z
11
+ date: 2021-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler