I wrote a simple composable that provides an IconButton.
@Composable
fun MenuIcon(
@DrawableRes iconRes: Int,
contentDescription: String,
onClick: () -> Unit,
) {
if (contentDescription.isEmpty()) {
throw IllegalArgumentException("contentDescription is mandatory and should not be empty.")
}
// I also tried assert(contentDescription.isNotEmpty())
IconButton(onClick = onClick) {
Image(
painter = painterResource(id = iconRes),
contentDescription = contentDescription,
)
}
}
In our project, we want to force our developers to provide a content description for accessibility purpose. So I add a simple check to throw an exception if contentDescription is empty.
The problem is the following: when exception is thrown, in Logcat, I get another exception that did not point on my Composable:
java.lang.IllegalStateException: pending composition has not been applied
[...]
It will not help developers using my composable to find the problem... Is there a solution to display my error message?
Thanks!