In Combine, publishers can emit errors during execution.
When an error occurs, the publisher terminates the stream and sends a failure completion to the subscriber.
The Combine pipeline therefore has two possible completion states:
.finished
.failure(Error)
Error handling allows developers to intercept failures, recover from them, or replace them with fallback values.
Every publisher has two associated types:
Publisher<Output, Failure>
Where:
Output → The type of values emitted
Failure → The type of error the publisher can produce
Example:
PassthroughSubject<String, Never>
This publisher:
emits String values
never produces errors
tryMapOperators such as map cannot throw errors.
To allow throwing errors inside the pipeline, Combine provides: