diff options
author | Torsten Grote <t@grobox.de> | 2020-07-23 15:41:50 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2020-07-23 15:42:43 -0300 |
commit | 8eb241ccce345a35b05a6335d11306465220f66d (patch) | |
tree | fe78879b2c0cafdb9ba47f38db6a3572a7116594 /merchant-lib/src/main/java/net/taler/merchantlib/Response.kt | |
parent | 08b10a2408f958cae96ae0c674ee450a35109e8a (diff) | |
download | taler-android-8eb241ccce345a35b05a6335d11306465220f66d.tar.gz taler-android-8eb241ccce345a35b05a6335d11306465220f66d.tar.bz2 taler-android-8eb241ccce345a35b05a6335d11306465220f66d.zip |
[pos] refactor configuration fetching and validation
Diffstat (limited to 'merchant-lib/src/main/java/net/taler/merchantlib/Response.kt')
-rw-r--r-- | merchant-lib/src/main/java/net/taler/merchantlib/Response.kt | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt b/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt index eb1ef27..1b49d78 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt @@ -50,14 +50,29 @@ class Response<out T> private constructor( } } + suspend fun handleSuspend( + onFailure: ((String) -> Any)? = null, + onSuccess: (suspend (T) -> Any)? = null + ) { + if (value is Failure) onFailure?.let { it(getFailureString(value)) } + else onSuccess?.let { + @Suppress("UNCHECKED_CAST") + it(value as T) + } + } + private suspend fun getFailureString(failure: Failure): String = when (failure.exception) { is ClientRequestException -> getExceptionString(failure.exception) else -> failure.exception.toString() } private suspend fun getExceptionString(e: ClientRequestException): String { - val error: Error = e.response.receive() - return "Error ${error.code}: ${error.hint}" + return try { + val error: Error = e.response.receive() + "Error ${error.code}: ${error.hint}" + } catch (ex: Exception) { + "Status code: ${e.response.status.value}" + } } private class Failure(val exception: Throwable) |