observers 0.9.0 → 1.0.0

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: fbb9116b5b3a86d777d4aca13b5c4c04214340596e1df83adebada4dd871bc43
4
- data.tar.gz: 560fbbb9480ac2968036b8024e9362c12ea68994c77a587abc2432505ceeda7f
3
+ metadata.gz: 1baa5ca1c0866fe6e708bc669db53424411099c44a7747cbe441f3e50191b3b8
4
+ data.tar.gz: 361100827e869fa95814d94ae4789637bb7b794d3e55063414adc647683e47e9
5
5
  SHA512:
6
- metadata.gz: 403fe656fda85237d897b4e82b84e9b770f352807ac5ef05a37f4c1d58784af7f43e5f22d6ab5999bdebeea831350757125f184870de45ad78330efc6fe07ace
7
- data.tar.gz: '06680f408eaf15377c441fd995a7a3b05b3a41bcf6b8e3e3aa87dd06a6e65227f43a50b01c03193845d26d306e368b2ecfaec814af0cb8f4ba9a34386641af83'
6
+ metadata.gz: 319423e0cd9b9cc4b2c68dd5887eef9c0cae72102957d79bddb86e13f18dcc369fdb5b5d361d88c5b27fb1503c80883c2c3692a50fb847c01b3caa206a622a07
7
+ data.tar.gz: afcfe235cc45aad95d657d3da28c118d116d767cda8f24790c7c99467cfc660493c18896eb0b0dcda893a0117b45b0e74b80ec290439a79e16821e54b0183811
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'observer_error'
4
+
3
5
  module Observers
4
6
  class Observer
5
7
  attr_reader :object, :action
@@ -20,15 +22,10 @@ module Observers
20
22
  else
21
23
  @object.send(action)
22
24
  end
23
- rescue ArgumentError => e
24
- type = @object.instance_of?(Class) ? @object : @object.class
25
- method_type = @object.instance_of?(Class) ? '.' : '#'
26
-
27
- raise ArgumentError, "#{type}##{action} has an 'event:' keyword argument but no event arg was sent" if event.nil?
28
-
29
- # Events trigger events, so the error bubbles up to become the error message for the next rescue's error message:
30
- # "RequestEvent sent to Rain::Router#route_request -> StatusEvent sent to Error404Node.render -> unknown keyword: :props"
31
- raise ArgumentError, "#{event.class} sent to #{type}#{method_type}#{action} -> #{e.message}"
25
+ rescue StandardError => e
26
+ # Events trigger events, so this error bubbles up to become the error for the next rescue:
27
+ # "RequestEvent -> Rain::Router#route_request -> StatusEvent -> Error404Node.render -> unknown keyword: :props"
28
+ raise ObserverError.new(e.message, object:, event:, action:, previous_error: e)
32
29
  end
33
30
  end
34
31
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Observers
4
+ # Map the events and actions along the way when showing an exception.
5
+ # Any exceptions in this class itself will fail silently as they will error out into itself.
6
+ class ObserverError < StandardError
7
+ def initialize(message, object:, event:, action:, previous_error:)
8
+ super(message)
9
+
10
+ @event = event
11
+ @object = object
12
+ @action = action
13
+ @previous_error = previous_error
14
+ end
15
+
16
+ def link
17
+ class_type = @object.instance_of?(Class) ? @object : @object.class
18
+ method_type = @object.instance_of?(Class) ? '.' : '#'
19
+ "#{@event.class} -> #{class_type}#{method_type}#{@action}"
20
+ end
21
+
22
+ def to_s
23
+ chain = [link]
24
+ first_error = first_error(@previous_error, chain:)
25
+ error_location = first_error.backtrace.first
26
+
27
+ "#{chain.join(' -> ')} -> #{first_error.message}\n#{error_location}"
28
+ end
29
+
30
+ private
31
+
32
+ def first_error(error, chain:)
33
+ chain << error.link if error.respond_to?(:link)
34
+
35
+ return error unless error.respond_to?(:previous_error)
36
+
37
+ first_error(error.previous_error, chain:)
38
+ end
39
+ end
40
+ end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Observers
4
- VERSION = '0.9.0'
4
+ VERSION = '1.0.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: observers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
@@ -19,6 +19,7 @@ files:
19
19
  - lib/keys.rb
20
20
  - lib/models/key.rb
21
21
  - lib/models/observer.rb
22
+ - lib/models/observer_error.rb
22
23
  - lib/observers.rb
23
24
  - lib/version.rb
24
25
  homepage: https://github.com/raindeer-rb/observers