summaryrefslogtreecommitdiff
path: root/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt
diff options
context:
space:
mode:
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.kt19
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)