diff --git a/src/indexer/index.ts b/src/indexer/index.ts index d235009..7e2dcca 100644 --- a/src/indexer/index.ts +++ b/src/indexer/index.ts @@ -9,12 +9,15 @@ import { } from '$lib/utils/error/parser/extract/svelte.js'; import { LegacyTemplateNotEnabledError } from '$lib/utils/error/legacy-api/index.js'; import { NoDestructuredDefineMetaCallError } from '$lib/utils/error/parser/analyse/define-meta.js'; +import { isStorybookSvelteCSFError } from '$lib/utils/error.js'; 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 { @@ -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 }); } }, }); diff --git a/src/utils/error.ts b/src/utils/error.ts index 426103d..690259d 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -10,6 +10,7 @@ import type { SvelteAST } from '$lib/parser/ast.js'; * 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; @@ -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; } @@ -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[1] + ) { super(); - + this.cause = options?.cause; this.filename = filename; this.component = component; } @@ -178,3 +182,11 @@ export abstract class StorybookSvelteCSFError extends Error { return ``; } } + +// 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 + ); +} diff --git a/src/utils/error/legacy-api/index.ts b/src/utils/error/legacy-api/index.ts index 46b5f70..757f08c 100644 --- a/src/utils/error/legacy-api/index.ts +++ b/src/utils/error/legacy-api/index.ts @@ -36,8 +36,9 @@ export class LegacyTemplateNotEnabledError extends StorybookSvelteCSFError { readonly code = 2; public documentation = true; - constructor(filename?: string) { + constructor(filename?: string, options?: ConstructorParameters[1]) { super({ filename }); + this.cause = options?.cause; } template(): string { diff --git a/src/utils/error/parser/extract/svelte.ts b/src/utils/error/parser/extract/svelte.ts index 064d098..9ba21e3 100644 --- a/src/utils/error/parser/extract/svelte.ts +++ b/src/utils/error/parser/extract/svelte.ts @@ -218,8 +218,8 @@ export class IndexerParseError extends StorybookSvelteCSFError { readonly code = 9; public documentation = true; - constructor() { - super({}); + constructor(options?: ConstructorParameters[1]) { + super({}, options); } template() {