Catch allows handling errors in the middle of a stream processing pipeline. Every error encountered in the input stream is passed to the function f for handling. The outcome depends on the return value of f: - If f returns nil, the error is considered handled and filtered out from the output stream
(in <-chan Try[A], n int, f func(error) error)
| 185 | // |
| 186 | // See the package documentation for more information on non-blocking unordered functions and error handling. |
| 187 | func Catch[A any](in <-chan Try[A], n int, f func(error) error) <-chan Try[A] { |
| 188 | return core.FilterMap(in, n, func(a Try[A]) (Try[A], bool) { |
| 189 | if a.Error == nil { |
| 190 | return a, true |
| 191 | } |
| 192 | |
| 193 | err := f(a.Error) |
| 194 | if err == nil { |
| 195 | return a, false // error handled, filter out |
| 196 | } |
| 197 | |
| 198 | return Try[A]{Error: err}, true // error replaced by f(a.Error) |
| 199 | }) |
| 200 | } |
| 201 | |
| 202 | // OrderedCatch is the ordered version of [Catch]. |
| 203 | func OrderedCatch[A any](in <-chan Try[A], n int, f func(error) error) <-chan Try[A] { |