From 00a88c78743555af941c26dffa88c840ae99e1c9 Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Mon, 17 Jun 2024 21:29:22 +0200 Subject: [PATCH] refactor: Allow PachliError.formatArgs to be null (#755) Simplifies the case where there are no format args. --- .../app/pachli/core/common/PachliError.kt | 21 ++++++++++++++----- .../core/data/repository/ServerRepository.kt | 6 +++--- .../network/retrofit/apiresult/ApiResult.kt | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/core/common/src/main/kotlin/app/pachli/core/common/PachliError.kt b/core/common/src/main/kotlin/app/pachli/core/common/PachliError.kt index adfa08501..0589127af 100644 --- a/core/common/src/main/kotlin/app/pachli/core/common/PachliError.kt +++ b/core/common/src/main/kotlin/app/pachli/core/common/PachliError.kt @@ -30,7 +30,7 @@ import app.pachli.core.common.string.unicodeWrap * ```kotlin * sealed class Error( * @StringRes override val resourceId: Int, - * override val formatArgs: Array, + * override val formatArgs: Array?, * cause: PachliError? = null, * ) : PachliError { * data object SomeProblem : Error(R.string.error_some_problem) @@ -62,6 +62,12 @@ import app.pachli.core.common.string.unicodeWrap * Value %1$d is out of range * Could not fetch %1$s: %2$s * ``` + * + * @property resourceId A string resource for the error message associated with + * this error. + * @property formatArgs Additional arguments to be interpolated in to the string + * resource when constructing the error message. + * @property cause The cause of this error, if any. */ interface PachliError { /** String resource ID for the error message. */ @@ -69,7 +75,7 @@ interface PachliError { val resourceId: Int /** Arguments to be interpolated in to the string from [resourceId]. */ - val formatArgs: Array + val formatArgs: Array? /** * The cause of this error. If present the string representation of `cause` @@ -78,11 +84,16 @@ interface PachliError { val cause: PachliError? /** - * @return A localised, unicode-wrapped error message for this error. + * Generate a formatted error message for this error. [resourceId] is + * fetched and the values of [formatArgs] are interpolated in to it. + * If [cause] is non-null then its error message is generated (recursively) + * and used as the last format argument. */ fun fmt(context: Context): String { - val args = mutableListOf(*formatArgs) - cause?.let { args.add(it.fmt(context)) } + val args = buildList { + formatArgs?.let { addAll(it) } + cause?.let { add(it.fmt(context)) } + } return context.getString(resourceId, *args.toTypedArray()).unicodeWrap() } } diff --git a/core/data/src/main/kotlin/app/pachli/core/data/repository/ServerRepository.kt b/core/data/src/main/kotlin/app/pachli/core/data/repository/ServerRepository.kt index b14a9464c..77f2575ac 100644 --- a/core/data/src/main/kotlin/app/pachli/core/data/repository/ServerRepository.kt +++ b/core/data/src/main/kotlin/app/pachli/core/data/repository/ServerRepository.kt @@ -120,13 +120,13 @@ class ServerRepository @Inject constructor( sealed class Error( @StringRes override val resourceId: Int, - override val formatArgs: Array = emptyArray(), + override val formatArgs: Array? = emptyArray(), override val cause: PachliError? = null, ) : PachliError { data class GetWellKnownNodeInfo(val throwable: Throwable) : Error( R.string.server_repository_error_get_well_known_node_info, - throwable.localizedMessage?.let { arrayOf(it) }.orEmpty(), + throwable.localizedMessage?.let { arrayOf(it) }, ) data object UnsupportedSchema : Error( @@ -146,7 +146,7 @@ class ServerRepository @Inject constructor( data class GetInstanceInfoV1(val throwable: Throwable) : Error( R.string.server_repository_error_get_instance_info, - throwable.localizedMessage?.let { arrayOf(it) }.orEmpty(), + throwable.localizedMessage?.let { arrayOf(it) }, ) data class Capabilities(val error: Server.Error) : Error( diff --git a/core/network/src/main/kotlin/app/pachli/core/network/retrofit/apiresult/ApiResult.kt b/core/network/src/main/kotlin/app/pachli/core/network/retrofit/apiresult/ApiResult.kt index 50ff6b7b0..a8b0f0b78 100644 --- a/core/network/src/main/kotlin/app/pachli/core/network/retrofit/apiresult/ApiResult.kt +++ b/core/network/src/main/kotlin/app/pachli/core/network/retrofit/apiresult/ApiResult.kt @@ -61,7 +61,7 @@ sealed class ApiError( ) : PachliError { override val formatArgs = ( throwable.getServerErrorMessage() ?: throwable.localizedMessage - )?.let { arrayOf(it) }.orEmpty() + )?.let { arrayOf(it) } override val cause: PachliError? = null companion object {