Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handing in case of invalid route responses #4618

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Sources/MapboxCoreNavigation/LegacyRouteController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,24 @@ open class LegacyRouteController: NSObject, Router, InternalRouter, CLLocationMa
updateRoute(with: indexedRouteResponse,
routeOptions: routeOptions,
isProactive: false,
isAlternative: false,
completion: completion)
isAlternative: false) { result in
completion?(result.isSuccess)
}
}

func updateRoute(with indexedRouteResponse: IndexedRouteResponse,
routeOptions: RouteOptions?,
isProactive: Bool,
isAlternative: Bool,
completion: ((Bool) -> Void)?) {
completion: ((Result<Void, Error>) -> Void)?) {
guard let route = indexedRouteResponse.currentRoute else {
preconditionFailure("`indexedRouteResponse` does not contain route for index `\(indexedRouteResponse.routeIndex)` when updating route.")
}
let routeOptions = routeOptions ?? routeProgress.routeOptions
routeProgress = RouteProgress(route: route, options: routeOptions)
self.indexedRouteResponse = indexedRouteResponse
announce(reroute: route, at: location, proactive: isProactive)
completion?(true)
completion?(.success(()))
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ extension Result {
preconditionFailure("Expected type is neither a value nor an error.")
}
}

var isSuccess: Bool {
switch self {
case .success:
return true
case .failure:
return false
}
}
}
26 changes: 15 additions & 11 deletions Sources/MapboxCoreNavigation/RouteController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -867,14 +867,17 @@ extension RouteController: Router {
case let .success(indexedResponse):
let response = indexedResponse.routeResponse
guard case let .route(routeOptions) = response.options else {
//TODO: Can a match hit this codepoint?
self.isRerouting = false; return
self.isRerouting = false
return announceReroutingError(with: ReroutingError.routeError)
azarovalex marked this conversation as resolved.
Show resolved Hide resolved
}
self.updateRoute(with: indexedResponse,
routeOptions: routeOptions,
isProactive: false,
isAlternative: false) { [weak self] success in
isAlternative: false) { [weak self] result in
self?.isRerouting = false
if case let .failure(error) = result {
self?.announceReroutingError(with: error)
}
}
case let .failure(error):
self.announceReroutingError(with: error)
Expand All @@ -890,15 +893,16 @@ extension RouteController: Router {
updateRoute(with: indexedRouteResponse,
routeOptions: routeOptions,
isProactive: false,
isAlternative: false,
completion: completion)
isAlternative: false) { result in
completion?(result.isSuccess)
}
}

func updateRoute(with indexedRouteResponse: IndexedRouteResponse,
routeOptions: RouteOptions?,
isProactive: Bool,
isAlternative: Bool,
completion: ((Bool) -> Void)?) {
completion: ((Result<Void, Error>) -> Void)?) {
guard let route = indexedRouteResponse.currentRoute else {
preconditionFailure("`indexedRouteResponse` does not contain route for index `\(indexedRouteResponse.routeIndex)` when updating route.")
}
Expand Down Expand Up @@ -926,9 +930,9 @@ extension RouteController: Router {
self.announce(reroute: route, at: self.location, proactive: isProactive)
self.indexedRouteResponse = indexedRouteResponse
self.didProactiveReroute = isProactive
completion?(true)
case .failure:
completion?(false)
completion?(.success(()))
case .failure(let error):
completion?(.failure(error))
}
}
}
Expand Down Expand Up @@ -1014,11 +1018,11 @@ extension RouteController: ReroutingControllerDelegate {
routeOptions: options,
isProactive: false,
isAlternative: true,
completion: { [weak self] success in
completion: { [weak self] result in
guard let self = self else { return }
var userInfo = [RouteController.NotificationUserInfoKey: Any]()
userInfo[.locationKey] = self.location
if success {
if result.isSuccess {
NotificationCenter.default.post(name: .routeControllerDidTakeAlternativeRoute,
object: self,
userInfo: userInfo)
Expand Down
2 changes: 1 addition & 1 deletion Sources/MapboxCoreNavigation/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ protocol InternalRouter: AnyObject {
routeOptions: RouteOptions?,
isProactive: Bool,
isAlternative: Bool,
completion: ((Bool) -> Void)?)
completion: ((Result<Void, Error>) -> Void)?)
}

extension InternalRouter where Self: Router {
Expand Down