Cloud Function post 请求未正确向 IOS 应用程序发送错误消息
Cloud Function post request not sending error message properly to IOS app
我有一个 iOS 应用程序从 google 云函数接收 json 数据。
数据已检索并且在没有错误的情况下完美运行。
然而,当云函数returns出错时,下面的函数将error识别为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()
}
客户端输出:
Failed to decode response from server.
服务器:
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});
});
日志(针对错误情况):
Function execution took 240 ms, finished with status code: 400
如果您的请求失败,我认为您的错误将进入响应参数,您必须对其进行解码。我认为仅当无法访问服务器或函数不存在时,错误参数才会与 nil 不同。所以基本上在 else 子句中你将不得不搜索错误的响应参数。
我有一个 iOS 应用程序从 google 云函数接收 json 数据。
数据已检索并且在没有错误的情况下完美运行。
然而,当云函数returns出错时,下面的函数将error识别为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()
}
客户端输出:
Failed to decode response from server.
服务器:
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});
});
日志(针对错误情况):
Function execution took 240 ms, finished with status code: 400
如果您的请求失败,我认为您的错误将进入响应参数,您必须对其进行解码。我认为仅当无法访问服务器或函数不存在时,错误参数才会与 nil 不同。所以基本上在 else 子句中你将不得不搜索错误的响应参数。