Skip to content

Commit

Permalink
fix(@angular/build): improve error message when an unhandled exceptio…
Browse files Browse the repository at this point in the history
…n occurs during prerendering

This change enhances the error messaging when an unhandled exception occurs during the prerendering process. The updated error message provides more context and clarity.

**Previous Behavior**

```
ng b
An unhandled exception occurred: Some error!!!
See "/tmp/ng-S2ABKF/angular-errors.log" for further details.
```

**Updated Behavior:**
```
ng b
Browser bundles
Initial chunk files     | Names               |  Raw size | Estimated transfer size
main-AFPIPGGK.js        | main                | 218.00 kB |                59.48 kB
polyfills-Z2GOM3BN.js   | polyfills           |  35.82 kB |                11.80 kB
styles-5INURTSO.css     | styles              |   0 bytes |                 0 bytes

                        | Initial total       | 253.82 kB |                71.28 kB

Server bundles
Initial chunk files     | Names               |  Raw size
server.mjs              | server              |   1.11 MB |
chunk-HZL5H5M5.mjs      | -                   | 526.77 kB |
polyfills.server.mjs    | polyfills.server    | 269.91 kB |
chunk-GFWAPST7.mjs      | -                   |  19.16 kB |
chunk-5XUXGTUW.mjs      | -                   |   2.55 kB |
render-utils.server.mjs | render-utils.server |   1.46 kB |
main.server.mjs         | main.server         | 149 bytes |

Lazy chunk files        | Names               |  Raw size
chunk-7YC4RJ5P.mjs      | xhr2                |  12.08 kB |

Prerendered 1 static route.
Application bundle generation failed. [4.923 seconds]

✘ [ERROR] An error occurred while prerendering route '/'.

Error: Some error!!!
    at render (node_modules/@angular/build/src/utils/server-rendering/render-worker.js:20:20)
    at /angular-cli/abc/node_modules/piscina/dist/worker.js:146:32
```

Closes #28212

(cherry picked from commit 71c06c6)
  • Loading branch information
alan-agius4 committed Aug 15, 2024
1 parent 94e27c8 commit 83b2699
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
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}`);
})
.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

0 comments on commit 83b2699

Please sign in to comment.