You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* develop:
specify next release
typo
prove deferred attempts behave the same way
add Attempt::guard() and ::xrecover()
give more flexibility to the attempt implementations
If `#!php $reduction` is `#!php 2` then `#!php $attempt` will contain a `DivisionByZeroError` otherwise for any other value it will contain a fraction of `#!php 42`.
113
113
114
+
## `->guard()`
115
+
116
+
This behaves like [`->flatMap()`](#-flatmap) except any error contained in the attempt returned by the callable won't be recovered when calling [`->xrecover()`](#-xrecover).
117
+
114
118
## `->match()`
115
119
116
120
This extracts the result value but also forces you to deal with any potential error.
Here `#!php $attempt` is `#!php 42` because the first `Attempt` raised a `DivisionByZeroError`.
148
152
153
+
## `->xrecover()`
154
+
155
+
This behaves like [`->recover()`](#-recover) except when conjointly used with [`->guard()`](#-guard). Guarded errors can't be recovered.
156
+
157
+
An example of this problem is an HTTP router with 2 routes. One tries to handle a `POST` request, then do some logging, the other tries to handle a `GET` request. It would look something like this:
158
+
159
+
```php
160
+
$response = handlePost($request)
161
+
->flatMap(static fn($response) => log($response))
162
+
->recover(static fn() => handleGet($request));
163
+
```
164
+
165
+
The problem here is that if the request is indeed a `POST` we handle it then log the response. But if the logging fails then we try to handle it as a `GET` request. In this case we handle the request twice, which isn't good.
166
+
167
+
The correct approach is:
168
+
169
+
```php
170
+
$response = handlePost($request)
171
+
->guard(static fn($response) => log($response))
172
+
->xrecover(static fn() => handleGet($request));
173
+
```
174
+
175
+
This way if the logging fails it will return this failure and not call `handleGet()`.
0 commit comments