You usually use (your own) completion handler to tell error to the caller, but in your case, adding a new @Published property holding the error would be an alternative. So in my class I should be adding a @Published property variable to handle errors ?
This is why I would like to implement a throw scenario to my request
Button(action: {
self.showGroup = false
if (connexion.checkEmpty) {
				do {
try self.connect(url: "url")
} catch {
self.showError = true
}
	 } else {
self.showEmpty = true
}
}) {
Text("button")
}
If iOS doesn't allow me to make throws is it better to make small functions for error management.
URLSession.shared.dataTask(with: request) { data, res, error in
guard let httpResponse = res as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
self.handleServerError(res)
return
func handleServerError(_ res: URLResponse?) {
print("ERROR: Status Code: \(res!): the status code MUST be between 200 and 299")
}
Also, why can't I implement throws in my function when it is throwing ? func connect(url: String) throws {
This is an exemple of the POST
{"email":"XXXX@hell.com","password":"123456!"}
Settings :
KEY = Content-Type
VALUE = application/json
Here is and example of the dataString I'm receiving:
[{"idTransfert":[],"_id":"60099ddc0eb9734ede9e831d","email":"test@test.eu","password":"4f8c9f9b94527112dbc","tokens":[{"_id":"60099de20eb9734ede9e8320","token":"eyJhbGciVCJ9.eyJfaWQ9.nEh2K7W0"},{"_id":"60099e160eb9734ede9e8323","token":"eyJhbGciOiJI.eyJfaWQiOiI2M9.nkJRk134b0"}],"sessions":[{"_id":"60099e160eb9734ede9e8325","sessionId":"nQIGGmN-766Z6nLwJBwSuLsQwlwP-WFu"}],"__v":2}]
Here another version of my class:
class Connexion: Codable, ObservableObject {
enum CodingKeys: String, CodingKey {
case email, password, id, token, sessionID
}
/* @Published var token: [Token] = [.init()] */
/* @Published var sessionID: [SessionID] = [.init()]*/
@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([SessionID].self, forKey: .sessionID)*/
/* token = try container.decode([Token].self, forKey: .token)*/
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)
}
var checkEmpty: Bool {
if (email.isEmpty || password.isEmpty) {
return false
}
return true
}
}