Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgsTable: Gracefully handle conditional args failures #18248

Merged
merged 1 commit into from
May 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/components/src/blocks/ArgsTable/ArgsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import pickBy from 'lodash/pickBy';
import { styled, ignoreSsrWarning } from '@storybook/theming';
import { opacify, transparentize, darken, lighten } from 'polished';
import { includeConditionalArg } from '@storybook/csf';
import { once } from '@storybook/client-logger';
import { Icons } from '../../icon/icon';
import { ArgRow } from './ArgRow';
import { SectionRow } from './SectionRow';
Expand Down Expand Up @@ -372,6 +373,22 @@ const groupRows = (rows: ArgType, sort: SortType) => {
return sorted;
};

/**
* Wrap CSF's `includeConditionalArg` in a try catch so that invalid
* conditionals don't break the entire UI. We can safely swallow the
* error because `includeConditionalArg` is also called in the preview
* in `prepareStory`, and that exception will be bubbled up into the
* UI in a red screen. Nevertheless, we log the error here just in case.
*/
const safeIncludeConditionalArg = (row: ArgType, args: Args, globals: Globals) => {
try {
return includeConditionalArg(row, args, globals);
} catch (err) {
once.warn(err.message);
return false;
}
};

/**
* Display the props for a component as a props table. Each row is a collection of
* ArgDefs, usually derived from docgen info for the component.
Expand Down Expand Up @@ -402,7 +419,7 @@ export const ArgsTable: FC<ArgsTableProps> = (props) => {
const groups = groupRows(
pickBy(
rows,
(row) => !row?.table?.disable && includeConditionalArg(row, args || {}, globals || {})
(row) => !row?.table?.disable && safeIncludeConditionalArg(row, args || {}, globals || {})
),
sort
);
Expand Down