refactor: Allow PachliError.formatArgs to be null (#755)
Simplifies the case where there are no format args.
This commit is contained in:
parent
2327ab5221
commit
00a88c7874
|
@ -30,7 +30,7 @@ import app.pachli.core.common.string.unicodeWrap
|
||||||
* ```kotlin
|
* ```kotlin
|
||||||
* sealed class Error(
|
* sealed class Error(
|
||||||
* @StringRes override val resourceId: Int,
|
* @StringRes override val resourceId: Int,
|
||||||
* override val formatArgs: Array<out String>,
|
* override val formatArgs: Array<out String>?,
|
||||||
* cause: PachliError? = null,
|
* cause: PachliError? = null,
|
||||||
* ) : PachliError {
|
* ) : PachliError {
|
||||||
* data object SomeProblem : Error(R.string.error_some_problem)
|
* 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_out_of_range">Value %1$d is out of range</string>
|
||||||
* <string name="error_fetch">Could not fetch %1$s: %2$s</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 {
|
interface PachliError {
|
||||||
/** String resource ID for the error message. */
|
/** String resource ID for the error message. */
|
||||||
|
@ -69,7 +75,7 @@ interface PachliError {
|
||||||
val resourceId: Int
|
val resourceId: Int
|
||||||
|
|
||||||
/** Arguments to be interpolated in to the string from [resourceId]. */
|
/** 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`
|
* The cause of this error. If present the string representation of `cause`
|
||||||
|
@ -78,11 +84,16 @@ interface PachliError {
|
||||||
val cause: 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 {
|
fun fmt(context: Context): String {
|
||||||
val args = mutableListOf(*formatArgs)
|
val args = buildList {
|
||||||
cause?.let { args.add(it.fmt(context)) }
|
formatArgs?.let { addAll(it) }
|
||||||
|
cause?.let { add(it.fmt(context)) }
|
||||||
|
}
|
||||||
return context.getString(resourceId, *args.toTypedArray()).unicodeWrap()
|
return context.getString(resourceId, *args.toTypedArray()).unicodeWrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -120,13 +120,13 @@ class ServerRepository @Inject constructor(
|
||||||
|
|
||||||
sealed class Error(
|
sealed class Error(
|
||||||
@StringRes override val resourceId: Int,
|
@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,
|
override val cause: PachliError? = null,
|
||||||
) : PachliError {
|
) : PachliError {
|
||||||
|
|
||||||
data class GetWellKnownNodeInfo(val throwable: Throwable) : Error(
|
data class GetWellKnownNodeInfo(val throwable: Throwable) : Error(
|
||||||
R.string.server_repository_error_get_well_known_node_info,
|
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(
|
data object UnsupportedSchema : Error(
|
||||||
|
@ -146,7 +146,7 @@ class ServerRepository @Inject constructor(
|
||||||
|
|
||||||
data class GetInstanceInfoV1(val throwable: Throwable) : Error(
|
data class GetInstanceInfoV1(val throwable: Throwable) : Error(
|
||||||
R.string.server_repository_error_get_instance_info,
|
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(
|
data class Capabilities(val error: Server.Error) : Error(
|
||||||
|
|
|
@ -61,7 +61,7 @@ sealed class ApiError(
|
||||||
) : PachliError {
|
) : PachliError {
|
||||||
override val formatArgs = (
|
override val formatArgs = (
|
||||||
throwable.getServerErrorMessage() ?: throwable.localizedMessage
|
throwable.getServerErrorMessage() ?: throwable.localizedMessage
|
||||||
)?.let { arrayOf(it) }.orEmpty()
|
)?.let { arrayOf(it) }
|
||||||
override val cause: PachliError? = null
|
override val cause: PachliError? = null
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
Loading…
Reference in New Issue