Skip to content

Commit aea875a

Browse files
committed
Exception handling bug fix
1 parent 3419722 commit aea875a

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

desktop/src/main/kotlin/com/jdamcd/arrivals/desktop/ArrivalsViewModel.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.viewModelScope
55
import com.jdamcd.arrivals.Arrivals
66
import com.jdamcd.arrivals.ArrivalsInfo
7+
import com.jdamcd.arrivals.NoDataException
78
import kotlinx.coroutines.Job
89
import kotlinx.coroutines.delay
910
import kotlinx.coroutines.flow.MutableStateFlow
@@ -43,9 +44,11 @@ class ArrivalsViewModel(private val arrivals: Arrivals) : ViewModel() {
4344
try {
4445
val result = arrivals.latest()
4546
_uiState.value = ArrivalsState.Data(result, false)
47+
} catch (e: NoDataException) {
48+
_uiState.value = ArrivalsState.Error(e.message ?: "Unknown error")
4649
} catch (e: Exception) {
4750
if (_uiState.value !is ArrivalsState.Data) {
48-
_uiState.value = ArrivalsState.Error(e.message ?: "Unknown error")
51+
_uiState.value = ArrivalsState.Error("Unknown error")
4952
}
5053
}
5154
}

shared/src/commonMain/kotlin/com/jdamcd/arrivals/gtfs/GtfsArrivals.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ internal class GtfsArrivals(
2323
@Throws(NoDataException::class, CancellationException::class)
2424
override suspend fun latest(): ArrivalsInfo {
2525
updateStops()
26+
val model: ArrivalsInfo
2627
try {
27-
val model = formatArrivals(api.fetchFeedMessage(settings.gtfsRealtime))
28-
if (model.arrivals.isNotEmpty()) {
29-
return model
30-
} else {
31-
throw NoDataException("No arrivals found")
32-
}
28+
model = formatArrivals(api.fetchFeedMessage(settings.gtfsRealtime))
3329
} catch (e: Exception) {
34-
throw NoDataException("Failed to connect")
30+
throw NoDataException("No connection")
31+
}
32+
if (model.arrivals.isEmpty()) {
33+
throw NoDataException("No arrivals found")
3534
}
35+
return model
3636
}
3737

3838
private suspend fun updateStops() {

shared/src/commonMain/kotlin/com/jdamcd/arrivals/tfl/TflArrivals.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ internal class TflArrivals(
2020

2121
@Throws(NoDataException::class, CancellationException::class)
2222
override suspend fun latest(): ArrivalsInfo {
23+
val model: ArrivalsInfo
2324
try {
24-
val model = formatArrivals(api.fetchArrivals(settings.tflStopId))
25-
if (model.arrivals.isNotEmpty()) {
26-
return model
27-
} else {
28-
throw NoDataException("No arrivals found")
29-
}
25+
model = formatArrivals(api.fetchArrivals(settings.tflStopId))
3026
} catch (e: Exception) {
3127
throw NoDataException("No connection")
3228
}
29+
if (model.arrivals.isEmpty()) {
30+
throw NoDataException("No arrivals found")
31+
}
32+
return model
3333
}
3434

3535
@Throws(CancellationException::class)

0 commit comments

Comments
 (0)