Skip to content

Commit abe4da1

Browse files
committed
Handle errors
1 parent ce60910 commit abe4da1

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

MapboxGeocoder/MBGeocoder.swift

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,33 @@ open class Geocoder: NSObject {
244244
request.setValue(userAgent, forHTTPHeaderField: "User-Agent")
245245
return URLSession.shared.dataTask(with: request) { (data, response, error) in
246246

247-
// TODO: Migrate result to Codable
248-
// let apiMessage = json["message"] as? String
249-
// guard data != nil && error == nil && apiMessage == nil else {
250-
// let apiError = Geocoder.descriptiveError(json, response: response, underlyingError: error as NSError?)
251-
// DispatchQueue.main.async {
252-
// errorHandler(apiError)
253-
// }
254-
// return
255-
// }
247+
guard let data = data else { return }
248+
let decoder = JSONDecoder()
256249

257-
DispatchQueue.main.async {
258-
completionHandler(data)
250+
do {
251+
let result = try decoder.decode(GeocodeAPIResult.self, from: data)
252+
guard result.message == nil else {
253+
let apiError = Geocoder.descriptiveError(["message": result.message!], response: response, underlyingError: error as NSError?)
254+
DispatchQueue.main.async {
255+
errorHandler(apiError)
256+
}
257+
return
258+
}
259+
DispatchQueue.main.async {
260+
completionHandler(data)
261+
}
262+
} catch {
263+
DispatchQueue.main.async {
264+
errorHandler(error as NSError)
265+
}
259266
}
260267
}
261268
}
262269

270+
internal struct GeocodeAPIResult: Codable {
271+
let message: String?
272+
}
273+
263274
/**
264275
The HTTP URL used to fetch the geocodes from the API.
265276
*/

0 commit comments

Comments
 (0)