Skip to content

Commit

Permalink
allow StorybookSvelteCSFError.message = "..."
Browse files Browse the repository at this point in the history
  • Loading branch information
JReinhold committed Oct 24, 2024
1 parent b74aee0 commit 6d3815a
Showing 1 changed file with 18 additions and 0 deletions.
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

0 comments on commit 6d3815a

Please sign in to comment.