Skip to content

Commit 4f4d8fd

Browse files
improvement - evaluateResponse
1 parent 3895f4d commit 4f4d8fd

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

Sources/NextcloudKit/NextcloudKit+Upload.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ public extension NextcloudKit {
9797
}
9898
}
9999

100-
options.queue.async { requestHandler(request) }
100+
options.queue.async {
101+
requestHandler(request)
102+
}
101103
}
102104

103105
/// Asynchronously uploads a file to the Nextcloud server.

Sources/NextcloudKit/NextcloudKit.swift

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,13 @@ open class NextcloudKit {
178178
}
179179
#endif
180180

181+
/*
181182
/// Evaluates an Alamofire response and returns the appropriate NKError.
182-
/// Treats `inputDataNilOrZeroLength` as `.success`.
183183
func evaluateResponse<Data>(_ response: AFDataResponse<Data>) -> NKError {
184-
if let afError = response.error?.asAFError {
185-
if afError.isExplicitlyCancelledError {
186-
return .cancelled
187-
}
184+
// Treat explicit cancellations as a first-class outcome
185+
if let afError = response.error?.asAFError,
186+
afError.isExplicitlyCancelledError {
187+
return .cancelled
188188
}
189189

190190
switch response.result {
@@ -200,4 +200,42 @@ open class NextcloudKit {
200200
return .success
201201
}
202202
}
203+
*/
204+
205+
/// Evaluates an Alamofire response and returns the appropriate NKError.
206+
func evaluateResponse<Data>(_ response: AFDataResponse<Data>) -> NKError {
207+
// Treat explicit cancellations as a first-class outcome
208+
if let afError = response.error?.asAFError,
209+
afError.isExplicitlyCancelledError {
210+
return .cancelled
211+
}
212+
213+
// Prefer HTTP status code over serializer outcome for uploads
214+
let statusCode = response.response?.statusCode
215+
if let code = statusCode {
216+
// Success on any 2xx; explicitly include 204/205 which carry no body by definition
217+
if (200...299).contains(code) || code == 204 || code == 205 {
218+
return .success
219+
}
220+
}
221+
222+
// Fall back to Alamofire's result only if HTTP status wasn't clearly successful
223+
switch response.result {
224+
case .success:
225+
return .success
226+
227+
case .failure(let error):
228+
// If the only failure reason is "no data" but status is actually OK, still succeed
229+
if let afError = error.asAFError,
230+
case .responseSerializationFailed(let reason) = afError,
231+
case .inputDataNilOrZeroLength = reason,
232+
let code = statusCode,
233+
(200...299).contains(code) || code == 204 || code == 205 {
234+
return .success
235+
}
236+
237+
// Everything else is a real error: keep the payload for diagnostics
238+
return NKError(error: error, afResponse: response, responseData: response.data)
239+
}
240+
}
203241
}

0 commit comments

Comments
 (0)