Throw exception instead of custom error

- This step unify our approach to validation on client side across all server calls
This commit is contained in:
Honza 2023-01-31 09:32:34 +01:00
parent 443d51fd02
commit ef248c9dc5
4 changed files with 13 additions and 33 deletions

View File

@ -82,8 +82,9 @@ internal class BlockingLightWalletClientImpl private constructor(
}
override fun submitTransaction(spendTransaction: ByteArray): Response<SendResponseUnsafe> {
if (spendTransaction.isEmpty()) {
return Response.Failure.Client.SubmitEmptyTransaction()
require(spendTransaction.isNotEmpty()) {
"${Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE} Failed to submit transaction because it was empty, so " +
"this request was ignored on the client-side." // NON-NLS
}
return try {
val request =
@ -100,8 +101,9 @@ internal class BlockingLightWalletClientImpl private constructor(
}
override fun fetchTransaction(txId: ByteArray): Response<RawTransactionUnsafe> {
if (txId.isEmpty()) {
return Response.Failure.Client.NullIdTransaction()
require(txId.isNotEmpty()) {
"${Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE} Failed to start fetching the transaction with null " +
"transaction ID, so this request was ignored on the client-side." // NON-NLS
}
return try {
val request = Service.TxFilter.newBuilder().setHash(ByteString.copyFrom(txId)).build()

View File

@ -84,8 +84,9 @@ internal class CoroutineLightWalletClientImpl private constructor(
}
override suspend fun submitTransaction(spendTransaction: ByteArray): Response<SendResponseUnsafe> {
if (spendTransaction.isEmpty()) {
return Response.Failure.Client.SubmitEmptyTransaction()
require(spendTransaction.isNotEmpty()) {
"${Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE} Failed to submit transaction because it was empty, so " +
"this request was ignored on the client-side." // NON-NLS
}
return try {
val request =
@ -102,10 +103,10 @@ internal class CoroutineLightWalletClientImpl private constructor(
}
override suspend fun fetchTransaction(txId: ByteArray): Response<RawTransactionUnsafe> {
if (txId.isEmpty()) {
return Response.Failure.Client.NullIdTransaction()
require(txId.isNotEmpty()) {
"${Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE} Failed to start fetching the transaction with null " +
"transaction ID, so this request was ignored on the client-side." // NON-NLS
}
return try {
val request = Service.TxFilter.newBuilder().setHash(ByteString.copyFrom(txId)).build()

View File

@ -1,13 +1,5 @@
package co.electriccoin.lightwallet.client.model
internal const val SUBMIT_EMPTY_TRANSACTION_ERROR_CODE = 3000
internal const val SUBMIT_EMPTY_TRANSACTION_ERROR_DESCRIPTION = "Failed to submit transaction because it was empty, " +
"so this request was ignored on the client-side." // NON-NLS
internal const val NULL_TRANSACTION_ID_ERROR_CODE = 3001
internal const val NULL_TRANSACTION_ID_ERROR_DESCRIPTION = "Failed to start fetching the transaction with null " +
"transaction ID, so this request was ignored on the client-side." // NON-NLS
internal const val CONNECTION_ERROR_CODE = 3100
internal const val CONNECTION_ERROR_DESCRIPTION = "Missing internet connection." // NON-NLS
@ -103,20 +95,6 @@ sealed class Response<T> {
*/
class Canceled<T>(code: Int, description: String?) : Client<T>(code, description)
/**
* The operation of submitting a transaction failed due to an empty transaction used.
*/
class SubmitEmptyTransaction<T>(
description: String? = SUBMIT_EMPTY_TRANSACTION_ERROR_DESCRIPTION
) : Client<T>(SUBMIT_EMPTY_TRANSACTION_ERROR_CODE, description)
/**
* The operation of fetching a transaction failed due to a null ID used.
*/
class NullIdTransaction<T>(
description: String? = NULL_TRANSACTION_ID_ERROR_DESCRIPTION
) : Client<T>(NULL_TRANSACTION_ID_ERROR_CODE, description)
override fun toString(): String {
return "Client Error(code='$code', description='$description')" // NON-NLS
}

View File

@ -215,8 +215,7 @@ internal class PersistentTransactionManager(
)
else -> {
twig("submitting transaction with memo: ${tx.memo} amount: ${tx.value}", -1)
val response = service.submitTransaction(tx.raw)
when (response) {
when (val response = service.submitTransaction(tx.raw)) {
is Response.Success -> {
twig("SUCCESS: submit transaction completed with response: ${response.result}")
safeUpdate("updating submitted transaction (hadError: false)", -1) {