Combine Code Review
Reviews Combine framework code for memory leaks, operator misuse, and error handling. Use when reviewing code with import Combine, AnyPublisher, @Published,...
Documentation & Preview
Combine Error Handling
Error Types in Combine
Every publisher declares Publisher<Output, Failure>. Unlike other reactive frameworks, Combine enforces error types at compile time.
The Never Type
Failure == Nevermeans the publisher can never fail- Required for
assign(to:on:)- must convert failable publishers first - Created by
replaceError(with:)orcatchwith infallible fallback
Converting Error Types
// setFailureType: Never → CustomError
Just("Hello")
.setFailureType(to: APIError.self)
// mapError: URLError → APIError
urlSession.dataTaskPublisher(for: url)
.mapError { .networkError($0) }try* Operators
The try-prefixed operators allow throwing but erase error type to Swift.Error.
| Operator | Preserves Failure Type | Can Throw | |----------|----------------------|-----------| | map | Yes | No | | tryMap | No (erases to Error) | Yes | | filter | Yes | No | | tryFilter | No (erases to Error) | Yes |
Always follow tryMap with mapError:
publisher
.tryMap { try JSONDecoder().decode(User.self, from: $0) }
.mapError { $0 as? APIError ?? .unknown($0) }catch vs replaceError
| Aspect | catch | replaceError(with:) | |--------|---------|----------------------| | Returns | New publisher | Single value | | Can inspect error | Yes | No | | Post-error values | Multiple possible | One then completes | | Result Failure type | Depends on fallback | Never |
// replaceError: Simple fallback value
imagePublisher
.replaceError(with: placeholderImage)
// catch: Inspect error, provide fallback publisher
primaryAPI
.catch { error -> AnyPublisher<Data, Never> in
if case .notFound = error {
return fallbackAPI.replaceError(with: Data())
}
return Just(Data()).eraseToAnyPublisher()
}Critical Anti-Patterns
1. Error Handling in Main Chain Kills Publisher
// BAD: Main chain dies after first error
searchText
.flatMap { query in networkRequest(query) }
.replaceError(with: []) // Publisher dead after one error!
.sink { results in ... }
// GOOD: Handle errors inside flatMap
searchText
.flatMap { query in
networkRequest(query)
.replaceError(with: []) // Inner publisher handles error
}
.sink { results in ... } // Main chain stays alive2. Using tryMap Without mapError
// BAD: Loses specific error type
publisher.tryMap { try decode($0) }
// Failure is now plain Error
// GOOD: Restore error type
publisher.tryMap { try decode($0) }
.mapError { $0 as? APIError ?? .unknown($0) }3. assertNoFailure in Production
// BAD: Crashes app on network error
networkPublisher
.assertNoFailure() // Fatal error!
// GOOD: Handle expected errors
networkPublisher
.catch { _ in Just(defaultValue) }4. assign(to:on:) with Failable Publishers
// COMPILE ERROR: Failure must be Never
networkPublisher // Failure: URLError
.assign(to: \.data, on: viewModel)
// FIXED: Handle errors first
networkPublisher
.replaceError(with: defaultData) // Now Failure is Never
.assign(to: \.data, on: viewModel)5. Not Handling Errors Before Long-Lived Subscriptions
// BAD: First error kills subscription permanently
dataPublisher
.receive(on: DispatchQueue.main)
.assign(to: \.items, on: viewModel) // Dead after first error
// GOOD: Error handling preserves subscription
dataPublisher
.catch { _ in Just([]) }
.receive(on: DispatchQueue.main)
.assign(to: \.items, on: viewModel)Review Questions
- Is error type preserved through the pipeline? (Check for naked tryMap)
- Will this publisher survive its first error? (Check where catch/replaceError is)
- Is
assertNoFailureused for expected errors? (Should only be programming errors) - Are error types unified at API boundaries with mapError?
- Is the publisher infallible (
Failure == Never) before assign(to:on:)?