Author: Adam Evyčędo <git@apiote.xyz>
react to errors from transitous
%!v(PANIC=String method: strings: negative Repeat count)
diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt b/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt index bdb383c7bb383976507ceeed0a8db083413fef7f..4ff3d6d839d708b8e84d21b774678bd6a4e2dc97 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt @@ -11,6 +11,8 @@ import android.net.NetworkCapabilities import android.os.Build import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import org.openapitools.client.infrastructure.ServerError +import org.openapitools.client.infrastructure.ServerException import xyz.apiote.bimba.czwek.R import xyz.apiote.bimba.czwek.settings.feeds.FeedsSettings import java.io.IOException @@ -52,7 +54,11 @@ } data class Result(val stream: InputStream?, val error: Error?) -data class Error(val statusCode: Int, val stringResource: Int, val imageResource: Int) +data class Error(val statusCode: Int, val stringResource: Int, val imageResource: Int) { + companion object { + fun fromTransitous(e: ServerException): Error = Error(e.statusCode, R.string.error, R.drawable.error_other) + } +} suspend fun getBimba(context: Context, server: Server): Result { return try { diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/home/HomeViewModel.kt b/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/home/HomeViewModel.kt index a29ee2ccca5d92c7bff96a401d89999cce5a91d4..361ea8f9903ab8ab086365dd4bcaf666b0020aef 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/home/HomeViewModel.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/home/HomeViewModel.kt @@ -18,6 +18,7 @@ import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch +import org.openapitools.client.infrastructure.ServerException import xyz.apiote.bimba.czwek.repo.Event import xyz.apiote.bimba.czwek.repo.Favourite import xyz.apiote.bimba.czwek.repo.FeedInfo @@ -45,6 +46,9 @@ try { getFeeds(context) mutableQueryables.value = OnlineRepository().queryQueryables(query, context) ?: emptyList() } catch (e: TrafficResponseException) { + // XXX intentionally no error showing in suggestions + Log.e("Suggestion", "$e") + } catch (e: ServerException) { // XXX intentionally no error showing in suggestions Log.e("Suggestion", "$e") } @@ -89,20 +93,26 @@ context, 12, // XXX heuristics favourite.exact ) - stopDepartures?.let { sDs -> - if (sDs.events.isEmpty()) { - Pair(favourite.feedID+favourite.stopCode, Optional.empty()) - } else { - Pair(favourite.feedID+favourite.stopCode, Optional.ofNullable(sDs.events.find { departure -> - favourite.lines.isEmpty() or favourite.lines.contains( - departure.vehicle.Line.name - ) - })) - } - } ?: Pair(favourite.feedID+favourite.stopCode, Optional.empty()) + stopDepartures?.let { sDs -> + if (sDs.events.isEmpty()) { + Pair(favourite.feedID + favourite.stopCode, Optional.empty()) + } else { + Pair( + favourite.feedID + favourite.stopCode, + Optional.ofNullable(sDs.events.find { departure -> + favourite.lines.isEmpty() or favourite.lines.contains( + departure.vehicle.Line.name + ) + }) + ) + } + } ?: Pair(favourite.feedID + favourite.stopCode, Optional.empty()) } catch (e: TrafficResponseException) { Log.w("DeparturesForFavourite", "$e") - Pair(favourite.feedID+favourite.stopCode, Optional.empty()) + Pair(favourite.feedID + favourite.stopCode, Optional.empty()) + } catch (e: ServerException) { + Log.w("DeparturesForFavourite", "Transitous returned ${e.statusCode}, ${e.message}") + Pair(favourite.feedID + favourite.stopCode, Optional.empty()) } } }.awaitAll().associate { it } diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesViewModel.kt b/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesViewModel.kt index 0e812fc1dbeee2644bcad37645add08d5deceddd..613aca9107d7bfebb31aed2d6189f045e0625f1b 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesViewModel.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesViewModel.kt @@ -5,6 +5,7 @@ package xyz.apiote.bimba.czwek.departures import android.app.Activity +import xyz.apiote.bimba.czwek.R import android.content.Context import android.content.Intent import android.net.Uri @@ -14,12 +15,14 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch +import org.openapitools.client.infrastructure.ServerException import xyz.apiote.bimba.czwek.api.Error import xyz.apiote.bimba.czwek.api.mapHttpError import xyz.apiote.bimba.czwek.repo.FeedInfo import xyz.apiote.bimba.czwek.repo.OfflineRepository import xyz.apiote.bimba.czwek.repo.OnlineRepository import xyz.apiote.bimba.czwek.repo.QrLocation +import xyz.apiote.bimba.czwek.repo.Queryable import xyz.apiote.bimba.czwek.repo.StopEvents import xyz.apiote.bimba.czwek.repo.TrafficResponseException import java.time.LocalDate @@ -76,6 +79,11 @@ if (!departures.isInitialized || force) { _error.value = e.error } Log.w("Departures", "$e") + } catch (e: ServerException) { + if (!departures.isInitialized || force) { + _error.value = Error.fromTransitous(e) + } + Log.w("Departures", "Transitous returned ${e.statusCode}, ${e.message}") } } } @@ -147,7 +155,9 @@ } } ?: throw TrafficResponseException(41) } - null -> intent?.extras?.getString(DeparturesActivity.CODE_PARAM) ?: throw TrafficResponseException(41) + null -> intent?.extras?.getString(DeparturesActivity.CODE_PARAM) + ?: throw TrafficResponseException(41) + else -> throw TrafficResponseException(41) } } diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/search/ResultsActivity.kt b/app/src/main/java/xyz/apiote/bimba/czwek/search/ResultsActivity.kt index b8e446eebb0382c72d17ccc9290b5cf967fe8d37..0c87cba970f5cce61d4d0d45aec18eef3015dfa7 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/search/ResultsActivity.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/search/ResultsActivity.kt @@ -32,6 +32,7 @@ import com.google.openlocationcode.OpenLocationCode import kotlinx.coroutines.MainScope import kotlinx.coroutines.Runnable import kotlinx.coroutines.launch +import org.openapitools.client.infrastructure.ServerException import xyz.apiote.bimba.czwek.R import xyz.apiote.bimba.czwek.api.Error import xyz.apiote.bimba.czwek.databinding.ActivityResultsBinding @@ -270,6 +271,9 @@ updateItems(result, null, false) } catch (e: TrafficResponseException) { Log.w("Suggestion", "$e") showError(e.error) + } catch (e: ServerException) { + Log.w("Suggestion", "Transitous returned: ${e.statusCode}, ${e.message}") + showError(Error.fromTransitous(e)) } } } @@ -289,6 +293,9 @@ updateItems(result, position, showArrow) } catch (e: TrafficResponseException) { Log.w("Suggestion", "$e") showError(e.error) + } catch (e: ServerException) { + Log.w("Suggestion", "Transitous returned: ${e.statusCode}, ${e.message}") + showError(Error.fromTransitous(e)) } } }