使用 Alamofire 4,我们有一个 API 响应验证器,我们像这样调用:
func request<Endpoint: APIEndpoint>(_ baseURL: URL, endpoint: Endpoint, completion: @escaping (_ object: Endpoint.ResponseType?, _ error: AFError?) -> Void) -> DataRequest where Endpoint.ResponseType: Codable {
let responseSerializer = APIObjectResponseSerializer(endpoint)
let request = self.request(baseURL, endpoint: endpoint)
.validate(APIResponseValidator.validate) << VALIDATOR PASSED HERE
.response(responseSerializer: responseSerializer) { response in
completion(response.value, response.error)
}
return request
}
它看起来像这样:
static func validate(request: URLRequest?, response: HTTPURLResponse, data: Data?) -> Request.ValidationResult {
// **INSERT OTHER FAILURE CHECKS HERE**
// Verify server time is within a valid time window.
let headers = response.allHeaderFields
guard let serverTimeString = headers["Date"] as? String, let serverTime = DateUtils.headerDateFormatter().date(from: serverTimeString) else {
Log.error("APIValidation: no Date in response header")
return .failure(APIError.appTimeSettingInvalid))
}
// **INSERT OTHER FAILURE CHECKS HERE**
return .success(Void())
}
适当的错误会返回到请求完成处理程序,
▿ APIError
▿ appTimeSettingInvalid
我们可以用正确的错误更新 UI,每个人都很高兴。
但现在有了 Alamofire,它是这样的:
▿ Optional<Error>
▿ some : AFError
▿ requestRetryFailed : 2 elements
▿ retryError : AFError
▿ responseValidationFailed : 1 element
▿ reason : ResponseValidationFailureReason
▿ customValidationFailed : 1 element
▿ error : APIError
▿ appTimeSettingInvalid << Original custom error
▿ originalError : AFError
▿ responseValidationFailed : 1 element
▿ reason : ResponseValidationFailureReason
▿ customValidationFailed : 1 element
▿ error : APIError
▿ appTimeSettingInvalid << Original custom error
我需要像这样访问:
if let underlyingError = (error as? AFError)?.underlyingError as? AFError,
case let AFError.requestRetryFailed(_, originalError) = underlyingError,
case let AFError.responseValidationFailed(reason) = originalError,
case let .customValidationFailed(initialCustomError) = reason {
showAlert(initialCustomError)
}
这似乎很荒谬。我错过了什么?当方法没有任何改变时,为什么自定义验证会失败,为什么它会包裹在一层其他错误中?当验证将以同样的方式失败时,为什么要重试请求?
如何在所有请求中始终如一地恢复我的自定义错误?