Skip to content

Commit

Permalink
better error handling and return of optional when no route is found
Browse files Browse the repository at this point in the history
  • Loading branch information
gentges committed Dec 29, 2019
1 parent 9021fcc commit de74d81
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ extension LicensePlate: Content {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

if let lastCharacterValue = try? container.decodeIfPresent(Int.self, forKey: .lastCharacter) {
self = .lastCharacter(lastCharacterValue)
} else {
throw fatalError()
guard let lastCharacterValue = try container.decodeIfPresent(Int.self, forKey: .lastCharacter) else {
let context = DecodingError.Context(codingPath: [CodingKeys.lastCharacter], debugDescription: "No last character found for license plate.")
throw DecodingError.valueNotFound(Int.self, context)
}

self = .lastCharacter(lastCharacterValue)
}

public func encode(to encoder: Encoder) throws {
Expand Down
20 changes: 14 additions & 6 deletions Sources/HereProvider/Models/Routing/RoutingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,37 @@ public final class HereRoutingClient: Service {
self.appCode = config.appCode
}

public func calculateRoute<W: WaypointParameterType, C: ConsumptionModelDetailType, M: ManeuverType, L: RouteLinkType, S: RouteSummaryType>(_ input: RouteCalculationRequest<W, C>) throws -> Future<HereRoutingResponse<CalculateRouteResponseType<M, L, S>>> {
public func calculateRoute<W: WaypointParameterType, C: ConsumptionModelDetailType, M: ManeuverType, L: RouteLinkType, S: RouteSummaryType>(_ input: RouteCalculationRequest<W, C>) throws -> Future<HereRoutingResponse<CalculateRouteResponseType<M, L, S>>?> {
let urlString = calculateRouteEndpoint + format.rawValue + "?app_id=\(appId)&app_code=\(appCode)" + input.requestParameters
guard let requestURL = URL(string: urlString) else {
throw Abort(.internalServerError, reason: "Could not create request URL.")
}

return httpClient.get(requestURL).flatMap { response in
guard response.http.status == .ok else {
return try response.content.decode(InvalidInputData.self).flatMap { error in
return try response.content.decode(RoutingServiceError.self).flatMap { error in
var possibleCauses = [String]()
if let additionalData = error.additionalData {
possibleCauses = additionalData.map { data in
"\(data.key): \(data.value)"
}
}

throw Abort(response.http.status, reason: error.details, possibleCauses: possibleCauses)
}.catchMap { error in
throw Abort(response.http.status)
switch error.type {
case .ApplicationError:
switch error.subtype {
case "NoRouteFound":
return self.httpClient.container.future(nil)
default:
throw Abort(response.http.status, reason: error.details, possibleCauses: possibleCauses)
}
default:
throw Abort(response.http.status, reason: error.details, possibleCauses: possibleCauses)
}
}
}

return try response.content.decode(HereRoutingResponse.self).catchMap { error in
return try response.content.decode(HereRoutingResponse?.self).catchMap { error in
throw Abort(.internalServerError, reason: "Could not decode route response.")
}
}
Expand Down

0 comments on commit de74d81

Please sign in to comment.