Skip to content

fix(AlertBanner): never crash on unknown variant or type — safe fallback - #329

Open
MihirSachdeva wants to merge 1 commit into
masterfrom
fix/alert-banner-safe-variant-fallback
Open

fix(AlertBanner): never crash on unknown variant or type — safe fallback#329
MihirSachdeva wants to merge 1 commit into
masterfrom
fix/alert-banner-safe-variant-fallback

Conversation

@MihirSachdeva

Copy link
Copy Markdown
Member

Why

A consumer in newton-web#8253 passed variant=\"information\" to <NSAlertBanner>. That string is not in the AlertBannerVariant union (`'primary' | 'success' | 'warning' | 'error' | 'default'`), so the runtime lookup ALERT_BANNER_COLOR_MAPPINGS[type][variant] returned undefined. The component then destructured { iconColor, textColor, backgroundColor, borderColor } from undefined, throwing a TypeError that crashed the entire SemesterFeePayment subtree behind the FE's ErrorBoundary.

`` reuses `getAlertBannerColors` and is affected by the same path.

This wasn't caught by TypeScript because the consumer was in a .js file. We want the design system to fail gracefully when this happens — bad colors are much better than a white screen.

What

  • Add resolveVariant / resolveType helpers that validate against the enum and fall back to primary / default when the input is not a recognized value.
  • Threading those resolvers through getAlertIconName, getAlertBannerColors, and getButtonColorFromAlertBannerTypeVariant so any downstream lookup is now always defined.
  • Dev-only `console.warn` when an unknown value is passed, so the misuse remains visible during development (silenced in production).
  • Two regression tests in `AlertBanner.test.tsx` covering unknown variant and unknown type.

All 19 existing Alert + AlertBanner tests still pass.

Test plan

```bash
npx jest ui/elements/AlertBanner ui/elements/Alert
```

Output:
```
Test Suites: 2 passed, 2 total
Tests: 19 passed, 19 total
```

The two new specs:

  • does not crash on an unknown variant — falls back to primary
  • does not crash on an unknown type — falls back to default

Risk

Low. Behaviour for valid variant/type is unchanged (same lookup, just with an early-return for known values). Only the previously-crashing path now returns a primary/default-styled banner with a console warning.

🤖 Generated with Claude Code

When a consumer passes a variant outside the AlertBannerVariant union
(e.g. typo "information"), the previous lookup
ALERT_BANNER_COLOR_MAPPINGS[type][variant] returned undefined. The
component then destructured colors from undefined, throwing a TypeError
that crashed the whole subtree behind a React error boundary. The same
path inside <Alert /> was affected (it reuses getAlertBannerColors).

Wrap variant + type with resolveVariant/resolveType helpers that
validate against the enum and fall back to primary/default when
unrecognized, emitting a dev-only console.warn so the misuse is still
visible during development. The component now renders instead of taking
the page down.

Two regression specs added in AlertBanner.test.tsx covering unknown
variant and unknown type. All Alert + AlertBanner tests pass (19/19).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant