Skip to content

fix(@angular/build): improve error message when an unhandled exception occurs during prerendering #28215

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

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
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
57 changes: 37 additions & 20 deletions packages/angular/build/src/utils/server-rendering/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ export async function prerenderPages(
}

// Get routes to prerender
const { routes: allRoutes, warnings: routesWarnings } = await getAllRoutes(
const {
routes: allRoutes,
warnings: routesWarnings,
errors: routesErrors,
} = await getAllRoutes(
workspaceRoot,
outputFilesForWorker,
assetsReversed,
Expand All @@ -92,11 +96,15 @@ export async function prerenderPages(
verbose,
);

if (routesErrors?.length) {
errors.push(...routesErrors);
}

if (routesWarnings?.length) {
warnings.push(...routesWarnings);
}

if (allRoutes.size < 1) {
if (allRoutes.size < 1 || errors.length > 0) {
return {
errors,
warnings,
Expand Down Expand Up @@ -190,22 +198,27 @@ async function renderPages(
const isAppShellRoute = appShellRoute === route;
const serverContext: ServerContext = isAppShellRoute ? 'app-shell' : 'ssg';
const render: Promise<RenderResult> = renderWorker.run({ route, serverContext });
const renderResult: Promise<void> = render.then(({ content, warnings, errors }) => {
if (content !== undefined) {
const outPath = isAppShellRoute
? 'index.html'
: posix.join(removeLeadingSlash(route), 'index.html');
output[outPath] = content;
}

if (warnings) {
warnings.push(...warnings);
}

if (errors) {
errors.push(...errors);
}
});
const renderResult: Promise<void> = render
.then(({ content, warnings, errors }) => {
if (content !== undefined) {
const outPath = isAppShellRoute
? 'index.html'
: posix.join(removeLeadingSlash(route), 'index.html');
output[outPath] = content;
}

if (warnings) {
warnings.push(...warnings);
}

if (errors) {
errors.push(...errors);
}
})
.catch((err) => {
errors.push(`An error occurred while prerendering route '${route}'.\n\n${err.stack}`);
void renderWorker.destroy();
});

renderingPromises.push(renderResult);
}
Expand All @@ -231,7 +244,7 @@ async function getAllRoutes(
prerenderOptions: PrerenderOptions,
sourcemap: boolean,
verbose: boolean,
): Promise<{ routes: Set<string>; warnings?: string[] }> {
): Promise<{ routes: Set<string>; warnings?: string[]; errors?: string[] }> {
const { routesFile, discoverRoutes } = prerenderOptions;
const routes = new RoutesSet();
const { route: appShellRoute } = appShellOptions;
Expand Down Expand Up @@ -275,8 +288,12 @@ async function getAllRoutes(
recordTiming: false,
});

const errors: string[] = [];
const { routes: extractedRoutes, warnings }: RoutersExtractorWorkerResult = await renderWorker
.run({})
.catch((err) => {
errors.push(`An error occurred while extracting routes.\n\n${err.stack}`);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I opted to display the stack trace instead of just the error message. This can help users identify the exact part of their application that is causing the error.

Alternatively, I can modify this behavior to only display the stack trace when verbose mode is enabled.

})
.finally(() => {
void renderWorker.destroy();
});
Expand All @@ -285,7 +302,7 @@ async function getAllRoutes(
routes.add(route);
}

return { routes, warnings };
return { routes, warnings, errors };
}

function addLeadingSlash(value: string): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ describeBuilder(execute, SERVER_BUILDER_INFO, (harness) => {
it('emits errors', async () => {
harness.useTarget('server', {
...BASE_OPTIONS,
watch: true,
});

// Generate an error
await harness.appendToFile('src/main.server.ts', `const foo: = 'abc';`);

const { result, logs } = await harness.executeOnce();
const { result, logs } = await harness.executeOnce({
outputLogsOnFailure: false,
});

expect(result?.success).toBeFalse();
expect(logs).toContain(
Expand Down