如何处理Parse.com将HTTP错误转换为NSError?

时间:2015-10-22 11:19:54

标签: parse-platform httpresponse nserror

我从云端代码呼叫第三方服务器。前者可以返回我想传递给调用iOS应用程序的HTTP错误。

当第三方服务器抛出HTTP异常时,我希望我的iOS应用程序能够处理HTTP错误详细信息。

我到目前为止找到的唯一方法是将这些例外视为云代码"成功"!

有更好的方法吗?

一个例子 当第三方服务器返回HTTP 400时,"缺少名称"错误:

从我的Cloud Code中我可以致电:

response.error(httpResponse);

然后在Swift中我可以解析它:

PFCloud.callFunctionInBackground("func", withParameters:[]) {
    (result, error) -> Void in
    if error == nil {
        // handle success
    } else {
        // handle error
        // Parse.com has wrapped the originating http error with a status code of 141 so I'll need to JSON parse the deeper fields to get 400 & "Missing Name".
    }

但是,相反,从Cloud Code,我可以调用response.success(httpResponse)然后执行此操作:

PFCloud.callFunctionInBackground("func", withParameters:[]) {
    (result, error) -> Void in
    if error == nil {
      let result = result as! [String:AnyObject]
      let status = result["status"]     
      let text = result["text"] as! String
      // status will be 400
      // text will be "Missing Name"
    } else {
       // handle Parse.com's 141s
    }

这使得字段解析更容易访问。但我处理HTTP错误以及成功通话。

1 个答案:

答案 0 :(得分:0)

您可以自定义错误消息/数据,但遗憾的是,如果您使用response.error()方法,则无法更改错误代码141。见this

相关问题