refactor: Allow PachliError.formatArgs to be null (#755)

Simplifies the case where there are no format args.
This commit is contained in:
Nik Clayton 2024-06-17 21:29:22 +02:00 committed by GitHub
parent 2327ab5221
commit 00a88c7874
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 9 deletions

View File

@ -30,7 +30,7 @@ import app.pachli.core.common.string.unicodeWrap
* ```kotlin
* sealed class Error(
* @StringRes override val resourceId: Int,
* override val formatArgs: Array<out String>,
* override val formatArgs: Array<out String>?,
* cause: PachliError? = null,
* ) : PachliError {
* data object SomeProblem : Error(R.string.error_some_problem)
@ -62,6 +62,12 @@ import app.pachli.core.common.string.unicodeWrap
* <string name="error_out_of_range">Value %1$d is out of range</string>
* <string name="error_fetch">Could not fetch %1$s: %2$s</string>
* ```
*
* @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<out String>
val formatArgs: Array<out String>?
/**
* 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()
}
}

View File

@ -120,13 +120,13 @@ class ServerRepository @Inject constructor(
sealed class Error(
@StringRes override val resourceId: Int,
override val formatArgs: Array<out String> = emptyArray<String>(),
override val formatArgs: Array<out String>? = emptyArray<String>(),
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(

View File

@ -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 {