MCPcopy
hub / github.com/destel/rill / Catch

Function Catch

transform.go:187–200  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

185//
186// See the package documentation for more information on non-blocking unordered functions and error handling.
187func 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].
203func OrderedCatch[A any](in <-chan Try[A], n int, f func(error) error) <-chan Try[A] {

Callers 2

ExampleCatchFunction · 0.92
universalCatchFunction · 0.85

Calls 1

FilterMapFunction · 0.92

Tested by 2

ExampleCatchFunction · 0.74
universalCatchFunction · 0.68