Skip to content

Errors & Cancellation

Sentinel Errors

Variable Message Returned / Emitted By
ErrSkip "automi: skip item" User callbacks (FromFunc, FromSeq2, TryMap, ParMap, Step.Then, Chain, OnError, ToFunc, Each, ParEach) to drop an item without halting the pipeline.
ErrNilSource "automi: nil source" From(nil)
ErrNilSink "automi: nil sink" Into(ctx, nil), ToSlice(nil), ToChan(nil), SinkFunc[T](nil).Consume(...), FanOut when any sink is nil
ErrNilFunc "automi: nil function" FromFunc, Filter, Tap, Map, TryMap, FlatMap, Through, ParMap, ParEach, Reduce, GroupBy, ToFunc, Do, Step.Then, Chain (when any element or receiver is nil)
ErrNilReader "automi: nil reader" FromCSV(nil), FromLines(nil), FromReader(nil, ...)
ErrNilWriter "automi: nil writer" ToCSV(nil), ToWriter(nil)
ErrCircuitOpen "automi: circuit breaker open" Step.CircuitBreaker while the breaker is open or a single half-open probe is already active

Additional validation errors:

  • RateLimit(perSec) with perSec <= 0 emits "automi: rate limit must be positive".

ErrSkip Contract

ErrSkip uses errors.Is(err, ErrSkip) matching across all stages:

  • FromSeq2, FromFunc: Drops item and pulls next item.
  • TryMap, ParMap: Drops item; in Ordered() mode, advances sequence tracking cleanly.
  • Each, ParEach, ToFunc: Ignores ErrSkip for the item and continues.
  • Step.Then, Chain: Short-circuits remaining steps in the chain and returns ErrSkip immediately.
  • Step.Retry: Never retries ErrSkip.
  • Step.CircuitBreaker: Does not increment failure counter or trip breaker; clears probeInFlight if a half-open probe returns ErrSkip.
  • Step.OnError: Skips calling handler if the wrapped step returns ErrSkip. If handler itself returns nil or ErrSkip, OnError returns ErrSkip.

Worker Panic Recovery

ParMap (Workers == 1 inline fast-path, unordered, and ordered) and ParEach (both workers == 1 and concurrent pool) wrap callback invocations in recover().

A panic converts to an error formatted as:

automi: worker panic: <recovered value>

Concurrent stages then cancel their child context, await sibling worker exit, and deliver the panic error to the terminal caller. Step.CircuitBreaker also guards half-open probe state with a defer so a panic during a probe re-opens the breaker rather than leaving probes permanently blocked.

Context Normalization and Cancellation Guarantees

  • Nil context: Terminals (Into, Collect, Each, ParEach, Reduce, GroupBy, All, FanOut), Step.Then, Chain, and SinkFunc.Consume treat a nil context.Context as context.Background().
  • Pre-canceled contexts: FromSlice, FromChan, FromSeq, FromSeq2, FromFunc, FromCSV, FromLines, FromReader, Step.Then, Chain, Concat, ToChan, and Step.Retry check ctx.Err() before blocking or issuing side effects.
  • Sibling teardown (errgroup semantics):
  • ParMap, ParEach, timed Batch, Buffer/BufferDrop, Debounce, Merge, Zip, and FanOut run background workers under context.WithCancel(ctx).
  • First non-skip error, upstream error, consumer error, or early loop exit cancels the child context and waits via sync.WaitGroup.Wait() (or pull stoppers in Zip) before returning.
  • Even if workers swallow cancellation and return ErrSkip on shutdown, ParMap, ParEach, Buffer, Debounce, timed Batch, and Merge still propagate non-nil innerCtx.Err().