Skip to content

Commit 67a2111

Browse files
authored
Merge pull request #26862 from storybookjs/yann/fix-docgen-convert-crash
Controls: Fix crashing when docgen extraction partially fails
2 parents 316779b + 2fb5ed5 commit 67a2111

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

code/lib/core-events/src/errors/preview-errors.test.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ describe('UnknownFlowArgTypesError', () => {
88
raw: "SomeType['someProperty']",
99
};
1010

11-
const message = `"There was a failure when generating ArgTypes in Typescript for {"name":"signature","raw":"SomeType['someProperty']"}
12-
This type is either not supported or it is a bug in Storybook.
13-
If you think this is a bug, please open an issue in Github."`;
14-
1511
const typeError = new UnknownArgTypesError({ type, language: 'Typescript' });
16-
expect(typeError.message).toMatchInlineSnapshot(message);
12+
expect(typeError.message).toMatchInlineSnapshot(`
13+
"There was a failure when generating detailed ArgTypes in Typescript for:
14+
15+
{
16+
"name": "signature",
17+
"raw": "SomeType['someProperty']"
18+
}
19+
20+
Storybook will fall back to use a generic type description instead.
21+
22+
This type is either not supported or it is a bug in the docgen generation in Storybook.
23+
If you think this is a bug, please detail it as much as possible in the Github issue.
24+
25+
More info: https://github.com/storybookjs/storybook/issues/26606
26+
"
27+
`);
1728
});
1829
});

code/lib/core-events/src/errors/preview-errors.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,22 @@ export class UnknownArgTypesError extends StorybookError {
259259

260260
readonly code = 1;
261261

262+
readonly documentation = 'https://github.com/storybookjs/storybook/issues/26606';
263+
262264
constructor(public data: { type: object; language: string }) {
263265
super();
264266
}
265267

266268
template() {
267-
return `There was a failure when generating ArgTypes in ${
269+
return dedent`There was a failure when generating detailed ArgTypes in ${
268270
this.data.language
269-
} for ${JSON.stringify(this.data.type)}
270-
This type is either not supported or it is a bug in Storybook.
271-
If you think this is a bug, please open an issue in Github.`;
271+
} for:
272+
273+
${JSON.stringify(this.data.type, null, 2)}
274+
275+
Storybook will fall back to use a generic type description instead.
276+
277+
This type is either not supported or it is a bug in the docgen generation in Storybook.
278+
If you think this is a bug, please detail it as much as possible in the Github issue.`;
272279
}
273280
}

code/lib/docs-tools/src/argTypes/convert/index.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import { convert as propTypesConvert } from './proptypes';
77

88
export const convert = (docgenInfo: DocgenInfo) => {
99
const { type, tsType, flowType } = docgenInfo;
10-
if (type != null) return propTypesConvert(type);
11-
if (tsType != null) return tsConvert(tsType as TSType);
12-
if (flowType != null) return flowConvert(flowType as FlowType);
10+
try {
11+
if (type != null) return propTypesConvert(type);
12+
if (tsType != null) return tsConvert(tsType as TSType);
13+
if (flowType != null) return flowConvert(flowType as FlowType);
14+
} catch (err) {
15+
// if we can't convert the type, we'll just return null to fallback to a simple summary, and provide the error to the user
16+
console.error(err);
17+
}
1318

1419
return null;
1520
};

0 commit comments

Comments
 (0)