Skip to content

Commit 899fd09

Browse files
committed
fix: address error type feedback
1 parent a77fe99 commit 899fd09

3 files changed

Lines changed: 51 additions & 9 deletions

File tree

app/src/main/java/to/bitkit/ext/PaymentFailureReasonExt.kt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ fun Throwable.toSendFailureMessage(context: Context): String {
5757
}
5858

5959
fun Throwable.toCompactFailureType(): String {
60+
if (this is LdkError) return compactType ?: UNKNOWN_FAILURE_TYPE
61+
6062
val rawValue = message?.trim()?.takeIf { it.isNotEmpty() }
6163
?: this::class.simpleName
6264
?: UNKNOWN_FAILURE_TYPE
@@ -88,12 +90,26 @@ private fun String.snakeToLowerCamel(): String {
8890
}
8991

9092
private fun String.compactFailureType(): String {
91-
val unwrappedOptional = removeSurrounding("Optional(", ")")
92-
val unwrappedNodeError = unwrappedOptional.removeSurrounding("NodeError(", ")")
93-
return unwrappedNodeError
93+
val strippedPrefixes = this
94+
.removePrefix("LDK Node error:")
95+
.removePrefix("LDK Build error:")
96+
.trim()
97+
val type = strippedPrefixes
9498
.substringBefore("(")
95-
.substringAfterLast(".")
9699
.trim()
100+
.trimEnd('.')
101+
.trim()
102+
103+
return type.toCompactTypeName()
104+
}
105+
106+
private fun String.toCompactTypeName(): String {
107+
if (isBlank()) return UNKNOWN_FAILURE_TYPE
108+
if (none { it.isWhitespace() || it == '-' || it == '_' }) return this
109+
110+
return split(Regex("[\\s_-]+"))
111+
.filter { it.isNotBlank() }
112+
.joinToString("") { it.replaceFirstChar(Char::titlecase) }
97113
.ifBlank { UNKNOWN_FAILURE_TYPE }
98114
}
99115

@@ -102,8 +118,6 @@ private fun String.looksInternalPaymentError(): Boolean {
102118
}
103119

104120
private val INTERNAL_PAYMENT_ERROR_MARKERS = listOf(
105-
"Optional(",
106-
"NodeError",
107121
"DuplicatePayment",
108122
"PaymentFailureReason",
109123
"ldknode",

app/src/main/java/to/bitkit/utils/Errors.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ class LdkError(private val inner: LdkException) : AppError("Unknown LDK error.")
3333
constructor(inner: NodeException) : this(LdkException.Node(inner))
3434

3535
override val message get() = inner.message ?: super.message
36+
val compactType get() = inner.compactType
3637

3738
sealed interface LdkException {
3839
val message: String?
40+
val compactType: String?
3941

4042
class Build(exception: BuildException) : LdkException {
43+
override val compactType = exception::class.simpleName
4144
override val message = when (exception) {
4245
is BuildException.InvalidChannelMonitor -> "Invalid channel monitor."
4346
is BuildException.InvalidSystemTime -> "Invalid system time."
@@ -57,6 +60,7 @@ class LdkError(private val inner: LdkException) : AppError("Unknown LDK error.")
5760
}
5861

5962
class Node(exception: NodeException) : LdkException {
63+
override val compactType = exception::class.simpleName
6064
override val message = when (exception) {
6165
is NodeException.AlreadyRunning -> "The node is already running."
6266
is NodeException.NotRunning -> "The node is not running."

app/src/test/java/to/bitkit/ext/PaymentFailureReasonExtTest.kt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package to.bitkit.ext
22

33
import android.content.Context
44
import org.junit.Test
5+
import org.lightningdevkit.ldknode.NodeException
56
import org.lightningdevkit.ldknode.PaymentFailureReason
67
import org.mockito.kotlin.mock
78
import org.mockito.kotlin.whenever
89
import to.bitkit.R
910
import to.bitkit.models.SendFailureDetails
11+
import to.bitkit.utils.LdkError
1012
import kotlin.test.assertEquals
1113
import kotlin.test.assertFalse
14+
import kotlin.test.assertNotEquals
1215
import kotlin.test.assertTrue
1316

1417
class PaymentFailureReasonExtTest {
@@ -38,13 +41,34 @@ class PaymentFailureReasonExtTest {
3841
whenever(context.getString(R.string.wallet__payment_failed_description)).thenReturn(message)
3942

4043
assertEquals(message, Exception(" ").toSendFailureMessage(context))
41-
assertEquals(message, Exception("Optional(NodeError(DuplicatePayment))").toSendFailureMessage(context))
44+
assertEquals(message, LdkError(NodeException.DuplicatePayment("Duplicate payment.")).toSendFailureMessage(context))
4245
}
4346

4447
@Test
45-
fun `compact failure types omit optional and node error wrappers`() {
48+
fun `payment failure reason compact failure types use lower camel case`() {
4649
assertEquals("routeNotFound", PaymentFailureReason.ROUTE_NOT_FOUND.toCompactFailureType())
47-
assertEquals("DuplicatePayment", Exception("Optional(NodeError(DuplicatePayment))").toCompactFailureType())
50+
}
51+
52+
@Test
53+
fun `compact failure types use android ldk error classes`() {
54+
assertEquals(
55+
"DuplicatePayment",
56+
LdkError(NodeException.DuplicatePayment("Duplicate payment.")).toCompactFailureType(),
57+
)
58+
assertEquals(
59+
"InvalidCustomTlvs",
60+
LdkError(NodeException.InvalidCustomTlvs("Invalid custom TLVs")).toCompactFailureType(),
61+
)
62+
}
63+
64+
@Test
65+
fun `compact failure types fall back to the exception class name`() {
66+
assertEquals("IllegalStateException", IllegalStateException().toCompactFailureType())
67+
}
68+
69+
@Test
70+
fun `compact failure types ignore sentence punctuation`() {
71+
assertNotEquals("Unknown", Exception("Payment sending failed.").toCompactFailureType())
4872
}
4973

5074
@Test

0 commit comments

Comments
 (0)