ios - 如何从 Alamofire 错误中获取底层错误?

标签 ios swift alamofire nserror

对于此请求:

Alamofire.request("https://google.com").responseCollection { (response: DataResponse<[User]>) in
  guard response.result.isSuccess else {
    print(response.error)

    return
  }
}

我在控制台中看到这个打印:

Optional(my_app_name.BackendError.jsonSerialization(Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))))

我尝试过的:

Alamofire.request("https://google.com").responseCollection { (response: DataResponse<[User]>) in
  guard response.result.isSuccess else {
    print(response.error)

    if let error1 = response.error as? AFError {
      print(error1)  // Execution DOES NOT reach here.
    }

    if let error2 = response.error as? BackendError {
      print(error2) // Execution DOES reach here.
    }

    return
  }
}

print(error2)以上打印:

jsonSerialization(Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))

我想做的是找出潜在的错误,这样我就可以解析 domain , code , 和 userInfo属性。

我创建了 BackendError Alamofire 在 https://github.com/Alamofire/Alamofire#handling-errors 提供的枚举示例:

enum BackendError: Error {
    case network(error: Error) // Capture any underlying Error from the URLSession API
    case dataSerialization(error: Error)
    case jsonSerialization(error: Error)
    case xmlSerialization(error: Error)
    case objectSerialization(reason: String)
}

我还实现了示例通用响应对象序列化,与 https://github.com/Alamofire/Alamofire#generic-response-object-serialization 中的示例完全一样:

extension DataRequest {
  @discardableResult
  func responseCollection<T: ResponseCollectionSerializable>(
    queue: DispatchQueue? = nil,
    completionHandler: @escaping (DataResponse<[T]>) -> Void) -> Self {
    let responseSerializer = DataResponseSerializer<[T]> { request, response, data, error in
      guard error == nil else {
        return .failure(BackendError.network(error: error!))
      }

      let jsonSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)
      let result = jsonSerializer.serializeResponse(request, response, data, nil)

      guard case let .success(jsonObject) = result else {
        return .failure(BackendError.jsonSerialization(error: result.error!))
      }

      guard let response = response else {
        let reason = "Response collection could not be serialized due to nil response."
        return .failure(BackendError.objectSerialization(reason: reason))
      }

      return .success(T.collection(from: response, withRepresentation: jsonObject))
    }

    return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
  }
}

我认为有switch是的,case s,并转换为 BackendError , AFError , Error , 和/或 NSError , 但我似乎无法理解。

我怎样才能找到潜在的错误,以便我可以解析 domain , code , 和 userInfo属性?

我正在使用 Swift 3 和 Alamofire 4.3.0。

最佳答案

对于 Alamofire 4.3,查看 response.result:

if case let .failure(error) = response.result {
    let error = error as NSError
    print("\(error.domain)")
    print("\(error.code)")
    print("\(error.userInfo)")
}

关于ios - 如何从 Alamofire 错误中获取底层错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42591945/

相关文章:

objective-c - 解析继承委托(delegate)的不兼容属性类型的语法

ios - 如何在 iPhone 中使用 unicode 显示空心子弹

c++ - 从 Vector3f 访问数据

iOS 13 在应用程序被终止时跟踪用户位置

ios - 当我在 UITableView 中更改 UITextField 的文本颜色时,滚动时颜色已应用于另一个 UITextfield

ios - 如何正确调整 UILabel 内容的大小? (查看帖子内的图片以便更好地理解)

swift - 如何使用 Share Extensions Swift 共享包含标记的笔记

swift - 完成另一个 Alamofire 请求后运行 Alamofire 请求

objective-c - 自定义 NSURLProtocol 以显示/隐藏 NetworkActivityIndi​​cator

ios - 当设备因错误而锁定时,URLSessionConfiguration 后台下载任务失败 - 与后台传输服务的连接丢失