Skip to content

Commit

Permalink
categorize webpack builder error
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Aug 31, 2023
1 parent 5079357 commit 78ef2f5
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
4 changes: 2 additions & 2 deletions code/builders/builder-webpack5/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { dirname, join, parse } from 'path';
import express from 'express';
import fs from 'fs-extra';
import { PREVIEW_BUILDER_PROGRESS } from '@storybook/core-events';
import { WebpackCompilationError } from '@storybook/core-events/server-errors';

import prettyTime from 'pretty-hrtime';

Expand Down Expand Up @@ -195,8 +196,7 @@ const starter: StarterFunction = async function* starterGeneratorFn({
}

if (stats.hasErrors()) {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw stats;
throw new WebpackCompilationError({ error: stats });
}

return {
Expand Down
61 changes: 61 additions & 0 deletions code/lib/core-events/src/errors/server-errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable local-rules/no-uncategorized-errors */
import { WebpackCompilationError } from './server-errors';

describe('WebpackCompilationError', () => {
it('should correctly handle error with error property', () => {
const error = new Error('Custom error message');
const data = {
error,
};

const webpackError = new WebpackCompilationError(data);

expect(webpackError.message).toBe(error.message);
});

it('should correctly handle error with error within error', () => {
const error = new Error('Custom error message');
const data = {
error: new Error(),
};
data.error = error;

const webpackError = new WebpackCompilationError(data);

expect(webpackError.message).toBe(error.message);
});

it('should correctly handle error with stats.compilation.errors', () => {
const compilationErrors = [new Error('Error 1 message'), new Error('Error 2 message')];
const data = new Error() as Error & {
error: Error & { stats?: { compilation: { errors: Error[] } } };
};
data.error = new Error();
data.error.stats = {
compilation: {
errors: compilationErrors,
},
};

const webpackError = new WebpackCompilationError(data);

expect(webpackError.message).toMatchInlineSnapshot(`
"Error: Error 1 message
Error: Error 2 message"
`);
});

it('should correctly handle error without specific format', () => {
const errorMessage = 'Generic error message';
const data = new Error() as Error & {
error: Error;
};

data.error = new Error(errorMessage);

const webpackError = new WebpackCompilationError(data);

expect(webpackError.message).toBe(errorMessage);
});
});
45 changes: 45 additions & 0 deletions code/lib/core-events/src/errors/server-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,48 @@ export class InvalidStoriesEntryError extends StorybookError {
`;
}
}

export class WebpackCompilationError extends StorybookError {
readonly category = Category.BUILDER_WEBPACK5;

readonly code = 1;

private errorMessage = '';

constructor(
public data: {
error:
| (Error & {
error?: Error;
stats?: { compilation: { errors: Error[] } };
compilation?: { errors: Error[] };
})
| {
compilation?: { errors: Error[] };
};
}
) {
super();

if (data.error instanceof Error) {
if (data.error.error) {
this.errorMessage = data.error.error.message;
this.stack = data.error.error.stack;
} else if (data.error.stats && data.error.stats.compilation.errors) {
data.error.stats.compilation.errors.forEach((e: Error) => {
this.errorMessage += `${e.name}: ${e.message}\n\n`;
});
} else {
this.errorMessage = data.error.message;
}
} else if (data.error.compilation?.errors) {
data.error.compilation.errors.forEach((e: Error) => {
this.errorMessage += `${e.name}: ${e.message}\n\n`;
});
}
}

template() {
return this.errorMessage.trim();
}
}
2 changes: 1 addition & 1 deletion code/lib/core-server/src/withTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { telemetry, getPrecedingUpgrade, oneWayHash } from '@storybook/telemetry
import type { EventType } from '@storybook/telemetry';
import { logger } from '@storybook/node-logger';

export type TelemetryOptions = {
type TelemetryOptions = {
cliOptions: CLIOptions;
presetOptions?: Parameters<typeof loadAllPresets>[0];
printError?: (err: any) => void;
Expand Down

0 comments on commit 78ef2f5

Please sign in to comment.