philiprehberger-try 0.4.0 → 0.5.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: 5b19953b62d7e9a8cf174f7d5d2c20996bcce267bebeb551658354fd0dbfd3e5
4
- data.tar.gz: 23d825ef9a47e0b68394763acaf68301403569c2fcf449596e8068dc8d99465f
3
+ metadata.gz: 0c9450e5d2764d72177933ac79769244a27c0963e89482efa19e6e99b3ed1880
4
+ data.tar.gz: 92bd0ab4f69ba2ae16a1f32e6e80f4ee20cb44cebce09ac59da3621fecbe5a12
5
5
  SHA512:
6
- metadata.gz: c107e212abc0e641e334f5f1a530015ba10fa4c361896f2d003e6218c40cb1a57c41dfe65a14e788412f344764f11ce5e76d134b150c7381fd8163aede0153ef
7
- data.tar.gz: fc69e87c5925b572a26d77fd08824626ccc3eafb4cd80a93cdecaa84b8f0f7c5d59a5f98a42df4f2b87e1b6f11323b9799c62aadbaf3ffa52d4e421b36f82b02
6
+ metadata.gz: 76cf42ba98e17b49a0db796c5dca5d95e82f9538740e28dcb77322d7c784e5631a054ab20952fbcc5729a818b583ab2a6a24bdd6ae2fb2dd1e274eb0e37ccd1e
7
+ data.tar.gz: f958fc1f60467f1ffe1932f79b5b86087725bf9ad76a0602105d10164fb8f3d8db866a4c8f539fd71cc19e7f3130bf00051a4c0025bdde69d41babd50f62dce8
data/CHANGELOG.md CHANGED
@@ -7,6 +7,11 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.5.0] - 2026-05-07
11
+
12
+ ### Added
13
+ - `Success#map_error` and `Failure#map_error` — declaratively transform a `Failure`'s error into a different exception. `Success#map_error` is a no-op. Non-Exception block return values are wrapped in `RuntimeError` to preserve the `Failure#error` invariant.
14
+
10
15
  ## [0.4.0] - 2026-04-09
11
16
 
12
17
  ### Added
@@ -64,6 +69,8 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
64
69
  - `.map` for transforming success values
65
70
  - Optional timeout support
66
71
 
72
+ [0.5.0]: https://github.com/philiprehberger/rb-try/releases/tag/v0.5.0
73
+ [0.4.0]: https://github.com/philiprehberger/rb-try/releases/tag/v0.4.0
67
74
  [0.3.0]: https://github.com/philiprehberger/rb-try/releases/tag/v0.3.0
68
75
  [0.2.0]: https://github.com/philiprehberger/rb-try/releases/tag/v0.2.0
69
76
  [0.1.5]: https://github.com/philiprehberger/rb-try/releases/tag/v0.1.5
data/README.md CHANGED
@@ -124,6 +124,21 @@ result = Philiprehberger::Try.call { raise ArgumentError, "bad input" }
124
124
  result.value # => "default value"
125
125
  ```
126
126
 
127
+ ### Mapping Errors
128
+
129
+ Transform a `Failure`'s error into a different exception without recovering:
130
+
131
+ ```ruby
132
+ require 'philiprehberger/try'
133
+
134
+ result = Philiprehberger::Try.call { File.read('missing.txt') }
135
+ .map_error { |e| MyError.new("read failed: #{e.message}") }
136
+ ```
137
+
138
+ `Success#map_error` is a no-op. On `Failure`, the block return is wrapped in a new `Failure`. If
139
+ the block returns a non-Exception value, it is wrapped in `RuntimeError` so that `Failure#error`
140
+ always exposes an Exception.
141
+
127
142
  ### Side effects with `tap`
128
143
 
129
144
  Execute side effects without changing the result:
@@ -215,6 +230,7 @@ result.error # => #<Timeout::Error: execution expired>
215
230
  | `#map { block }` | Wraps block result in new `Try.call` | Returns self |
216
231
  | `#flat_map { block }` | Chains block returning Try | Returns self |
217
232
  | `#recover { block }` | Returns self | Wraps block result in `Try.call` |
233
+ | `#map_error { block }` | Returns self | Wraps block result (or `RuntimeError`) in new `Failure` |
218
234
  | `#filter { block }` | Returns self if truthy, `Failure` if falsy | Returns self |
219
235
  | `#deconstruct_keys(keys)` | `{ success: true, value: }` | `{ success: false, error: }` |
220
236
  | `#tap { block }` | Calls block, returns self | Calls block, returns self |
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module Try
5
- VERSION = '0.4.0'
5
+ VERSION = '0.5.0'
6
6
  end
7
7
  end
@@ -90,6 +90,10 @@ module Philiprehberger
90
90
  self
91
91
  end
92
92
 
93
+ def map_error
94
+ self
95
+ end
96
+
93
97
  def filter
94
98
  return Failure.new(ArgumentError.new('filter condition not met')) unless yield @value
95
99
 
@@ -165,6 +169,12 @@ module Philiprehberger
165
169
  Try.call { yield @error }
166
170
  end
167
171
 
172
+ def map_error
173
+ new_error = yield @error
174
+ new_error = RuntimeError.new(new_error.to_s) unless new_error.is_a?(Exception)
175
+ Failure.new(new_error)
176
+ end
177
+
168
178
  def filter
169
179
  self
170
180
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-try
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Rehberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-10 00:00:00.000000000 Z
11
+ date: 2026-05-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A tiny utility for concise error handling. Wrap risky expressions with
14
14
  Try.call, chain fallbacks with or_else and or_try, filter with predicates, handle