云功能发布请求未正确向IOS应用程序发送错误消息



我有一个iOS应用程序,从谷歌云函数接收json数据。

数据会被检索出来,并且在没有错误的情况下工作良好。

但是,当cloud函数返回错误时,下面的函数会将错误识别为nil

客户端:

func initialize() {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONSerialization.data(withJSONObject: json)
let task = URLSession.shared.dataTask(with: request, completionHandler: { [weak self] (data, response, error) in

guard let response = response as? HTTPURLResponse,
response.statusCode == 200,
let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any],
let clientSecret = json["clientSecret"] as? String,
let publishableKey = json["publishableKey"] as? String else {

///////////   
//ERROR is not being recognized
/////////
let message = error?.localizedDescription ?? "Failed to decode response from server."
print("Error loading page: (message)")
return
}
})
task.resume()
}

客户端输出:

无法解码来自服务器的响应。

服务器:

firebase.functions().httpsCallable('myFunction')(message)
.then(result => {
// this works perfectly and is recognized by the swift function //
res.send({publishableKey: publishableKey, clientSecret: clientSecret});
})
.catch(err => {
// this is not recognized by the swift function //
console.log(err) // full of data
return res.status(400).send({error: err});
});

日志(针对错误情况(:

函数执行耗时240毫秒,完成状态代码:400

如果您的请求失败,我认为您的错误将进入响应参数,您必须对其进行解码。我认为只有在无法访问服务器或功能不存在的情况下,错误参数才会与nil不同。因此,基本上在else子句中,您必须搜索响应参数中的错误。

相关内容

  • 没有找到相关文章

最新更新