Commit a6c3e7cd authored by Kourser's avatar Kourser
Browse files

fix: a cancelled task is never shown as an error

Users saw an alert reading "The operation couldn't be completed.
(Swift.CancellationError error 1.)". AppModel.load() reads the library
through GRDB, which cancels its database access when the calling task
is cancelled and throws CancellationError; load() showed any error as it
was. It is reached from work the system cancels: a background refresh
out of time (the alert then waits for the next launch), a pull to
refresh interrupted by leaving the screen.

load() now reads the library in a task of its own, so a cancelled caller
neither leaves it stale nor reports anything. Every error shown to the
user goes through report(_:_:), which skips cancellations
(CancellationError, NSURLErrorCancelled); the sync status keeps its last
message when a sync is cancelled.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 7cbb8911
Loading
Loading
Loading
Loading
+7
Changes for CHANGELOG.md: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,13 @@ adopte le [versionnage sémantique](https://semver.org/lang/fr/).

## [Non publié]

### Corrigé
- Une alerte « Swift.CancellationError » pouvait s'afficher, par exemple au
  retour dans l'app après une actualisation en arrière-plan interrompue par
  iOS, ou après un tirer-pour-actualiser laissé en cours. Une tâche annulée
  n'est plus jamais présentée comme une erreur (alertes et état de la
  synchronisation), et la bibliothèque est relue malgré l'annulation.

## [1.4.1] - 2026-10-24

iOS uniquement : rien ne change sur Mac.
+33 −8
Changes for Skingomz/AppModel.swift: 33 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@ final class AppModel {
            Task { await self?.handlePlaybackFinished() }
        }
        playback.onError = { [weak self] error in
            self?.errorMessage = String(localized: "Échec de la lecture : \(error.localizedDescription)")
            self?.report(error, String(localized: "Échec de la lecture : \(error.localizedDescription)"))
        }
        playback.onSleepAtEpisodeEnd = { [weak self] in
            Task { await self?.markCurrentEpisodePlayed() }
@@ -249,12 +249,17 @@ final class AppModel {
    // MARK: Library

    func load() async {
        // Often reached from work the system cancels: a pull to refresh left
        // early, a background refresh out of time. The library is read in a
        // task of its own all the same, so a cancelled caller neither leaves it
        // stale nor greets the user with a CancellationError.
        let store = store
        do {
            podcasts = try await store.allPodcasts()
            podcasts = try await Task { try await store.allPodcasts() }.value
        } catch {
            errorMessage = error.localizedDescription
            report(error, error.localizedDescription)
        }
        podcastTags = (try? await store.allPodcastTags()) ?? [:]
        podcastTags = (try? await Task { try await store.allPodcastTags() }.value) ?? [:]
    }

    // MARK: Browse / search / tags
@@ -328,7 +333,7 @@ final class AppModel {
            noteLibraryChanged()
            return true
        } catch {
            errorMessage = String(localized: "Échec de l'ajout : \(error.localizedDescription)")
            report(error, String(localized: "Échec de l'ajout : \(error.localizedDescription)"))
            return false
        }
    }
@@ -390,7 +395,7 @@ final class AppModel {
            noteLibraryChanged()
            return true
        } catch {
            errorMessage = String(localized: "Échec de l'ajout : \(error.localizedDescription)")
            report(error, String(localized: "Échec de l'ajout : \(error.localizedDescription)"))
            return false
        }
    }
@@ -456,7 +461,7 @@ final class AppModel {
            noteLibraryChanged()
            return true
        } catch {
            errorMessage = String(localized: "Échec de l'ajout : \(error.localizedDescription)")
            report(error, String(localized: "Échec de l'ajout : \(error.localizedDescription)"))
            return false
        }
    }
@@ -731,7 +736,7 @@ final class AppModel {
        do {
            try await store.restoreDatabase(from: url)
        } catch {
            errorMessage = String(localized: "Échec de la restauration : \(error.localizedDescription)")
            report(error, String(localized: "Échec de la restauration : \(error.localizedDescription)"))
            return false
        }
        playback?.stop()
@@ -905,6 +910,8 @@ final class AppModel {

            syncSettings.save()
            syncMessage = String(localized: "Synchronisé — \(podcasts.count) abonnement(s).")
        } catch where error.isCancellation {
            // Stopped on purpose (the app left, a newer sync): the last status stands.
        } catch {
            syncMessage = String(localized: "Échec de la synchronisation : \(error.localizedDescription)")
        }
@@ -1075,3 +1082,21 @@ final class AppModel {
        return URL(string: string)
    }
}

extension AppModel {
    /// Shows `message` for `error`, unless the error is a cancellation.
    func report(_ error: Error, _ message: @autoclosure () -> String) {
        guard !error.isCancellation else { return }
        errorMessage = message()
    }
}

extension Error {
    /// Work stopped on purpose — a task cancelled by the system or by a newer
    /// request, a download cancelled — never something to tell the user about.
    var isCancellation: Bool {
        if self is CancellationError { return true }
        let error = self as NSError
        return error.domain == NSURLErrorDomain && error.code == NSURLErrorCancelled
    }
}