to-result 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +27 -3
- data/lib/to-result.rb +3 -2
- data/tests/support/fake_logger.rb +2 -2
- data/tests/to_result_test.rb +3 -2
- data/to-result.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc48c861de210a05cba164c61710457447f04b60b492ef15791ab7e5c28a3375
|
4
|
+
data.tar.gz: fb5802866e75958e710ec9a0be23584f8f737101ef8afd18c5b212a775539c1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60728e022ce553988082effc1ab86a3260cf399a464bddea06d936e6428bf54f0604a8b86638ee7932526a20d72b3c80bcc491f6d816d6d17bdd93326c11b2ce
|
7
|
+
data.tar.gz: 959d7af9b3eb1367269c6650ca9c9babedd8c3cfd6e36403d3d4f01b750219dc7781cec78e4bc2a4972ce34e230d157dbd17a8bee70b7c9cc24c407fb71f5bc3
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## [0.1.1](https://github.com/a-chris/to-result/tree/0.1.1) (2023-08-16)
|
4
|
+
|
5
|
+
HOTFIXES:
|
6
|
+
|
7
|
+
- Always pass the error to the `on_error` callable object instead of mixed `error/Failure(error)`
|
8
|
+
|
3
9
|
## [0.1.0](https://github.com/a-chris/to-result/tree/0.1.0) (2022-10-28)
|
4
10
|
|
5
11
|
- Explicit requires `dry-monads` >= 1.x, just for convenience
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
|
2
2
|
# ToResult
|
3
3
|
|
4
|
-
ToResult is a wrapper built over `dry-monads` to make the `Do Notation`, `Result` and `Try` concepts more handy and consistent to use,
|
4
|
+
ToResult is a wrapper built over `dry-monads` to make the `Do Notation`, `Result` and `Try` concepts more handy and consistent to use, especially to implement the **Railway Pattern**.
|
5
5
|
|
6
6
|
## Why I created ToResult
|
7
7
|
|
@@ -120,7 +120,7 @@ class MyClass
|
|
120
120
|
end
|
121
121
|
```
|
122
122
|
|
123
|
-
now you can always use `ToResult` all the time you wanted to use `
|
123
|
+
now you can always use `ToResult` all the time you wanted to use `Try` or return `Success/Failure` but with a more convenient interface and consistent behaviour, my goal is to have a solution that can be used for every use-case.
|
124
124
|
|
125
125
|
Look at this:
|
126
126
|
|
@@ -144,6 +144,30 @@ ToResult(only: [ArgumentError]) { yield Failure(YourCustomError.new('error code'
|
|
144
144
|
# raises YourCustomError('error code')
|
145
145
|
```
|
146
146
|
|
147
|
+
## Local and global callback on errors
|
148
|
+
to-result gives you the possibility to define a callback to be called when an error is raised inside the `ToResult` block, this is a handy place to log errors.
|
149
|
+
|
150
|
+
You can define a global callback, usually defined into an initializer:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
# initializers/to_result.rb
|
154
|
+
|
155
|
+
ToResultMixin.configure do |c|
|
156
|
+
c.on_error = Proc.new { |e| Logger.log_error(e) }
|
157
|
+
end
|
158
|
+
```
|
159
|
+
|
160
|
+
or a local callback:
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
ToResult(on_error: proc { |e| Logger.log_error(e) }) do
|
164
|
+
yield Failure(StandardError.new('error code'))
|
165
|
+
end
|
166
|
+
```
|
167
|
+
|
168
|
+
you can even use both at the same time but keep in mind that **local callback overrides the global one**.
|
169
|
+
|
170
|
+
|
147
171
|
## Changelog
|
148
172
|
|
149
173
|
[Changelog](CHANGELOG.md)
|
@@ -154,7 +178,7 @@ I'm already planning to implement some useful features:
|
|
154
178
|
- [x] configurable error logging when an exception is catched inside `DoResult`
|
155
179
|
e.g. sending the log to Airbrake or whathever service you are using
|
156
180
|
- [x] transform/process the catched error => this can be handled with `alt_map` or other methods already available in `dry-monads`
|
157
|
-
- [ ] any
|
181
|
+
- [ ] any type of suggestion is appreciated 😁
|
158
182
|
|
159
183
|
## Authors
|
160
184
|
|
data/lib/to-result.rb
CHANGED
@@ -48,9 +48,10 @@ module ToResultMixin
|
|
48
48
|
Proc.new do
|
49
49
|
f.call
|
50
50
|
rescue Dry::Monads::Do::Halt => e
|
51
|
-
error = e.result
|
51
|
+
failure = error = e.result
|
52
|
+
error = error.failure if error.respond_to?(:failure)
|
52
53
|
on_error.call(error) if on_error.respond_to?(:call)
|
53
|
-
return
|
54
|
+
return failure
|
54
55
|
rescue *only => e
|
55
56
|
on_error.call(e) if on_error.respond_to?(:call)
|
56
57
|
raise e
|
data/tests/to_result_test.rb
CHANGED
@@ -67,7 +67,7 @@ class ToResultTest < Minitest::Test
|
|
67
67
|
clean_room = Class.new(Object)
|
68
68
|
clean_room.new.instance_eval do
|
69
69
|
ToResultMixin.configure do |c|
|
70
|
-
c.on_error = Proc.new { FakeLogger.log_error }
|
70
|
+
c.on_error = Proc.new { |e| FakeLogger.log_error(e) }
|
71
71
|
end
|
72
72
|
end
|
73
73
|
end
|
@@ -78,6 +78,7 @@ class ToResultTest < Minitest::Test
|
|
78
78
|
FakeLogger.expects(:log_error).once
|
79
79
|
|
80
80
|
expected = StandardError.new(@value)
|
81
|
+
|
81
82
|
assert ToResult { raise expected } == Failure(expected)
|
82
83
|
end
|
83
84
|
|
@@ -108,6 +109,6 @@ class ToResultTest < Minitest::Test
|
|
108
109
|
|
109
110
|
expected = StandardError.new(@value)
|
110
111
|
FakeLogger.expects(:return_error).with(expected).returns(expected).once
|
111
|
-
assert ToResult(on_error: local_on_error) {
|
112
|
+
assert ToResult(on_error: local_on_error) { yield Failure(expected) } == Failure(expected)
|
112
113
|
end
|
113
114
|
end
|
data/to-result.gemspec
CHANGED
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = 'to-result'
|
8
|
-
s.version = '0.1.
|
8
|
+
s.version = '0.1.1'
|
9
9
|
s.summary = 'A wrapper over dry-monads to offer a handy and consistent way to implement the Railway pattern.'
|
10
10
|
s.description = 'A wrapper over dry-monads to offer a handy and consistent way to implement the Railway pattern.'
|
11
11
|
s.authors = ['Christian Toscano']
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: to-result
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christian Toscano
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-monads
|
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
requirements: []
|
63
|
-
rubygems_version: 3.
|
63
|
+
rubygems_version: 3.4.10
|
64
64
|
signing_key:
|
65
65
|
specification_version: 4
|
66
66
|
summary: A wrapper over dry-monads to offer a handy and consistent way to implement
|