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 +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +16 -0
- data/lib/philiprehberger/try/version.rb +1 -1
- data/lib/philiprehberger/try.rb +10 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0c9450e5d2764d72177933ac79769244a27c0963e89482efa19e6e99b3ed1880
|
|
4
|
+
data.tar.gz: 92bd0ab4f69ba2ae16a1f32e6e80f4ee20cb44cebce09ac59da3621fecbe5a12
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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 |
|
data/lib/philiprehberger/try.rb
CHANGED
|
@@ -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
|
+
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-
|
|
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
|