@@ -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
@@ -528,10 +539,13 @@ extension WordPressComRestApi {
528539
529540 let mappedError = errorsMap [ errorCode] ?? . unknown
530541 if mappedError == . invalidToken {
531- invalidTokenHandler ? ( )
542+ // Call `invalidTokenHandler in the main thread since it's typically used by the apps to present an authentication UI.
543+ DispatchQueue . main. async {
544+ self . invalidTokenHandler ? ( )
545+ }
532546 }
533547
534- var originalErrorUserInfo = ( originalError as NSError ) . userInfo
548+ var originalErrorUserInfo = additionalUserInfo ?? [ : ]
535549 originalErrorUserInfo. removeValue ( forKey: NSLocalizedDescriptionKey)
536550
537551 return . init(
0 commit comments