Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 3f13aa6

Browse files
committed
Decouple WP.com REST API error handling from Alamofire
1 parent 9df47c4 commit 3f13aa6

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

WordPressKit/WordPressComRestApi.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,21 @@ extension WordPressComRestApi {
484484

485485
/// A custom error processor to handle error responses when status codes are betwen 400 and 500
486486
func processError(response: DataResponse<Any>, originalError: Error) -> WordPressComRestApiEndpointError? {
487-
guard let afError = originalError as? AFError, case AFError.responseValidationFailed(_) = afError, let httpResponse = response.response, (400...500).contains(httpResponse.statusCode), let data = response.data else {
488-
if let afError = originalError as? AFError, case AFError.responseSerializationFailed(_) = afError {
489-
return .init(code: .responseSerializationFailed)
490-
}
487+
if let afError = originalError as? AFError, case AFError.responseSerializationFailed(_) = afError {
488+
return .init(code: .responseSerializationFailed)
489+
}
490+
491+
guard let httpResponse = response.response, let data = response.data else {
492+
return nil
493+
}
494+
495+
return processError(response: httpResponse, body: data, additionalUserInfo: (originalError as NSError).userInfo)
496+
}
497+
498+
func processError(response httpResponse: HTTPURLResponse, body data: Data, additionalUserInfo: [String: Any]?) -> WordPressComRestApiEndpointError? {
499+
// Not sure if it's intentional to include 500 status code, but the code seems to be there from the very beginning.
500+
// https://github.com/wordpress-mobile/WordPressKit-iOS/blob/1.0.1/WordPressKit/WordPressComRestApi.swift#L374
501+
guard (400...500).contains(httpResponse.statusCode) else {
491502
return nil
492503
}
493504

@@ -534,7 +545,7 @@ extension WordPressComRestApi {
534545
}
535546
}
536547

537-
var originalErrorUserInfo = (originalError as NSError).userInfo
548+
var originalErrorUserInfo = additionalUserInfo ?? [:]
538549
originalErrorUserInfo.removeValue(forKey: NSLocalizedDescriptionKey)
539550

540551
return .init(

0 commit comments

Comments
 (0)