From e064aea97d85bd3715ca600f89b3ecbb9cfc5af1 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 17 Oct 2025 22:39:12 -0700 Subject: [PATCH] =?UTF-8?q?Skip=20caching=20errors=20=E2=80=94=C2=A0those?= =?UTF-8?q?=20are=20things=20like=20connectivity=20errors,=20and=20shouldn?= =?UTF-8?q?=E2=80=99t=20be=20cached.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Modules/RSWeb/Sources/RSWeb/DownloadCache.swift | 8 +++----- Modules/RSWeb/Sources/RSWeb/DownloadSession.swift | 9 ++++----- Modules/RSWeb/Sources/RSWeb/Downloader.swift | 4 ++-- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/Modules/RSWeb/Sources/RSWeb/DownloadCache.swift b/Modules/RSWeb/Sources/RSWeb/DownloadCache.swift index b4c380bc5..dbf7032e3 100644 --- a/Modules/RSWeb/Sources/RSWeb/DownloadCache.swift +++ b/Modules/RSWeb/Sources/RSWeb/DownloadCache.swift @@ -12,12 +12,10 @@ struct DownloadCacheRecord: CacheRecord { let dateCreated = Date() let data: Data? let response: URLResponse? - let error: NSError? - init(data: Data?, response: URLResponse?, error: NSError?) { + init(data: Data?, response: URLResponse?) { self.data = data self.response = response - self.error = error } } @@ -35,8 +33,8 @@ final class DownloadCache: Sendable { } } - func add(_ urlString: String, data: Data?, response: URLResponse?, error: Error?) { - let cacheRecord = DownloadCacheRecord(data: data, response: response, error: error as? NSError) + func add(_ urlString: String, data: Data?, response: URLResponse?) { + let cacheRecord = DownloadCacheRecord(data: data, response: response) cache[urlString] = cacheRecord } } diff --git a/Modules/RSWeb/Sources/RSWeb/DownloadSession.swift b/Modules/RSWeb/Sources/RSWeb/DownloadSession.swift index 01c52bc13..e86c00b76 100755 --- a/Modules/RSWeb/Sources/RSWeb/DownloadSession.swift +++ b/Modules/RSWeb/Sources/RSWeb/DownloadSession.swift @@ -122,10 +122,9 @@ extension DownloadSession: URLSessionTaskDelegate { return } - if let url = task.originalRequest?.url { + if let url = task.originalRequest?.url, error == nil { Self.logger.debug("DownloadSession: Caching response for \(url)") - let responseToCache = DownloadCacheRecord(data: info.data as Data, response: info.urlResponse, error: error as NSError?) - cache[url.absoluteString] = responseToCache + cache.add(url.absoluteString, data: info.data as Data, response: info.urlResponse) } delegate.downloadSession(self, downloadDidComplete: info.url, response: info.urlResponse, data: info.data as Data, error: error as NSError?) @@ -172,7 +171,7 @@ extension DownloadSession: URLSessionDataDelegate { if statusCode != HTTPResponseCode.tooManyRequests { if let urlString = response.url?.absoluteString { Self.logger.debug("DownloadSession: Caching >= 400 response for \(urlString)") - cache.add(urlString, data: nil, response: response, error: nil) + cache.add(urlString, data: nil, response: response) } } @@ -234,7 +233,7 @@ private extension DownloadSession { // Check cache if let cachedResponse = cache[urlToUse.absoluteString] { Self.logger.info("DownloadSession: using cached response for \(urlToUse) - \(cachedResponse.response?.forcedStatusCode ?? -1)") - delegate.downloadSession(self, downloadDidComplete: url, response: cachedResponse.response, data: cachedResponse.data ?? Data(), error: cachedResponse.error) + delegate.downloadSession(self, downloadDidComplete: url, response: cachedResponse.response, data: cachedResponse.data ?? Data(), error: nil) return } diff --git a/Modules/RSWeb/Sources/RSWeb/Downloader.swift b/Modules/RSWeb/Sources/RSWeb/Downloader.swift index 3c77b2bbb..8aa3063b0 100755 --- a/Modules/RSWeb/Sources/RSWeb/Downloader.swift +++ b/Modules/RSWeb/Sources/RSWeb/Downloader.swift @@ -62,7 +62,7 @@ public typealias DownloadCallback = @MainActor (Data?, URLResponse?, Error?) -> if isCacheableRequest { if let cachedRecord = cache[url.absoluteString] { Self.logger.debug("Downloader: returning cached record for \(url)") - callback(cachedRecord.data, cachedRecord.response, cachedRecord.error) + callback(cachedRecord.data, cachedRecord.response, nil) return } } @@ -86,7 +86,7 @@ public typealias DownloadCallback = @MainActor (Data?, URLResponse?, Error?) -> if isCacheableRequest { Self.logger.debug("Downloader: caching response for \(url)") - self.cache.add(url.absoluteString, data: data, response: response, error: error) + self.cache.add(url.absoluteString, data: data, response: response) } Task { @MainActor in