Skip to content

Commit

Permalink
fix: Prevent parser indexer not letting other addon errors to throw (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
xeho91 authored Dec 13, 2024
1 parent 52910d0 commit acd17d1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
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

0 comments on commit acd17d1

Please sign in to comment.