Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialCarson Carbery
9,876 PointsError handling
In this tutorial, Pasan leaves it to us to sort out the error handling, related to a no error and no data case. Has anybody done that, could you share your code please. I'm really not sure what I should be entering here and when ever I am testing error cases this is the one that comes up the most. Many thanks for any help with this. Kind regards
3 Answers
Hunter Fortuin
4,486 PointsAnother way to handle it would be to instantiate a new NSError object that can be passed to the completion handler. Underneath the public constant for MissingHTTPResponseError
create another constant:
public let MissingJsonAndErrorVariablesError: Int = 11
Now where the TODO comment is:
let userInfo = [NSLocalizedDescriptionKey: NSLocalizedString("Both JSON and Error Variables Are Set to Nil", comment: "")]
let newError = NSError(domain: TRENetworkingErrorDomain, code: MissingJsonAndErrorVariablesError, userInfo: userInfo)
completion(.Failure(newError))
Anthony Boutinov
13,844 PointsSince Pasan said that with the way we structured our code in JSONTask(:completion:)
method, there should be no way for the program to reach this line of code. So my proposal here is to simply call fatalError
. If it so happens that the application we use this code in reaches this particular line, it would mean that we have made a logical error when modifying JSONTask(:completion:)
method. And instead of trying to handle this case "gracefully", what we really should do is review that method.
That would be something like that:
fatalError("Both `json` and `error` variables are set to nil")
jakesager
12,023 PointsI'm a few months late, but what I decided to do is create a .failureWithoutError
clause in the result enum, and I handled this error later like this in the view controller upon calling forecastClient.fetchCurrentWeather()
.
case.failureWithoutError:
DispatchQueue.main.async {
self.showAlert(title: "Forecast Failed", message: "An unknown error has occurred. Try again.")
self.toggleRefreshIndicator(on: false)
}
As for the DispatchQueue.main.async
line, that's the way of running async on the main thread in Swift 3.
As for the answer above, while there is no way the code really should've / could've reached that location, it's just a simple networking error - no reason to crash the app if you don't get an error back while loading network data. The user can always try reloading again and it might work.