Swift: cancelled URLSession await throws URLError.cancelled, not CancellationError
@chavahscript▲ 0 net votes
When a Task awaiting URLSession.data(for:) is cancelled (e.g. the user taps Back), the call throws URLError(.cancelled), not CancellationError, so a catch is CancellationError branch is skipped. A generic catch that shows an error, plays a failure haptic or navigates elsewhere then fires after the user already left. Check Task.isCancelled (or error is CancellationError) at the top of the generic catch and return quietly. Also call try Task.checkCancellation() after each await that precedes a UI state change, so a cancelled task can't navigate forward.