fix(AlertBanner): never crash on unknown variant or type — safe fallback - #329
Open
MihirSachdeva wants to merge 1 commit into
Open
fix(AlertBanner): never crash on unknown variant or type — safe fallback#329MihirSachdeva wants to merge 1 commit into
MihirSachdeva wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
A consumer in newton-web#8253 passed
variant=\"information\"to<NSAlertBanner>. That string is not in theAlertBannerVariantunion (`'primary' | 'success' | 'warning' | 'error' | 'default'`), so the runtime lookupALERT_BANNER_COLOR_MAPPINGS[type][variant]returnedundefined. The component then destructured{ iconColor, textColor, backgroundColor, borderColor }fromundefined, throwing aTypeErrorthat crashed the entire SemesterFeePayment subtree behind the FE'sErrorBoundary.`` reuses `getAlertBannerColors` and is affected by the same path.
This wasn't caught by TypeScript because the consumer was in a
.jsfile. We want the design system to fail gracefully when this happens — bad colors are much better than a white screen.What
resolveVariant/resolveTypehelpers that validate against the enum and fall back toprimary/defaultwhen the input is not a recognized value.getAlertIconName,getAlertBannerColors, andgetButtonColorFromAlertBannerTypeVariantso any downstream lookup is now always defined.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 primarydoes not crash on an unknown type — falls back to defaultRisk
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