Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors at enhanceRollupError in Vite #222

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export abstract class StorybookSvelteCSFError extends Error {
*/
readonly fromStorybook: true = true as const;

/**
* Any custom message to override the default error message.
* Vite overrides the `Error.message` property, so we support that.
*/
private customMessage?: string;

get fullErrorCode() {
const paddedCode = String(this.code).padStart(4, '0');
return `SB_SVELTE_CSF_${this.category}_${paddedCode}` as `SB_SVELTE_CSF_${this['category']}_${string}`;
Expand All @@ -74,6 +80,10 @@ export abstract class StorybookSvelteCSFError extends Error {
* Generates the error message along with additional documentation link (if applicable).
*/
get message() {
if(this.customMessage) {
return this.customMessage;
}

let page: string | undefined;

if (this.documentation === true) {
Expand All @@ -87,6 +97,14 @@ export abstract class StorybookSvelteCSFError extends Error {
return `${this.template()}${page != null ? `\n\nMore info: ${page}\n` : ''}`;
}

/**
* Allows anyone to set Error.message after creation, mimicking the native Error behavior.
* Vite does this sometimes.
*/
set message(message: string) {
this.customMessage = message;
}

/**
* `*.stories.svelte` file path where the error has occurred.
*/
Expand Down
Loading