UNPKG

storybook

Version:

Storybook: Develop, document, and test UI components in isolation

79 lines (73 loc) 2.77 kB
import CJS_COMPAT_NODE_URL_yr66iw5gef from 'node:url'; import CJS_COMPAT_NODE_PATH_yr66iw5gef from 'node:path'; import CJS_COMPAT_NODE_MODULE_yr66iw5gef from "node:module"; var __filename = CJS_COMPAT_NODE_URL_yr66iw5gef.fileURLToPath(import.meta.url); var __dirname = CJS_COMPAT_NODE_PATH_yr66iw5gef.dirname(__filename); var require = CJS_COMPAT_NODE_MODULE_yr66iw5gef.createRequire(import.meta.url); // ------------------------------------------------------------ // end of CJS compatibility banner, injected by Storybook's esbuild configuration // ------------------------------------------------------------ // src/storybook-error.ts function parseErrorCode({ code, category }) { let paddedCode = String(code).padStart(4, "0"); return `SB_${category}_${paddedCode}`; } function appendErrorRef(url) { if (/^(?!.*storybook\.js\.org)|[?&]ref=error\b/.test(url)) return url; try { let urlObj = new URL(url); return urlObj.searchParams.set("ref", "error"), urlObj.toString(); } catch { return url; } } var StorybookError = class _StorybookError extends Error { constructor(props) { super(_StorybookError.getFullMessage(props)); /** * Data associated with the error. Used to provide additional information in the error message or * to be passed to telemetry. */ this.data = {}; /** Flag used to easily determine if the error originates from Storybook. */ this.fromStorybook = !0; /** * Flag used to determine if the error is handled by us and should therefore not be shown to the * user. */ this.isHandledError = !1; this.category = props.category, this.documentation = props.documentation ?? !1, this.code = props.code, this.isHandledError = props.isHandledError ?? !1, this.name = props.name; } get fullErrorCode() { return parseErrorCode({ code: this.code, category: this.category }); } /** Overrides the default `Error.name` property in the format: SB_<CATEGORY>_<CODE>. */ get name() { let errorName = this._name || this.constructor.name; return `${this.fullErrorCode} (${errorName})`; } set name(name) { this._name = name; } /** Generates the error message along with additional documentation link (if applicable). */ static getFullMessage({ documentation, code, category, message }) { let page; return documentation === !0 ? page = `https://storybook.js.org/error/${parseErrorCode({ code, category })}?ref=error` : typeof documentation == "string" ? page = appendErrorRef(documentation) : Array.isArray(documentation) && (page = ` ${documentation.map((doc) => ` - ${appendErrorRef(doc)}`).join(` `)}`), `${message}${page != null ? ` More info: ${page} ` : ""}`; } }; export { StorybookError };