このページはまだ翻訳されていないため、英語で表示されます。 翻訳に参加する

fxMapCatching

A map where each element's failure is replaced by a value — one bad row never ends the iteration.

Iterable<R> fxMapCatching<A, R>(R Function(A a) f, R Function(Object error, StackTrace stackTrace) onError, Iterable<A> iterable) FxAsyncIterable<R> fxMapCatchingAsync<A, R>(FutureOr<R> Function(A a) f, FutureOr<R> Function(Object error, StackTrace stackTrace) onError, FxAsyncIterable<A> iterable) Fx<R> Fx<T>.mapCatching<R>(R Function(T a) f, R Function(Object error, StackTrace stackTrace) onError) // chain (sync) FxAsync<R> FxAsync<T>.mapCatching<R>(FutureOr<R> Function(T a) f, FutureOr<R> Function(Object error, StackTrace stackTrace) onError) // chain (async)

Lecture

A map whose callback throws ends the whole pipeline at that element: one malformed row in ten thousand and you get an exception, not nine thousand nine hundred and ninety-nine results. mapCatching(f, onError) runs f on each element and, when it throws, hands the error and its stack trace to onError and yields what that returns in the failed element's place. The next element is mapped as if nothing had happened. It stays lazy, and on Fx it stays on the sync chain — there is nothing to await.

It sits beside mapRetry, and the two are the two answers to a failing element: mapRetry runs it again; mapCatching gives up on it and substitutes. The async form, fxMapCatchingAsync / fxAsync(…).mapCatching, lets both f and onError await, and is built on the parallel-safe fxMapAsync, so under concurrent(n) each in-flight element recovers on its own and order is kept. RxDart writes this as onErrorReturnWith; on an fxdart push stream the neighbours are onErrorReturn, which substitutes one fixed value per error, and attempt, which keeps the error as a Left instead of replacing it.

One thing is never recovered: the library's own raise signal. A r.raise(…) — from a bind, an ensure, or a nested builder — crossing f is rethrown, so it still short-circuits the enclosing fxEither / fxNullable block. Recovering it would turn a typed error into a lost one. As with any lazy pipeline in a raise block, materialize it (toList()) inside the block. fxdart extension (not part of FxTS).

Demo 1 · One bad row, recovered in place

Demo 2 · Async under concurrent, and raise passes through

Try it yourself

Exercise: replace the try/catch loop with one operator.

Related: retry — run the element again instead of replacing it · onErrorReturn — the same idea on a push stream · typed errors — when the failure should stay a value you can inspect