Skip to content

Commit

Permalink
check for the existence of a static property in the prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
xeho91 committed Dec 13, 2024
1 parent c5f8af5 commit 3b5e794
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 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 @@ -179,17 +180,10 @@ export abstract class StorybookSvelteCSFError extends Error {
}
}

// WARN: We can't use instance of `StorybookSvelteCSFError`, because is an _abstract_ class :sob:
export function isStorybookSvelteCSFError(error: unknown) {
if (typeof error !== 'object' || error === null) {
return false;
}

for (const key of ['category', 'code', 'data', 'documentation', 'fullErrorCode', 'template']) {
if (!Object.hasOwn(error, key)) {
return false;
}
}

return true;
// 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
);
}

0 comments on commit 3b5e794

Please sign in to comment.