From e9536e99ed3ab37c172b484b4b04b55bdd7c5f10 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Tue, 30 Apr 2024 23:44:13 +0200 Subject: [PATCH] Improve http errors wording --- .../credentials/AccountCredentialsScreen.kt | 4 +- .../app/compose/timelime/ErrorListDialog.kt | 4 +- .../readrops/app/compose/util/ErrorMessage.kt | 43 +++++++++++++++++++ .../compose/util/components/ErrorDialog.kt | 18 +------- appcompose/src/main/res/values-fr/strings.xml | 9 +++- appcompose/src/main/res/values/strings.xml | 9 +++- 6 files changed, 65 insertions(+), 22 deletions(-) create mode 100644 appcompose/src/main/java/com/readrops/app/compose/util/ErrorMessage.kt diff --git a/appcompose/src/main/java/com/readrops/app/compose/account/credentials/AccountCredentialsScreen.kt b/appcompose/src/main/java/com/readrops/app/compose/account/credentials/AccountCredentialsScreen.kt index 9624c29b..cd569ff2 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/account/credentials/AccountCredentialsScreen.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/account/credentials/AccountCredentialsScreen.kt @@ -37,7 +37,7 @@ import cafe.adriel.voyager.navigator.currentOrThrow import com.readrops.app.compose.R import com.readrops.app.compose.home.HomeScreen import com.readrops.app.compose.util.components.AndroidScreen -import com.readrops.app.compose.util.components.errorText +import com.readrops.app.compose.util.ErrorMessage import com.readrops.app.compose.util.theme.ShortSpacer import com.readrops.app.compose.util.theme.VeryLargeSpacer import com.readrops.app.compose.util.theme.spacing @@ -178,7 +178,7 @@ class AccountCredentialsScreen( ShortSpacer() Text( - text = errorText(exception = state.loginException!!), + text = ErrorMessage.get(exception = state.loginException!!), style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.error ) diff --git a/appcompose/src/main/java/com/readrops/app/compose/timelime/ErrorListDialog.kt b/appcompose/src/main/java/com/readrops/app/compose/timelime/ErrorListDialog.kt index 3fdd5bde..d3763452 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/timelime/ErrorListDialog.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/timelime/ErrorListDialog.kt @@ -13,8 +13,8 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import com.readrops.app.compose.R import com.readrops.app.compose.repositories.ErrorResult +import com.readrops.app.compose.util.ErrorMessage import com.readrops.app.compose.util.components.BaseDialog -import com.readrops.app.compose.util.components.errorText import com.readrops.app.compose.util.theme.MediumSpacer import com.readrops.app.compose.util.theme.ShortSpacer @@ -44,7 +44,7 @@ fun ErrorListDialog( modifier = Modifier.verticalScroll(scrollableState) ) { for (error in errorResult.entries) { - Text(text = "${error.key.name}: ${errorText(error.value)}") + Text(text = "${error.key.name}: ${ErrorMessage.get(error.value)}") ShortSpacer() } diff --git a/appcompose/src/main/java/com/readrops/app/compose/util/ErrorMessage.kt b/appcompose/src/main/java/com/readrops/app/compose/util/ErrorMessage.kt new file mode 100644 index 00000000..f7ce9503 --- /dev/null +++ b/appcompose/src/main/java/com/readrops/app/compose/util/ErrorMessage.kt @@ -0,0 +1,43 @@ +package com.readrops.app.compose.util + +import androidx.compose.runtime.Composable +import androidx.compose.ui.res.stringResource +import com.readrops.api.utils.exceptions.HttpException +import com.readrops.api.utils.exceptions.ParseException +import com.readrops.api.utils.exceptions.UnknownFormatException +import com.readrops.app.compose.R +import java.io.IOException +import java.net.UnknownHostException + +object ErrorMessage { + + @Composable + fun get(exception: Exception) = when (exception) { + is HttpException -> getHttpMessage(exception) + is UnknownHostException -> stringResource(R.string.unreachable_url) + is NoSuchFileException -> stringResource(R.string.unable_open_file) + is IOException -> stringResource(R.string.network_failure, exception.message.orEmpty()) + is ParseException, is UnknownFormatException -> stringResource(R.string.processing_feed_error) + else -> "${exception.javaClass.simpleName}: ${exception.message}" + } + + @Composable + private fun getHttpMessage(exception: HttpException): String { + return when (exception.code) { + in 400..499 -> { + when (exception.code) { + 400 -> stringResource(id = R.string.http_error_400) + 401 -> stringResource(id = R.string.http_error_401) + 403 -> stringResource(id = R.string.http_error_403) + 404 -> stringResource(id = R.string.http_error_404) + else -> stringResource(id = R.string.http_error_4XX, exception.code) + } + } + + in 500..599 -> { + stringResource(id = R.string.http_error_5XX, exception.code) + } + else -> stringResource(id = R.string.http_error, exception.code) + } + } +} \ No newline at end of file diff --git a/appcompose/src/main/java/com/readrops/app/compose/util/components/ErrorDialog.kt b/appcompose/src/main/java/com/readrops/app/compose/util/components/ErrorDialog.kt index a10e010f..53737e75 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/util/components/ErrorDialog.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/util/components/ErrorDialog.kt @@ -4,12 +4,8 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource -import com.readrops.api.utils.exceptions.HttpException -import com.readrops.api.utils.exceptions.ParseException -import com.readrops.api.utils.exceptions.UnknownFormatException import com.readrops.app.compose.R -import java.io.IOException -import java.net.UnknownHostException +import com.readrops.app.compose.util.ErrorMessage @Composable fun ErrorDialog( @@ -21,17 +17,7 @@ fun ErrorDialog( icon = painterResource(id = R.drawable.ic_error), onDismiss = onDismiss ) { - Text(text = errorText(exception = exception)) + Text(text = ErrorMessage.get(exception = exception)) } } -// TODO check compatibility with other accounts errors -@Composable -fun errorText(exception: Exception) = when (exception) { - is HttpException -> stringResource(id = R.string.unreachable_feed_http_error, exception.code.toString()) - is UnknownHostException -> stringResource(R.string.unreachable_feed) - is NoSuchFileException -> stringResource(R.string.unable_open_file) - is IOException -> stringResource(R.string.network_failure, exception.message.orEmpty()) - is ParseException, is UnknownFormatException -> stringResource(R.string.processing_feed_error) - else -> "${exception.javaClass.simpleName}: ${exception.message}" -} \ No newline at end of file diff --git a/appcompose/src/main/res/values-fr/strings.xml b/appcompose/src/main/res/values-fr/strings.xml index 7905f03d..c5e6eea8 100644 --- a/appcompose/src/main/res/values-fr/strings.xml +++ b/appcompose/src/main/res/values-fr/strings.xml @@ -155,6 +155,13 @@ Flux non attaignable, erreur HTTP %1$s Erreur réseau: %1$s Erreur de traitement du flux - Flux non attaignable + URL non attaignable Impossible d\'ouvrir le fichier + Erreur HTTP 400, veuillez vérifier l\'URL du serveur + Erreur HTTP 401, veuillez vérifier vos identifiants + Erreur HTTP 403, accès interdit + Erreur HTTP 404, l\'URL n\'existe pas + Erreur HTTP %1$s, veuillez vérifier vos champs + Erreur HTTP %1$s, erreur serveur + Erreur HTTP %1$s \ No newline at end of file diff --git a/appcompose/src/main/res/values/strings.xml b/appcompose/src/main/res/values/strings.xml index e58d7604..03a6eff1 100644 --- a/appcompose/src/main/res/values/strings.xml +++ b/appcompose/src/main/res/values/strings.xml @@ -161,6 +161,13 @@ Unreachable feed, HTTP error %1$s Network failure: %1$s Processing feed error - Unreachable feed + Unreachable URL Unable to open file + HTTP error 400, please check your server URL + HTTP error 401, please check your credentials + HTTP error 403, access forbidden + HTTP error 404, URL not found + HTTP error %1$s, please check your fields + HTTP error %1$s, server error + HTTP error %1$s \ No newline at end of file