augmented 0.2.4 → 0.2.5

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: 7614af557970d760bbf22bb29e09ba552d62da96b7980ebddaf0677ca2639be7
4
- data.tar.gz: 41f453b5c5b095ab76b48ddc033db840884598fe4e78c3f56dd76dcac4464cfe
3
+ metadata.gz: 02a3d4fd8fbc13022569168b89c6fb60b575dea7030a6c36b1457a049c468b62
4
+ data.tar.gz: bb89fe218a192cb1eb72f9456988a65c08eec651fc6e3f89e62941e9632dac9a
5
5
  SHA512:
6
- metadata.gz: 6c2622e88196a3746e3c344a44329dfbae27c5b9abf9f49eff8862b33faf1c53548df07fd4f61f8087e3599e065c501da0740f1fae6cd88239b0b70dee763c51
7
- data.tar.gz: bf2aa41adb4d32fdd97ac28a1694916ef175a395e3feb735ce48567f6b50de0130ca6ec2bc64d2c4e0622f542b9c68cdac28063fa9462dd7f74bf58809042926
6
+ metadata.gz: c4f04bc92e1bdfbff68e62283bfb16b7f8499297916c208ae62156e84d1358efdf4a94f56cb5d0d0123ea985a7f315126026147b82e338e6fea7d4ddcbff489f
7
+ data.tar.gz: 99d873ac314d8ff5a8cc7bf6f6fc5a47cfb27080dbf69167c2a6f6f0509175e6f7cd4c6ab23c2a41a7f73cbd8a5b39cda20c473d1448228dd0316013b7659ae9
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.5] - 2021-05-30
4
+
5
+ - Added `Exception#details`, `Exception#details=`, `Exception#detailed`
6
+ - Added `Exception#chain`
7
+ - Added `Exception#to_h`
8
+
3
9
  ## [0.2.3] - 2021-05-29
4
10
 
5
11
  - Added `String#truncate`, `String#truncate!` and `String#blank?`
data/README.md CHANGED
@@ -31,6 +31,7 @@ You can load all refinements for just one type:
31
31
  ```ruby
32
32
  using Augmented::Arrays
33
33
  using Augmented::Enumerators
34
+ using Augmented::Exceptions
34
35
  using Augmented::Hashes
35
36
  using Augmented::Modules
36
37
  using Augmented::Objects
@@ -83,6 +84,81 @@ using Augmented::Enumerators::Indexing
83
84
  ```
84
85
 
85
86
 
87
+ #### `Augmented::Exceptions`
88
+
89
+ ##### `Exception#chain`
90
+
91
+ Returns an enumerator over the exception's causal chain, starting with the exception itself.
92
+
93
+ ```ruby
94
+ using Augmented::Exceptions::Chain
95
+
96
+ begin
97
+ begin
98
+ begin
99
+ raise 'first'
100
+ rescue
101
+ raise 'second'
102
+ end
103
+ rescue
104
+ raise 'third'
105
+ end
106
+ rescue => error
107
+ error.chain.map(&:message)
108
+ end
109
+ # ["third", "second", "first"]
110
+ ```
111
+
112
+
113
+ ##### `Exception#details`, `Exception#details=`, `Exception#detailed`
114
+
115
+ Attach a hash of details to any exception.
116
+
117
+ ```ruby
118
+ using Augmented::Exceptions::Detailed
119
+
120
+ exception = RuntimeError.new('oops!').detailed(foo: 10, bar: { baz: 30 })
121
+ exception.details
122
+ # {:foo=>10, :bar=>{:baz=>30}}
123
+ exception.details = { bam: 40 }
124
+ exception.details
125
+ # {:bam=>40}
126
+ ```
127
+
128
+
129
+ ##### `Exception#to_h`
130
+
131
+ Serializes an exception into a Hash including its backtrace, details and causal chain.
132
+
133
+ ```ruby
134
+ using Augmented::Exceptions::Serializable
135
+ using Augmented::Exceptions::Detailed
136
+
137
+ begin
138
+ begin
139
+ raise RuntimeError.new('first').detailed(foo: 10)
140
+ rescue
141
+ raise RuntimeError.new('second').detailed(bar: 20)
142
+ end
143
+ rescue => error
144
+ error.to_h
145
+ end
146
+ # {
147
+ # :class => "RuntimeError",
148
+ # :message => "second",
149
+ # :details => { :bar => 20 },
150
+ # :backtrace => [ ... ],
151
+ # :cause => {
152
+ # :class => "RuntimeError",
153
+ # :message => "first",
154
+ # :details => { :foo => 10 },
155
+ # :backtrace => [ ... ],
156
+ # :cause => nil
157
+ # }
158
+ # }
159
+ ```
160
+
161
+
86
162
  #### `Augmented::Hashes`
87
163
 
88
164
  ##### `Hash#map_values`
data/lib/augmented.rb CHANGED
@@ -2,6 +2,7 @@ require 'augmented/version'
2
2
 
3
3
  require 'augmented/arrays'
4
4
  require 'augmented/enumerators'
5
+ require 'augmented/exceptions'
5
6
  require 'augmented/hashes'
6
7
  require 'augmented/modules'
7
8
  require 'augmented/objects'
@@ -12,6 +13,7 @@ require 'augmented/symbols'
12
13
  module Augmented
13
14
  include Arrays
14
15
  include Enumerators
16
+ include Exceptions
15
17
  include Hashes
16
18
  include Modules
17
19
  include Objects
@@ -0,0 +1,11 @@
1
+ require 'augmented/exceptions/chain'
2
+ require 'augmented/exceptions/detailed'
3
+ require 'augmented/exceptions/serializable'
4
+
5
+ module Augmented
6
+ module Exceptions
7
+ include Chain
8
+ include Detailed
9
+ include Serializable
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ module Augmented
2
+ module Exceptions
3
+ module Chain
4
+ refine Exception do
5
+
6
+ def chain
7
+ Enumerator.new do |yielder|
8
+ yielder << exception = self
9
+ yielder << exception while exception = exception.cause
10
+ end
11
+ end
12
+
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ module Augmented
2
+ module Exceptions
3
+ module Detailed
4
+ refine Exception do
5
+
6
+ def details
7
+ @_details ||= {}
8
+ end
9
+
10
+ def details= **details
11
+ @_details = details
12
+ end
13
+
14
+ def detailed **details
15
+ self.details = details
16
+ self
17
+ end
18
+
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ module Augmented
2
+ module Exceptions
3
+ module Serializable
4
+ refine Exception do
5
+ using Chain
6
+ using Detailed
7
+
8
+ def to_h
9
+ self.chain.map do |exception|
10
+ {
11
+ class: exception.class.name,
12
+ message: exception.message,
13
+ details: exception.details,
14
+ backtrace: exception.backtrace || [],
15
+ cause: nil,
16
+ }
17
+ end.reverse.reduce do |cause, exception|
18
+ exception.merge!(cause: cause)
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Augmented
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: augmented
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - brunze
@@ -57,6 +57,10 @@ files:
57
57
  - lib/augmented/arrays/tieable.rb
58
58
  - lib/augmented/enumerators.rb
59
59
  - lib/augmented/enumerators/indexing.rb
60
+ - lib/augmented/exceptions.rb
61
+ - lib/augmented/exceptions/chain.rb
62
+ - lib/augmented/exceptions/detailed.rb
63
+ - lib/augmented/exceptions/serializable.rb
60
64
  - lib/augmented/hashes.rb
61
65
  - lib/augmented/hashes/mappable.rb
62
66
  - lib/augmented/hashes/polymorphable.rb