Hello,
I encounter two issues with my networking function.
The function is working properly when all entries are correctly entered.
Now I would like to implement my error management.
I'm having this first issue:
Invalid conversion from throwing function of type '(Data?, URLResponse?, Error?) throws -> Void' to non-throwing function type '(Data?, URLResponse?, Error?) -> Void' (line 15)
Here is the code :
This is the enum would like to use for the error handling:
This is my class:
https://developer.apple.com/forums/thread/671055
The second error, is coming out of nowhere... (if you don't know it is okay)
When using Postman, I get this:
But when I print the dataString (line 30) it's another story. I get almost all the BDD ...
I encounter two issues with my networking function.
The function is working properly when all entries are correctly entered.
Now I would like to implement my error management.
I'm having this first issue:
Invalid conversion from throwing function of type '(Data?, URLResponse?, Error?) throws -> Void' to non-throwing function type '(Data?, URLResponse?, Error?) -> Void' (line 15)
Here is the code :
Code Block Swift func connect(url: String) throws { guard let encoded = try? JSONEncoder().encode(connexion) else { print("Fail to encode newMed") return } let url = URL(string: "/api/\(url)")! var request = URLRequest(url: url) request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.httpMethod = "POST" request.httpBody = encoded URLSession.shared.dataTask(with: url) { data, res, error in guard let httpResponse = res as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else if (res == nil) { throw ConnexionError.invalidServerRes /* here is the error */ } else { self.handleServerError(res!) return } if let data = data { let decoder = JSONDecoder() if let json = try? decoder.decode(Connexion.self, from: data) { print(json) self.connexion.id = json.id self.connexion.token = json.token self.connexion.sessionID = json.sessionID self.signInSuccess = true } else { let dataString = String(decoding: data, as: UTF8.self) print("Invalid response \(dataString)") } } }.resume() } }
This is the enum would like to use for the error handling:
Code Block enum ConnexionError: Error { case invalidPasswd case invalidId case invalidServerRes }
This is my class:
Code Block class Connexion: Codable, ObservableObject { enum CodingKeys: String, CodingKey { case email, password, id, token, sessionID } @Published var token: String = "" @Published var sessionID: String = "" @Published var id: String = "" @Published var email: String = "" @Published var password: String = "" init() { } required init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) sessionID = try container.decode(String.self, forKey: .sessionID) token = try container.decode(String.self, forKey: .token) id = try container.decode(String.self, forKey: .id) } func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(email, forKey: .email) try container.encode(password, forKey: .password) } }
https://developer.apple.com/forums/thread/671055
The second error, is coming out of nowhere... (if you don't know it is okay)
When using Postman, I get this:
Code Block { "id": "5fc565209ce8b43c1315da9b", "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9", "sessionID": "O7KdKIuDMVHeVV91AWcApPYezCXEfz7n" }
But when I print the dataString (line 30) it's another story. I get almost all the BDD ...
If you declare your connect as throws, it should:Also, why can't I implement throwsin my function when it is throwing ?
return normally
return with error (when error thrown)
If you are having such use case in mind, completion handler pattern might be easier:This is why I would like to implement a throw scenario to my request
Code Block func connect(url: String, onError errorHandler: @escaping (Error?)->Void) { //<- let encoded: Data do { encoded = try JSONEncoder().encode(connexion) } catch { print("Fail to encode newMed: \(error)") return } let url = URL(string: "/api/\(url)")! var request = URLRequest(url: url) request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.httpMethod = "POST" request.httpBody = encoded URLSession.shared.dataTask(with: url) { data, res, error in if let error = error { errorHandler(error) return } guard let response = res else { errorHandler(ConnexionError.invalidServerRes) //<- return } guard let httpResponse = response as? HTTPURLResponse, case 200...299 = httpResponse.statusCode else { self.handleServerError(res!) return } guard let data = data else { print("data is nil") return } let decoder = JSONDecoder() do { let json = try decoder.decode(Connexion.self, from: data) print(json) self.connexion.id = json.id self.connexion.token = json.token self.connexion.sessionID = json.sessionID self.signInSuccess = true } catch { let dataString = String(data: data, encoding: .utf8) ?? "Unknown encoding" print("Invalid response \(error) - \(dataString)") } }.resume() }
And use it as:
Code Block Button(action: { self.showGroup = false if connexion.isNotEmpty { self.connect(url: "url", onError: { err in self.showError = true }) } else { self.showEmpty = true } }) { Text("button") }
Sorry, far from I expected. Please show all the customizable things of Postman, including Http Method, Authorization, Header and Body.Settings :