Skip to content

Conversation

@NorlanT
Copy link

@NorlanT NorlanT commented Jul 14, 2020

Copy link

@swift-student swift-student left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice start to this project, Norlan!

Comment on lines +46 to +78
do {
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let jsonData = try encoder.encode(user)
print(String(data: jsonData, encoding: .utf8)!)
request.httpBody = jsonData

let task = URLSession.shared.dataTask(with: request) { (_, response, error) in

// Check for error first
if let error = error {
print("Sing up failed with error: \(error)")
completion(.failure(.failedSignUp))
return
}

// Check for response and make sure is a 200
guard let response = response as? HTTPURLResponse,
response.statusCode == 200 else {
print("Sign up was unsuccessful")
completion(.failure(.failedSignUp))
return
}

completion(.success(true))
}

task.resume()

} catch {
print("Error encoding user: \(error)")
completion(.failure(.failedSignUp))
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While there is nothing wrong with the way you wrote this, I would personally prefer to wrap the do catch block more directly around the line doing the try to make it clear what is going to throw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants