|
| 1 | +package com.example.ui |
| 2 | + |
| 3 | +import androidx.compose.foundation.BorderStroke |
| 4 | +import androidx.compose.foundation.layout.* |
| 5 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 6 | +import androidx.compose.material3.* |
| 7 | +import androidx.compose.runtime.* |
| 8 | +import androidx.compose.ui.Alignment |
| 9 | +import androidx.compose.ui.Modifier |
| 10 | +import androidx.compose.ui.graphics.Color |
| 11 | +import androidx.compose.ui.text.font.FontWeight |
| 12 | +import androidx.compose.ui.text.style.TextAlign |
| 13 | +import androidx.compose.ui.unit.dp |
| 14 | +import androidx.compose.ui.unit.sp |
| 15 | +import com.example.core.AppLogger |
| 16 | +import com.example.ui.theme.* |
| 17 | + |
| 18 | +/** |
| 19 | + * Enterprise UI Error Boundary. |
| 20 | + * Catches rendering / state evaluation faults in subtree composables to prevent app fatal aborts, |
| 21 | + * displaying a graceful fallback recovery card. |
| 22 | + */ |
| 23 | +@Composable |
| 24 | +fun ErrorBoundary( |
| 25 | + componentName: String = "Component", |
| 26 | + modifier: Modifier = Modifier, |
| 27 | + hasError: Boolean = false, |
| 28 | + errorMessage: String? = null, |
| 29 | + onRetry: () -> Unit = {}, |
| 30 | + fallback: (@Composable (String, () -> Unit) -> Unit)? = null, |
| 31 | + content: @Composable () -> Unit |
| 32 | +) { |
| 33 | + if (hasError) { |
| 34 | + val message = errorMessage ?: "$componentName encountered an unexpected error" |
| 35 | + AppLogger.e("ErrorBoundary", "UI Fallback rendered for $componentName: $message") |
| 36 | + |
| 37 | + if (fallback != null) { |
| 38 | + fallback(message, onRetry) |
| 39 | + } else { |
| 40 | + Surface( |
| 41 | + modifier = modifier |
| 42 | + .fillMaxWidth() |
| 43 | + .padding(16.dp), |
| 44 | + shape = RoundedCornerShape(16.dp), |
| 45 | + color = CosmicSlateCard, |
| 46 | + border = BorderStroke(1.dp, ErrorRed.copy(alpha = 0.5f)) |
| 47 | + ) { |
| 48 | + Column( |
| 49 | + modifier = Modifier.padding(16.dp), |
| 50 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 51 | + verticalArrangement = Arrangement.spacedBy(8.dp) |
| 52 | + ) { |
| 53 | + Text("⚠️", fontSize = 24.sp) |
| 54 | + Text( |
| 55 | + text = "$componentName encountered an issue", |
| 56 | + color = TextPrimary, |
| 57 | + fontSize = 14.sp, |
| 58 | + fontWeight = FontWeight.Bold |
| 59 | + ) |
| 60 | + Text( |
| 61 | + text = message, |
| 62 | + color = TextSecondary, |
| 63 | + fontSize = 11.sp, |
| 64 | + textAlign = TextAlign.Center |
| 65 | + ) |
| 66 | + Spacer(modifier = Modifier.height(4.dp)) |
| 67 | + Button( |
| 68 | + onClick = onRetry, |
| 69 | + colors = ButtonDefaults.buttonColors(containerColor = PrimaryCosmic), |
| 70 | + shape = RoundedCornerShape(8.dp) |
| 71 | + ) { |
| 72 | + Text("Retry", color = Color.White, fontSize = 12.sp, fontWeight = FontWeight.Bold) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } else { |
| 78 | + content() |
| 79 | + } |
| 80 | +} |
0 commit comments