@@ -12,9 +12,8 @@ import os
1212enum BackgroundDownloadError : Error {
1313 case missingInstructionsError
1414 case fileSystemError( _ underlyingError: Error )
15- case networkError( _ underlyingError: Error ? )
16- case unexpectedResponseError
17- case unexpectedStatusCode
15+ case clientError( _ underlyingError: Error )
16+ case serverError( _ underlyingResponse: URLResponse ? )
1817}
1918
2019class BackgroundDownloadService : NSObject {
@@ -69,38 +68,33 @@ extension BackgroundDownloadService: URLSessionDownloadDelegate {
6968 didFinishDownloadingTo location: URL ) {
7069 guard let fromURL = downloadTask. originalRequest? . url else {
7170 os_log ( . error, " Unexpected nil URL " )
72- // Unable to call the closure here as we use originalRequestURL as the key to retrieve the closure
71+ // Unable to call the closure here as we use fromURL as the key to retrieve the closure
7372 return
7473 }
7574
7675 defer {
7776 store. removeMetadata ( for: fromURL)
7877 }
7978
80- let fromURLAbsoluteString = fromURL. absoluteString
79+ let fromURLAsString = fromURL. absoluteString
8180
82- os_log ( . info, " Download request completed for: %{public}@ " , fromURLAbsoluteString )
81+ os_log ( . info, " Download request completed for: %{public}@ " , fromURLAsString )
8382
8483 store. retrieveMetadata ( for: fromURL) { toURL, completionHandler in
8584 guard let toURL else {
86- os_log ( . error, " Unable to find existing download item for: %{public}@ " , fromURLAbsoluteString )
85+ os_log ( . error, " Unable to find existing download item for: %{public}@ " , fromURLAsString )
8786 completionHandler ? ( . failure( BackgroundDownloadError . missingInstructionsError) )
8887 return
8988 }
9089
91- guard let response = downloadTask. response as? HTTPURLResponse else {
92- os_log ( . error, " Unexpected response for: %{public}@ " , fromURLAbsoluteString)
93- completionHandler ? ( . failure( BackgroundDownloadError . unexpectedResponseError) )
90+ guard let response = downloadTask. response as? HTTPURLResponse ,
91+ response. statusCode == 200 else {
92+ os_log ( . error, " Unexpected response for: %{public}@ " , fromURLAsString)
93+ completionHandler ? ( . failure( BackgroundDownloadError . serverError ( downloadTask. response) ) )
9494 return
9595 }
9696
97- guard response. statusCode == 200 else {
98- os_log ( . error, " Unexpected status code of: %{public}d, for: %{public}@ " , response. statusCode, fromURLAbsoluteString)
99- completionHandler ? ( . failure( BackgroundDownloadError . unexpectedStatusCode) )
100- return
101- }
102-
103- os_log ( . info, " Download successful for: %{public}@ " , fromURLAbsoluteString)
97+ os_log ( . info, " Download successful for: %{public}@ " , fromURLAsString)
10498
10599 do {
106100 try FileManager . default. moveItem ( at: location,
@@ -129,12 +123,12 @@ extension BackgroundDownloadService: URLSessionDownloadDelegate {
129123 store. removeMetadata ( for: fromURL)
130124 }
131125
132- let fromURLAbsoluteString = fromURL. absoluteString
126+ let fromURLAaString = fromURL. absoluteString
133127
134- os_log ( . info, " Download failed for: %{public}@ " , fromURLAbsoluteString )
128+ os_log ( . info, " Download failed for: %{public}@ " , fromURLAaString )
135129
136130 store. retrieveMetadata ( for: fromURL) { _, completionHandler in
137- completionHandler ? ( . failure( BackgroundDownloadError . networkError ( error) ) )
131+ completionHandler ? ( . failure( BackgroundDownloadError . clientError ( error) ) )
138132 }
139133 }
140134
0 commit comments