Skip to content

Commit

Permalink
Handle exceptions during creation of tyr actor
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophKaser committed Jan 28, 2025
1 parent b4bf7fa commit 18440eb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
14 changes: 10 additions & 4 deletions apple/Sources/Valhalla/Valhalla.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public protocol ValhallaProviding {

init(_ config: ValhallaConfig) throws

init(configPath: String)
init(configPath: String) throws

func route(request: RouteRequest) throws -> RouteResponse
}
Expand All @@ -17,10 +17,10 @@ public final class Valhalla: ValhallaProviding {

public convenience init(_ config: ValhallaConfig) throws {
let configURL = try ValhallaFileManager.saveConfigTo(config)
self.init(configPath: configURL.relativePath)
try self.init(configPath: configURL.relativePath)
}

public required init(configPath: String) {
public required init(configPath: String) throws {
do {
try ValhallaFileManager.injectTzdataIntoLibrary()
} catch {
Expand All @@ -29,7 +29,13 @@ public final class Valhalla: ValhallaProviding {
}

self.configPath = configPath
self.actor = ValhallaWrapper(configPath: configPath)
do {
self.actor = try ValhallaWrapper(configPath: configPath)
} catch let error as NSError {
throw ValhallaError.valhallaError(error.code, error.domain)
} catch {
throw ValhallaError.valhallaError(-1, error.localizedDescription)
}
}

public func route(request: RouteRequest) throws -> RouteResponse {
Expand Down
18 changes: 16 additions & 2 deletions apple/Sources/ValhallaObjc/ValhallaWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@

@implementation ValhallaWrapper

- (instancetype)initWithConfigPath:(NSString*)config_path
- (instancetype)initWithConfigPath:(NSString*)config_path error:(__autoreleasing NSError **)error
{
self = [super init];
std::string path = std::string([config_path UTF8String]);
_actor = create_valhalla_actor(path.c_str());
try {
_actor = create_valhalla_actor(path.c_str());
} catch (NSException *exception) {
*error = [[NSError alloc] initWithDomain:exception.name code:0 userInfo:@{
NSUnderlyingErrorKey: exception,
NSLocalizedDescriptionKey: exception.reason,
@"CallStackSymbols": exception.callStackSymbols
}];
} catch (const std::exception &err) {
*error = [[NSError alloc] initWithDomain: [NSString stringWithUTF8String:err.what()] code:-1 userInfo: nil];
return nil;
} catch (...) {
*error = [[NSError alloc] initWithDomain: @"unknown exception" code:-1 userInfo: nil];
return nil;
}
return self;
}

Expand Down
2 changes: 1 addition & 1 deletion apple/Sources/ValhallaObjc/include/ValhallaWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
void* _actor;
}

- (instancetype)initWithConfigPath:(NSString*)config_path;
- (instancetype)initWithConfigPath:(NSString*)config_path error:(__autoreleasing NSError **)error;

- (NSString*)route:(NSString*)request;

Expand Down
20 changes: 10 additions & 10 deletions apple/Tests/ValhallaTests/TestValhallaWithTar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ final class TestValhallaWithTar: XCTestCase {

/// Validate an incorrect configuration (config file not found).
func testNoConfigFile() throws {
let valhalla = Valhalla(configPath: "missing.json")
do {
let valhalla = try Valhalla(configPath: "missing.json")

let request = RouteRequest(
locations: [
RoutingWaypoint(lat: 38.429719, lon: -108.827425),
RoutingWaypoint(lat: 38.4604331, lon: -108.8817009)
],
costing: .auto,
directionsOptions: DirectionsOptions(units: .mi)
)
let request = RouteRequest(
locations: [
RoutingWaypoint(lat: 38.429719, lon: -108.827425),
RoutingWaypoint(lat: 38.4604331, lon: -108.8817009)
],
costing: .auto,
directionsOptions: DirectionsOptions(units: .mi)
)

do {
let _ = try valhalla.route(request: request)
XCTFail("route should throw cannot open file missing.json")
} catch let error as ValhallaError {
Expand Down

0 comments on commit 18440eb

Please sign in to comment.