Skip to content

Commit 42169fb

Browse files
committed
fix(onramp/deeplinks): show "No Internet" alert instead of generic error for network failures
Preserve the original throwable as `cause` in FailedToCreateTransaction and FailedToSimulateTransaction so the handler can detect network errors (UnknownHostException, ConnectException, TimeoutException) and show the standard "No Internet Connection" alert instead of a confusing "Something Went Wrong" error. Network-caused failures are also excluded from Bugsnag via the existing isAlert guard. Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent b6abe45 commit 42169fb

4 files changed

Lines changed: 46 additions & 20 deletions

File tree

apps/flipcash/shared/onramp/deeplinks/src/main/kotlin/com/flipcash/app/onramp/DeeplinkOnRampErrors.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ sealed class DeeplinkOnRampError(
99
class FailedToGenerateDeeplink(
1010
override val message: String? = null
1111
) : DeeplinkOnRampError(message = message)
12-
class FailedToCreateTransaction(override val message: String?) :
13-
DeeplinkOnRampError(message = message)
12+
class FailedToCreateTransaction(
13+
override val message: String?,
14+
override val cause: Throwable? = null
15+
) : DeeplinkOnRampError(message = message, cause = cause)
1416

15-
class FailedToSimulateTransaction(override val message: String?) :
16-
DeeplinkOnRampError(message = message)
17+
class FailedToSimulateTransaction(
18+
override val message: String?,
19+
override val cause: Throwable? = null
20+
) : DeeplinkOnRampError(message = message, cause = cause)
1721

1822
class FailedToSendTransaction(
1923
override val code: Long = -99,

apps/flipcash/shared/onramp/deeplinks/src/main/kotlin/com/flipcash/app/onramp/ExternalWalletOnRampController.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class ExternalWalletOnRampController @Inject constructor(
157157
val fee = _feeAmount.value ?: LocalFiat.Zero
158158

159159
createUsdcToUsdfSwapTransaction(state, amount)
160-
.onFailure { fail(DeeplinkOnRampError.FailedToCreateTransaction(message = it.message), state) }
160+
.onFailure { fail(DeeplinkOnRampError.FailedToCreateTransaction(message = it.message, cause = it), state) }
161161
.fold(
162162
onSuccess = { (transaction, swapId) ->
163163
withContext(Dispatchers.IO) {
@@ -170,7 +170,7 @@ class ExternalWalletOnRampController @Inject constructor(
170170
},
171171
onFailure = { Result.failure(it) }
172172
)
173-
.onFailure { fail(DeeplinkOnRampError.FailedToSimulateTransaction(message = it.message), state) }
173+
.onFailure { fail(DeeplinkOnRampError.FailedToSimulateTransaction(message = it.message, cause = it), state) }
174174
.onSuccess { (transaction, swapId) ->
175175
transitionTo(
176176
ExternalWalletOnRampState.Signing(
@@ -195,7 +195,7 @@ class ExternalWalletOnRampController @Inject constructor(
195195
val fee = _feeAmount.value ?: LocalFiat.Zero
196196

197197
createDepositTransaction(state, amount)
198-
.onFailure { fail(DeeplinkOnRampError.FailedToCreateTransaction(message = it.message), state) }
198+
.onFailure { fail(DeeplinkOnRampError.FailedToCreateTransaction(message = it.message, cause = it), state) }
199199
.fold(
200200
onSuccess = { transaction ->
201201
withContext(Dispatchers.IO) {
@@ -208,7 +208,7 @@ class ExternalWalletOnRampController @Inject constructor(
208208
},
209209
onFailure = { Result.failure(it) }
210210
)
211-
.onFailure { fail(DeeplinkOnRampError.FailedToSimulateTransaction(message = it.message), state) }
211+
.onFailure { fail(DeeplinkOnRampError.FailedToSimulateTransaction(message = it.message, cause = it), state) }
212212
.onSuccess { transaction ->
213213
transitionTo(
214214
ExternalWalletOnRampState.Signing(

apps/flipcash/shared/onramp/deeplinks/src/main/kotlin/com/flipcash/app/onramp/ExternalWalletOnRampHandler.kt

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.getcode.manager.BottomBarManager
2525
import com.getcode.navigation.core.CodeNavigator
2626
import com.getcode.util.permissions.rememberNotificationPermission
2727
import com.getcode.utils.TraceType
28+
import com.getcode.utils.isNetworkError
2829
import com.getcode.utils.trace
2930
import kotlinx.coroutines.delay
3031
import kotlinx.coroutines.launch
@@ -296,18 +297,28 @@ fun ExternalWalletOnRampHandler(
296297
controller.reset()
297298
}
298299

299-
if (error.isAlert) {
300-
BottomBarManager.showAlert(
301-
title = title,
302-
message = message,
303-
onDismiss = { onDismiss() },
304-
)
305-
} else {
306-
BottomBarManager.showError(
307-
title = title,
308-
message = message,
309-
onDismiss = { onDismiss() },
310-
)
300+
when {
301+
error.isNetworkCause -> {
302+
BottomBarManager.showAlert(
303+
title = resources.getString(R.string.error_title_noInternet),
304+
message = resources.getString(R.string.error_description_noInternet),
305+
onDismiss = { onDismiss() },
306+
)
307+
}
308+
error.isAlert -> {
309+
BottomBarManager.showAlert(
310+
title = title,
311+
message = message,
312+
onDismiss = { onDismiss() },
313+
)
314+
}
315+
else -> {
316+
BottomBarManager.showError(
317+
title = title,
318+
message = message,
319+
onDismiss = { onDismiss() },
320+
)
321+
}
311322
}
312323
}
313324
}
@@ -325,6 +336,12 @@ private val DeeplinkOnRampError.isAlert: Boolean
325336
DeeplinkError.Disconnected,
326337
DeeplinkError.TransactionRejected,
327338
) || this is DeeplinkOnRampError.FailedToSendTransaction
339+
|| (this is DeeplinkOnRampError.FailedToSimulateTransaction && cause?.isNetworkError() == true)
340+
|| (this is DeeplinkOnRampError.FailedToCreateTransaction && cause?.isNetworkError() == true)
341+
342+
private val DeeplinkOnRampError.isNetworkCause: Boolean
343+
get() = (this is DeeplinkOnRampError.FailedToSimulateTransaction || this is DeeplinkOnRampError.FailedToCreateTransaction)
344+
&& cause?.isNetworkError() == true
328345

329346
private typealias Title = String
330347
private typealias Message = String

libs/logging/src/main/kotlin/com/getcode/utils/ErrorUtils.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ object ErrorUtils {
112112
throwable is SQLException || throwable is SuppressibleException || throwable is TimeoutCancellationException
113113
}
114114

115+
fun Throwable.isNetworkError(): Boolean =
116+
this is UnknownHostException || cause is UnknownHostException ||
117+
this is ConnectException || cause is ConnectException ||
118+
this is TimeoutException || cause is TimeoutException
119+
115120
data class SuppressibleException(override val message: String, override val cause: Throwable? = null) : Throwable(message, cause) {
116121
constructor(cause: Throwable) : this(cause.message.orEmpty(), cause)
117122
}

0 commit comments

Comments
 (0)