Skip to content

[@mantine/core] parseThemeColor: Throw descriptive error for invalid theme shades - #9556

Open
rishabhx29 wants to merge 1 commit into
mantinedev:masterfrom
rishabhx29:fix/parse-theme-color-invalid-shade
Open

rishabhx29 wants to merge 1 commit into
mantinedev:masterfrom
rishabhx29:fix/parse-theme-color-invalid-shade

Conversation

@rishabhx29

Copy link
Copy Markdown
Contributor

Problem

parseThemeColor accepts any suffix after a theme color name. For red.10 (or red.foo), theme.colors.red[10] is undefined, which is then passed to isLightColor. That throws an opaque internal error:

TypeError: Cannot read properties of undefined (reading 'startsWith')

The caller has no indication that the color string is invalid, and the stack points at luminance.ts rather than at the bad input.

Reproduction

parseThemeColor({ color: 'red.10', theme: DEFAULT_THEME });
// unfixed master: TypeError: Cannot read properties of undefined (reading 'startsWith')

Solution

After resolving the theme color value, throw a descriptive error when no value exists:

[@mantine/core] Failed to parse color. "red.10" is not a valid theme color shade. Expected a shade from 0 to 9.

Valid shades and non-theme colors are unaffected.

Tests

Added coverage for red.10 and red.foo. All 6 parse-theme-color tests pass with the fix; the new test fails on unfixed master with the internal startsWith TypeError.

This branch has not been deployed

No deployments
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