From 3b5e794c033191938818a667bcbec6eb3f3a6092 Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 18:16:53 +0800 Subject: [PATCH] check for the existence of a static property in the prototype --- src/utils/error.ts | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/utils/error.ts b/src/utils/error.ts index c24b408..6408aad 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -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; @@ -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 + ); }