Skip to content

Commit

Permalink
Reorder failure errors (#10)
Browse files Browse the repository at this point in the history
When there are no locations found for a query, MapKit returns a NSError
instead of an empty array. This resulted in us printing a opaque error.
Now we'll do that last, only if there is an error, but we also found a
place.

Here's an example of a query that currently fails:

Lyft HQ San Francisco
  • Loading branch information
keith authored Aug 31, 2018
1 parent 5f5b05f commit c3c2d4f
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions sources/query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ func findLocation(from arguments: [String]) -> Result<CLLocationCoordinate2D> {
let search = MKLocalSearch(request: request)
let (response, error) = search.perform()

if let error = error {
return .failure(error.localizedDescription)
}

guard let placemark = response?.mapItems.first?.placemark else {
return .failure("No locations found for '\(query)'")
}
Expand All @@ -26,6 +22,10 @@ func findLocation(from arguments: [String]) -> Result<CLLocationCoordinate2D> {
return .failure("No coordinate found for '\(placemark.name ?? "")'")
}

if let error = error {
return .failure(error.localizedDescription)
}

return .success(coordinate)
}

Expand Down

0 comments on commit c3c2d4f

Please sign in to comment.