From 3419722e775bbc976a7c67222b72b39eaea5ec8e Mon Sep 17 00:00:00 2001 From: Jamie McDonald Date: Sun, 26 Jan 2025 15:39:17 -0500 Subject: [PATCH] Fix crash on invalid custom GTFS URL --- macOS/Arrivals.xcodeproj/project.pbxproj | 8 ++++---- .../com/jdamcd/arrivals/gtfs/GtfsArrivals.kt | 16 ++++++++++------ .../com/jdamcd/arrivals/gtfs/GtfsArrivalsTest.kt | 10 ++++++++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/macOS/Arrivals.xcodeproj/project.pbxproj b/macOS/Arrivals.xcodeproj/project.pbxproj index 4a60cec..02dc972 100644 --- a/macOS/Arrivals.xcodeproj/project.pbxproj +++ b/macOS/Arrivals.xcodeproj/project.pbxproj @@ -468,7 +468,7 @@ "CODE_SIGN_IDENTITY[sdk=macosx*]" = "-"; CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 4; + CURRENT_PROJECT_VERSION = 5; DEAD_CODE_STRIPPING = YES; DEVELOPMENT_ASSET_PATHS = "\"Arrivals/Preview Content\""; DEVELOPMENT_TEAM = ""; @@ -486,7 +486,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - MARKETING_VERSION = 0.4; + MARKETING_VERSION = 0.5; OTHER_LDFLAGS = ( "$(inherited)", "-framework", @@ -511,7 +511,7 @@ "CODE_SIGN_IDENTITY[sdk=macosx*]" = "-"; CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 4; + CURRENT_PROJECT_VERSION = 5; DEAD_CODE_STRIPPING = YES; DEVELOPMENT_ASSET_PATHS = "\"Arrivals/Preview Content\""; DEVELOPMENT_TEAM = ""; @@ -530,7 +530,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - MARKETING_VERSION = 0.4; + MARKETING_VERSION = 0.5; OTHER_LDFLAGS = ( "$(inherited)", "-framework", diff --git a/shared/src/commonMain/kotlin/com/jdamcd/arrivals/gtfs/GtfsArrivals.kt b/shared/src/commonMain/kotlin/com/jdamcd/arrivals/gtfs/GtfsArrivals.kt index 7eeb1b9..5e1b68d 100644 --- a/shared/src/commonMain/kotlin/com/jdamcd/arrivals/gtfs/GtfsArrivals.kt +++ b/shared/src/commonMain/kotlin/com/jdamcd/arrivals/gtfs/GtfsArrivals.kt @@ -31,16 +31,20 @@ internal class GtfsArrivals( throw NoDataException("No arrivals found") } } catch (e: Exception) { - throw NoDataException("No connection") + throw NoDataException("Failed to connect") } } private suspend fun updateStops() { - if (!hasFreshStops()) { - stops = GtfsStops(api.downloadStops(settings.gtfsSchedule)) - settings.gtfsStopsUpdated = clock.now().epochSeconds - } else if (!::stops.isInitialized) { - stops = GtfsStops(api.readStops()) + try { + if (!hasFreshStops()) { + stops = GtfsStops(api.downloadStops(settings.gtfsSchedule)) + settings.gtfsStopsUpdated = clock.now().epochSeconds + } else if (!::stops.isInitialized) { + stops = GtfsStops(api.readStops()) + } + } catch (e: Exception) { + throw NoDataException("Failed to load stops") } } diff --git a/shared/src/jvmTest/kotlin/com/jdamcd/arrivals/gtfs/GtfsArrivalsTest.kt b/shared/src/jvmTest/kotlin/com/jdamcd/arrivals/gtfs/GtfsArrivalsTest.kt index e7b2732..2127f00 100644 --- a/shared/src/jvmTest/kotlin/com/jdamcd/arrivals/gtfs/GtfsArrivalsTest.kt +++ b/shared/src/jvmTest/kotlin/com/jdamcd/arrivals/gtfs/GtfsArrivalsTest.kt @@ -98,4 +98,14 @@ class GtfsArrivalsTest { coVerify { api.downloadStops("schedule_url") } latest.arrivals shouldHaveSize 3 } + + @Test + fun `throws NoDataException if stops fail to load`() = runBlocking { + every { clock.now() } returns Instant.fromEpochSeconds(fetchTime) + coEvery { api.downloadStops("schedule_url") } throws Exception() + + assertFailsWith { + arrivals.latest() + } + } }