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

fix: Prevent parser indexer not letting other addon errors to throw #242

Merged
merged 5 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/indexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import {
} from '#utils/error/parser/extract/svelte';
import { LegacyTemplateNotEnabledError } from '#utils/error/legacy-api/index';
import { NoDestructuredDefineMetaCallError } from '#utils/error/parser/analyse/define-meta';
import { isStorybookSvelteCSFError } from '#utils/error';

export const createIndexer = (legacyTemplate: boolean): Indexer => ({
test: /\.svelte$/,
createIndex: async (filename, { makeTitle }) => {
try {
const { meta, stories } = await parseForIndexer(filename, { legacyTemplate });
const { meta, stories } = await parseForIndexer(filename, {
legacyTemplate,
});

return stories.map((story) => {
return {
Expand All @@ -36,10 +39,15 @@ export const createIndexer = (legacyTemplate: boolean): Indexer => ({
error instanceof GetDefineMetaFirstArgumentError
) {
const { filename } = error;
throw new LegacyTemplateNotEnabledError(filename);
throw new LegacyTemplateNotEnabledError(filename, { cause: error });
}

// WARN: We can't use `instanceof StorybookSvelteCSFError`, because is an _abstract_ class
if (isStorybookSvelteCSFError(error)) {
throw error;
}

throw new IndexerParseError();
throw new IndexerParseError({ cause: error });
}
},
});
30 changes: 21 additions & 9 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { SvelteAST } from '#parser/ast';
* and modified for this addon needs.
*/
export abstract class StorybookSvelteCSFError extends Error {
public static isStorybookCSFSvelteError = true;
public static packageName = pkg.name;
public static packageVersion = pkg.version;

Expand Down Expand Up @@ -80,7 +81,7 @@ export abstract class StorybookSvelteCSFError extends Error {
* Generates the error message along with additional documentation link (if applicable).
*/
get message() {
if(this.customMessage) {
if (this.customMessage) {
return this.customMessage;
}

Expand Down Expand Up @@ -115,15 +116,18 @@ export abstract class StorybookSvelteCSFError extends Error {
*/
readonly component?: SvelteAST.Component;

constructor({
filename,
component: component,
}: {
filename?: StorybookSvelteCSFError['filename'];
component?: StorybookSvelteCSFError['component'];
}) {
constructor(
{
filename,
component: component,
}: {
filename?: StorybookSvelteCSFError['filename'];
component?: StorybookSvelteCSFError['component'];
},
options?: ConstructorParameters<typeof Error>[1]
) {
super();

this.cause = options?.cause;
this.filename = filename;
this.component = component;
}
Expand Down Expand Up @@ -178,3 +182,11 @@ export abstract class StorybookSvelteCSFError extends Error {
return `<Story name="${this.storyNameFromAttribute}" />`;
}
}

// WARN: We can't use `instanceof StorybookSvelteCSFError`, because is an _abstract_ class
export function isStorybookSvelteCSFError(error: unknown): error is StorybookSvelteCSFError {
return Boolean(
(Object.getPrototypeOf(error)?.constructor as typeof StorybookSvelteCSFError)
?.isStorybookCSFSvelteError
);
}
3 changes: 2 additions & 1 deletion src/utils/error/legacy-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ export class LegacyTemplateNotEnabledError extends StorybookSvelteCSFError {
readonly code = 2;
public documentation = true;

constructor(filename?: string) {
constructor(filename?: string, options?: ConstructorParameters<typeof Error>[1]) {
super({ filename });
this.cause = options?.cause;
}

template(): string {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/error/parser/extract/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ export class IndexerParseError extends StorybookSvelteCSFError {
readonly code = 9;
public documentation = true;

constructor() {
super({});
constructor(options?: ConstructorParameters<typeof Error>[1]) {
super({}, options);
}

template() {
Expand Down
Loading