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

Doc Tools: Signature Type Error Handling #26774

Merged
merged 12 commits into from
Apr 16, 2024
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`UnknownFlowArgTypesError > should correctly handle error with Flow convertSig > There was a failure when generating ArgTypes
in Typescript for {"name":"signature","raw":"SomeType['someProperty']"}
This type is either not supported or it is a bug in Storybook.
If you think this is a bug, please open an issue in Github. 1`] = `
"There was a failure when generating ArgTypes in Typescript for {"name":"signature","raw":"SomeType['someProperty']"}.
This type is either not supported or it is a bug in Storybook.
If you think this is a bug, please open an issue in Github."
`;

exports[`UnknownFlowArgTypesError > should correctly handle error with Flow convertSig > There was a failure when generating ArgTypes
in Typescript for {"name":"signature","raw":"SomeType['someProperty']"}
This type is either not supported or it is a bug in Storybook.
If you think this is a bug, please open an issue in Github. 1`] = `
"There was a failure when generating ArgTypes in Typescript for {"name":"signature","raw":"SomeType['someProperty']"}.
This type is either not supported or it is a bug in Storybook.
If you think this is a bug, please open an issue in Github."
`;
19 changes: 19 additions & 0 deletions code/lib/core-events/src/errors/preview-errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, it, expect } from 'vitest';
import { UnknownArgTypesError } from './preview-errors';

describe('UnknownFlowArgTypesError', () => {
it('should correctly handle error with Flow convertSig', () => {
const type = {
name: 'signature',
raw: "SomeType['someProperty']",
};

const message = `There was a failure when generating ArgTypes
in Typescript for {"name":"signature","raw":"SomeType['someProperty']"}
This type is either not supported or it is a bug in Storybook.
If you think this is a bug, please open an issue in Github.`;

const typeError = new UnknownArgTypesError({ type, language: 'Typescript' });
expect(typeError.message).toMatchSnapshot(message);
ethriel3695 marked this conversation as resolved.
Show resolved Hide resolved
});
});
19 changes: 19 additions & 0 deletions code/lib/core-events/src/errors/preview-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { StorybookError } from './storybook-error';
* to prevent manager and preview errors from having the same category and error code.
*/
export enum Category {
DOCS_TOOLS = 'DOCS-TOOLS',
PREVIEW_CLIENT_LOGGER = 'PREVIEW_CLIENT-LOGGER',
PREVIEW_CHANNELS = 'PREVIEW_CHANNELS',
PREVIEW_CORE_EVENTS = 'PREVIEW_CORE-EVENTS',
Expand Down Expand Up @@ -252,3 +253,21 @@ export class NextJsSharpError extends StorybookError {
`;
}
}

export class UnknownArgTypesError extends StorybookError {
readonly category = Category.DOCS_TOOLS;

readonly code = 1;

constructor(public data: { type: object; language: string }) {
super();
}

template() {
return dedent`There was a failure when generating ArgTypes in ${
this.data.language
} for ${JSON.stringify(this.data.type)}.
This type is either not supported or it is a bug in Storybook.
If you think this is a bug, please open an issue in Github.`;
}
}
1 change: 1 addition & 0 deletions code/lib/docs-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
},
"dependencies": {
"@storybook/core-common": "workspace:*",
"@storybook/core-events": "workspace:*",
"@storybook/preview-api": "workspace:*",
"@storybook/types": "workspace:*",
"@types/doctrine": "^0.0.3",
Expand Down
3 changes: 2 additions & 1 deletion code/lib/docs-tools/src/argTypes/convert/flow/convert.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UnknownArgTypesError } from '@storybook/core-events/preview-errors';
import type { SBType } from '@storybook/types';
import type { FlowType, FlowSigType, FlowLiteralType } from './types';

Expand All @@ -18,7 +19,7 @@ const convertSig = (type: FlowSigType) => {
value: values,
};
default:
throw new Error(`Unknown: ${type}`);
throw new UnknownArgTypesError({ type: type, language: 'Flow' });
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UnknownArgTypesError } from '@storybook/core-events/preview-errors';
import type { SBType } from '@storybook/types';
import type { TSType, TSSigType } from './types';
import { parseLiteral } from '../utils';
Expand All @@ -16,7 +17,7 @@ const convertSig = (type: TSSigType) => {
value: values,
};
default:
throw new Error(`Unknown: ${type}`);
throw new UnknownArgTypesError({ type, language: 'Typescript' });
}
};

Expand Down
1 change: 1 addition & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5903,6 +5903,7 @@ __metadata:
dependencies:
"@babel/preset-react": "npm:^7.23.3"
"@storybook/core-common": "workspace:*"
"@storybook/core-events": "workspace:*"
"@storybook/preview-api": "workspace:*"
"@storybook/types": "workspace:*"
"@types/doctrine": "npm:^0.0.3"
Expand Down