Add a little error handling for invalid auth

When changing my api keys, realized I couldn't reauth. Now on a bad
auth, we clear the keychain so that we can reauth the next time
This commit is contained in:
IamTheFij 2018-01-20 18:42:55 -08:00
parent bc5e9e9d2b
commit 098d9c3ea6
2 changed files with 13 additions and 2 deletions

View File

@ -168,6 +168,7 @@ class FitbitClient {
Alamofire.request(urlString) Alamofire.request(urlString)
.validate()
.validate(contentType: ["application/json"]) .validate(contentType: ["application/json"])
.responseJSON(completionHandler: handler) .responseJSON(completionHandler: handler)
} }

View File

@ -11,7 +11,7 @@ import HealthKit
import UIKit import UIKit
class ViewController: UIViewController { class ViewController: UIViewController {
@IBOutlet var loggedIn: UILabel! @IBOutlet var loggedIn: UILabel!
@IBOutlet var outputText: UITextView! @IBOutlet var outputText: UITextView!
let client = FitbitClient() let client = FitbitClient()
@ -88,8 +88,18 @@ class ViewController: UIViewController {
response in FitbitClient.weightResponseHandler(response: response) { response in FitbitClient.weightResponseHandler(response: response) {
fbWeights, error in fbWeights, error in
if let error = error { if let error = error {
NSLog("Unable to get weights: \(error)") NSLog("Unable to get weights: \(error.localizedDescription)")
if let error = error as? AFError {
if let suggestion = error.recoverySuggestion {
NSLog("Suggestion to resolve: \(suggestion)")
}
if error.responseCode == 401 {
NSLog("Unauthorized. Clearing credentials")
self.client.clearCredential()
}
}
self.outputText.text = "Error getting weights" self.outputText.text = "Error getting weights"
return return
} }