Skip to content

Commit 18cf6b3

Browse files
javachemeta-codesync[bot]
authored andcommitted
Make SurfaceMountingManager errors more actionable (#57318)
Summary: Pull Request resolved: #57318 Make it clearer when parent view is null vs not a view group and log what class it is if so. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D109539811 fbshipit-source-id: 89808be7b8051047e7f6dbc306ba95576deb166f
1 parent e745c41 commit 18cf6b3

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.kt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,16 @@ internal constructor(
308308
)
309309
return
310310
}
311-
if (parentViewState.view !is ViewGroup) {
311+
312+
val parentView = parentViewState.view
313+
checkNotNull(parentView) { "Unable to find parentView for tag $parentTag" }
314+
if (parentView !is ViewGroup) {
312315
val message =
313-
"Unable to add a view into a view that is not a ViewGroup. ParentTag: $parentTag - Tag: $tag - Index: $index"
316+
"Unable to add a view into a non-ViewGroup ${parentView.javaClass.simpleName} when inserting [$tag] into parent [$parentTag]"
314317
FLog.e(TAG, message)
315318
throw IllegalStateException(message)
316319
}
317-
val parentView = parentViewState.view as ViewGroup
320+
318321
val viewState = getNullableViewState(tag)
319322
if (viewState == null) {
320323
ReactSoftExceptionLogger.logSoftException(
@@ -332,13 +335,13 @@ internal constructor(
332335
logViewHierarchy(parentView, false)
333336
}
334337

335-
val viewParent = view.parent
336-
if (viewParent != null) {
337-
val actualParentId = if (viewParent is ViewGroup) viewParent.id else View.NO_ID
338+
val currParentView = view.parent
339+
if (currParentView != null) {
340+
val actualParentId = if (currParentView is ViewGroup) currParentView.id else View.NO_ID
338341
ReactSoftExceptionLogger.logSoftException(
339342
TAG,
340343
IllegalStateException(
341-
"addViewAt: cannot insert view [$tag] into parent [$parentTag]: View already has a parent: [$actualParentId] Parent: ${viewParent.javaClass.simpleName} View: ${view.javaClass.simpleName}"
344+
"addViewAt: cannot insert view [$tag] into parent [$parentTag]: View already has a parent: [$actualParentId] Parent: ${currParentView.javaClass.simpleName} View: ${view.javaClass.simpleName}"
342345
),
343346
)
344347

@@ -356,8 +359,8 @@ internal constructor(
356359
// should be impossible - we mark this as a "readded" View and
357360
// thus prevent the RemoveDeleteTree worker from deleting this
358361
// View in the future.
359-
if (viewParent is ViewGroup) {
360-
viewParent.removeView(view)
362+
if (currParentView is ViewGroup) {
363+
currParentView.removeView(view)
361364
}
362365
erroneouslyReaddedReactTags.add(tag)
363366
}
@@ -394,6 +397,7 @@ internal constructor(
394397

395398
@UiThread
396399
public fun removeViewAt(tag: Int, parentTag: Int, index: Int): Unit {
400+
UiThreadUtil.assertOnUiThread()
397401
if (isStopped) {
398402
return
399403
}
@@ -409,22 +413,22 @@ internal constructor(
409413
return
410414
}
411415

412-
UiThreadUtil.assertOnUiThread()
413416
val parentViewState = getNullableViewState(parentTag)
414-
415-
// TODO: throw exception here?
416417
if (parentViewState == null) {
417418
ReactSoftExceptionLogger.logSoftException(
418419
ReactSoftExceptionLogger.Categories.SURFACE_MOUNTING_MANAGER_MISSING_VIEWSTATE,
419-
IllegalStateException("Unable to find viewState for tag: [$parentTag] for removeViewAt"),
420+
ReactNoCrashSoftException(
421+
"Unable to find viewState for tag: [$parentTag] for removeViewAt"
422+
),
420423
)
421424
return
422425
}
423426

424427
val parentView = parentViewState.view
428+
checkNotNull(parentView) { "Unable to find parentView for tag $parentTag" }
425429
if (parentView !is ViewGroup) {
426430
val message =
427-
"Unable to remove a view from a view that is not a ViewGroup. ParentTag: $parentTag - Tag: $tag - Index: $index"
431+
"Unable to remove a view from a a non-ViewGroup ${parentView.javaClass.simpleName} when removing [$tag] from parent [$parentTag]"
428432
FLog.e(TAG, message)
429433
throw IllegalStateException(message)
430434
}
@@ -478,7 +482,7 @@ internal constructor(
478482
logViewHierarchy(parentView, true)
479483
ReactSoftExceptionLogger.logSoftException(
480484
TAG,
481-
IllegalStateException(
485+
ReactNoCrashSoftException(
482486
"Tried to remove view [$tag] of parent [$parentTag] at index $index, but got view tag $actualTag - actual index of view: $tagActualIndex"
483487
),
484488
)

0 commit comments

Comments
 (0)