Skip to content

Commit a6a8dad

Browse files
committed
fix(nav3): Keep names when the property encoder can't link
The encoding probe's broad catch passed LinkageErrors to ExceptionUtils.rethrowIfFatal, which rethrows them. The error then escaped the mapper's constructor, and BuiltInBackStackEntryMappers fell back to a mapper that records no names, instead of only disabling property encoding. The probe now returns a LinkageError before the fatal-error check, so an encoder that can't link keeps names and records encoding_failed, as other encoder failures do.
1 parent 95786cb commit a6a8dad

3 files changed

Lines changed: 105 additions & 16 deletions

File tree

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxCompatibilityProbe.kt‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,38 @@ internal interface KotlinxCompatibilityProbe {
8484
return error
8585
}
8686

87-
override fun encoderIncompatibility(): Throwable? =
87+
override fun encoderIncompatibility(): Throwable? = encoderFailureOf {
88+
val writer = TreeArgumentWriter(ArgumentBudget())
89+
@Suppress("UNCHECKED_CAST")
90+
KotlinxPropertiesEncoder.encode(
91+
ProbeClass.serializer() as KSerializer<Any>,
92+
ProbeClass(),
93+
writer,
94+
)
95+
// An incompatible kotlinx.serialization version can link and run yet mis-encode properties
96+
// under R8 without throwing. Verifying the fixture's output disables such a silently
97+
// corrupting encoder instead of letting wrong arguments reach Sentry.
98+
if (writer.argumentsOrThrow() == EXPECTED_PROBE_ENCODING) {
99+
null
100+
} else {
101+
EncoderProducedUnexpectedOutput()
102+
}
103+
}
104+
105+
/**
106+
* Runs [check], returning the failure it reports or throws, or `null` if it reports none.
107+
* Rethrows fatal errors other than [LinkageError].
108+
*
109+
* A [LinkageError] is returned rather than rethrown so that an encoder that can't link only
110+
* disables property encoding. Rethrown, it would escape the mapper's constructor, and
111+
* `BuiltInBackStackEntryMappers` would fall back to a mapper that records no names either.
112+
*/
113+
internal inline fun encoderFailureOf(check: () -> Throwable?): Throwable? =
88114
try {
89-
val writer = TreeArgumentWriter(ArgumentBudget())
90-
@Suppress("UNCHECKED_CAST")
91-
KotlinxPropertiesEncoder.encode(
92-
ProbeClass.serializer() as KSerializer<Any>,
93-
ProbeClass(),
94-
writer,
95-
)
96-
// An incompatible kotlinx.serialization version can link and run yet mis-encode properties
97-
// under R8 without throwing. Verifying the fixture's output disables such a silently
98-
// corrupting encoder instead of letting wrong arguments reach Sentry.
99-
if (writer.argumentsOrThrow() == EXPECTED_PROBE_ENCODING) {
100-
null
101-
} else {
102-
EncoderProducedUnexpectedOutput()
103-
}
115+
check()
116+
} catch (e: LinkageError) {
117+
// Caught before rethrowIfFatal, which rethrows LinkageErrors.
118+
e
104119
} catch (@Suppress("TooGenericExceptionCaught") t: Throwable) {
105120
// Broad because an incompatible kotlinx.serialization version can fail the encoder in many
106121
// ways, not only with a LinkageError. Any failure disables property encoding but keeps

‎sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxBackStackEntryMapperProbeTest.kt‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,28 @@ class KotlinxBackStackEntryMapperProbeTest {
160160
.inOrder()
161161
}
162162

163+
@Test
164+
fun `keeps the name and records only the encoding failure when the properties encoder can't link`() {
165+
// Runs the LinkageError through the real probe's failure handling instead of returning it.
166+
val probe =
167+
object : KotlinxCompatibilityProbe {
168+
override fun nameLookupLinkageError(onInconclusive: (Throwable?) -> Unit): LinkageError? =
169+
null
170+
171+
override fun encoderIncompatibility(): Throwable? =
172+
KotlinxCompatibilityProbe.encoderFailureOf {
173+
throw NoSuchMethodError()
174+
}
175+
}
176+
val fixture = KotlinxMapperFixture.create(includeProperties = true, probe = probe)
177+
val mapper = fixture.mapper
178+
179+
val mapped = mapper.map(Detail("1"))
180+
181+
assertThat(mapped)
182+
.isEqualTo(SentryBackStackEntry("Detail", droppedBy(ArgumentDropReason.ENCODING_FAILED)))
183+
}
184+
163185
@Test
164186
fun `records no arguments for objects when the properties encoder is incompatible`() {
165187
val probe = FakeCompatibilityProbe(encoderError = NoSuchMethodError())

‎sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxCompatibilityProbeTest.kt‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.sentry.compose.navigation3.internal.backstack.kotlinx
33
import com.google.common.truth.Truth.assertThat
44
import io.sentry.compose.navigation3.internal.backstack.kotlinx.KotlinxCompatibilityProbe.Companion.ProbeClass
55
import io.sentry.compose.navigation3.internal.backstack.kotlinx.KotlinxCompatibilityProbe.Companion.ProbeObject
6+
import kotlin.test.assertFailsWith
67
import org.junit.Test
78

89
/**
@@ -34,6 +35,57 @@ class KotlinxCompatibilityProbeTest {
3435
assertThat(error).isNull()
3536
}
3637

38+
// encoderFailureOf()
39+
40+
@Test
41+
fun `encoderFailureOf() - returns null when the check reports no failure`() {
42+
val failure = KotlinxCompatibilityProbe.encoderFailureOf { null }
43+
44+
assertThat(failure).isNull()
45+
}
46+
47+
@Test
48+
fun `encoderFailureOf() - returns the failure the check reports without throwing`() {
49+
val error = EncoderProducedUnexpectedOutput()
50+
51+
val failure = KotlinxCompatibilityProbe.encoderFailureOf { error }
52+
53+
assertThat(failure).isSameInstanceAs(error)
54+
}
55+
56+
@Test
57+
fun `encoderFailureOf() - returns a LinkageError the check throws instead of rethrowing it`() {
58+
// Rethrown, the error would escape the mapper's constructor and disable name recording too.
59+
val errors = listOf(NoSuchMethodError(), NoClassDefFoundError(), VerifyError())
60+
61+
val failures = errors.map { error ->
62+
KotlinxCompatibilityProbe.encoderFailureOf { throw error }
63+
}
64+
65+
assertThat(failures).containsExactlyElementsIn(errors).inOrder()
66+
}
67+
68+
@Test
69+
fun `encoderFailureOf() - returns a non-fatal exception the check throws`() {
70+
val error = NullPointerException()
71+
72+
val failure = KotlinxCompatibilityProbe.encoderFailureOf { throw error }
73+
74+
assertThat(failure).isSameInstanceAs(error)
75+
}
76+
77+
@Test
78+
fun `encoderFailureOf() - rethrows a fatal error the check throws that isn't a LinkageError`() {
79+
val error = OutOfMemoryError()
80+
81+
val thrown =
82+
assertFailsWith<OutOfMemoryError> {
83+
KotlinxCompatibilityProbe.encoderFailureOf { throw error }
84+
}
85+
86+
assertThat(thrown).isSameInstanceAs(error)
87+
}
88+
3789
@Test
3890
fun `EncoderProducedUnexpectedOutput is a RuntimeException with a message that leaks no route data`() {
3991
val error = EncoderProducedUnexpectedOutput()

0 commit comments

Comments
 (0)