Skip to content

Commit

Permalink
Adding internal implementation layers and a private DirectionsError
Browse files Browse the repository at this point in the history
… protocol to allow for custom directions services to provide their own errors
  • Loading branch information
Jerrad Thramer authored and Jerrad Thramer committed Dec 13, 2019
1 parent dc553c7 commit 110566d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
39 changes: 24 additions & 15 deletions Sources/MapboxDirections/Directions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ open class Directions: NSObject {

// MARK: Getting Directions

internal typealias RouteCompletionHandlerInteral = (_ waypoints: [Waypoint]?, _ routes: [Route]?, _ error: DirectionsServiceError?) -> Void

This comment was marked as outdated.

Copy link
@1ec5

1ec5 Dec 14, 2019

Contributor

Not sure what needs to be internal about this. Isn’t DirectionsServiceError already public?

/**
Begins asynchronously calculating routes using the given options and delivers the results to a closure.
Expand All @@ -153,22 +154,24 @@ open class Directions: NSObject {
- returns: The data task used to perform the HTTP request. If, while waiting for the completion handler to execute, you no longer want the resulting routes, cancel this task.
*/
@discardableResult open func calculate(_ options: RouteOptions, completionHandler: @escaping RouteCompletionHandler) -> URLSessionDataTask {

let complete = completionHandler as RouteCompletionHandlerInteral
let fetchStart = Date()
let request = urlRequest(forCalculating: options)
let requestTask = URLSession.shared.dataTask(with: request) { (possibleData, possibleResponse, possibleError) in
let responseEndDate = Date()
guard let response = possibleResponse, ["application/json", "text/html"].contains(response.mimeType) else {
completionHandler(nil, nil, .invalidResponse)
complete(nil, nil, .invalidResponse)
return
}

guard let data = possibleData else {
completionHandler(nil, nil, .noData)
complete(nil, nil, .noData)
return
}

if let error = possibleError {
completionHandler(nil, nil, .unknown(response: possibleResponse, underlying: error, code: nil, message: nil))
complete(nil, nil, .unknown(response: possibleResponse, underlying: error, code: nil, message: nil))
return
}

Expand All @@ -183,7 +186,7 @@ open class Directions: NSObject {
}

guard let routes = result.routes else {
completionHandler(result.waypoints, nil, .unableToRoute)
complete(result.waypoints, nil, .unableToRoute)
return
}

Expand All @@ -206,6 +209,8 @@ open class Directions: NSObject {
return requestTask
}


internal typealias MatchCompletionHandlerInternal = (_ matches: [Match]?, _ error: DirectionsServiceError?) -> Void
/**
Begins asynchronously calculating matches using the given options and delivers the results to a closure.
Expand All @@ -218,22 +223,24 @@ open class Directions: NSObject {
- returns: The data task used to perform the HTTP request. If, while waiting for the completion handler to execute, you no longer want the resulting matches, cancel this task.
*/
@discardableResult open func calculate(_ options: MatchOptions, completionHandler: @escaping MatchCompletionHandler) -> URLSessionDataTask {

let complete = completionHandler as MatchCompletionHandlerInternal
let fetchStart = Date()
let request = urlRequest(forCalculating: options)
let requestTask = URLSession.shared.dataTask(with: request) { (possibleData, possibleResponse, possibleError) in
let responseEndDate = Date()
guard let response = possibleResponse, response.mimeType == "application/json" else {
completionHandler(nil, .invalidResponse)
complete(nil, .invalidResponse)
return
}

guard let data = possibleData else {
completionHandler(nil, .noData)
complete(nil, .noData)
return
}

if let error = possibleError {
completionHandler(nil, .unknown(response: possibleResponse, underlying: error, code: nil, message: nil))
complete(nil, .unknown(response: possibleResponse, underlying: error, code: nil, message: nil))
return
}

Expand All @@ -248,7 +255,7 @@ open class Directions: NSObject {
}

guard let matches = result.matches else {
completionHandler(nil, .unableToRoute)
complete(nil, .unableToRoute)
return
}

Expand All @@ -259,7 +266,7 @@ open class Directions: NSObject {
}
} catch {
DispatchQueue.main.async {
completionHandler(nil, .unknown(response: response, underlying: error, code: nil, message: nil))
complete(nil, .unknown(response: response, underlying: error, code: nil, message: nil))
}
}
}
Expand All @@ -282,22 +289,24 @@ open class Directions: NSObject {
- returns: The data task used to perform the HTTP request. If, while waiting for the completion handler to execute, you no longer want the resulting routes, cancel this task.
*/
@discardableResult open func calculateRoutes(matching options: MatchOptions, completionHandler: @escaping RouteCompletionHandler) -> URLSessionDataTask {

let complete = completionHandler as RouteCompletionHandlerInteral
let fetchStart = Date()
let request = urlRequest(forCalculating: options)
let requestTask = URLSession.shared.dataTask(with: request) { (possibleData, possibleResponse, possibleError) in
let responseEndDate = Date()
guard let response = possibleResponse, response.mimeType == "application/json" else {
completionHandler(nil, nil, .invalidResponse)
complete(nil, nil, .invalidResponse)
return
}

guard let data = possibleData else {
completionHandler(nil, nil, .noData)
complete(nil, nil, .noData)
return
}

if let error = possibleError {
completionHandler(nil, nil, .unknown(response: possibleResponse, underlying: error, code: nil, message: nil))
complete(nil, nil, .unknown(response: possibleResponse, underlying: error, code: nil, message: nil))
return
}

Expand All @@ -312,7 +321,7 @@ open class Directions: NSObject {
}

guard let routes = result.routes else {
completionHandler(result.waypoints, nil, .unableToRoute)
complete(result.waypoints, nil, .unableToRoute)
return
}

Expand All @@ -323,7 +332,7 @@ open class Directions: NSObject {
}
} catch {
DispatchQueue.main.async {
completionHandler(nil, nil, .unknown(response: response, underlying: error, code: nil, message: nil))
complete(nil, nil, .unknown(response: response, underlying: error, code: nil, message: nil))
}
}
}
Expand Down Expand Up @@ -402,7 +411,7 @@ open class Directions: NSObject {
/**
Returns an error that supplements the given underlying error with additional information from the an HTTP response’s body or headers.
*/
static func informativeError(code: String?, message: String?, response: URLResponse?, underlyingError error: Error?) -> DirectionsError {
static func informativeError(code: String?, message: String?, response: URLResponse?, underlyingError error: Error?) -> DirectionsServiceError {
if let response = response as? HTTPURLResponse {
switch (response.statusCode, code ?? "") {
case (200, "NoRoute"):
Expand Down
9 changes: 6 additions & 3 deletions Sources/MapboxDirections/DirectionsError.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import Foundation

public protocol DirectionsError: LocalizedError { }


/**
An error that occurs when calculating directions.
*/
public enum DirectionsError: LocalizedError {
public enum DirectionsServiceError: DirectionsError {
/**
The server returned an empty response.
*/
Expand Down Expand Up @@ -131,8 +134,8 @@ public enum DirectionsError: LocalizedError {
}
}

extension DirectionsError: Equatable {
public static func == (lhs: DirectionsError, rhs: DirectionsError) -> Bool {
extension DirectionsServiceError: Equatable {
public static func == (lhs: DirectionsServiceError, rhs: DirectionsServiceError) -> Bool {
switch (lhs, rhs) {
case (.noData, .noData),
(.invalidResponse, .invalidResponse),
Expand Down

0 comments on commit 110566d

Please sign in to comment.