Skip to content

fix(core): properly recognize failed fetch responses when loading external resources in JIT #62992

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions goldens/public-api/core/errors.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export const enum RuntimeErrorCode {
// (undocumented)
EXPRESSION_CHANGED_AFTER_CHECKED = -100,
// (undocumented)
EXTERNAL_RESOURCE_LOADING_FAILED = 918,
// (undocumented)
HOST_DIRECTIVE_COMPONENT = 310,
// (undocumented)
HOST_DIRECTIVE_CONFLICTING_ALIAS = 312,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export const enum RuntimeErrorCode {
MISSING_NG_MODULE_DEFINITION = 915,
MISSING_DIRECTIVE_DEFINITION = 916,
NO_COMPONENT_FACTORY_FOUND = 917,
EXTERNAL_RESOURCE_LOADING_FAILED = 918,

// Signal integration errors
REQUIRED_INPUT_NO_VALUE = -950,
Expand All @@ -143,7 +144,7 @@ export const enum RuntimeErrorCode {
RUNTIME_DEPS_INVALID_IMPORTED_TYPE = 980,
RUNTIME_DEPS_ORPHAN_COMPONENT = 981,

// Resource errors
// resource() API errors
MUST_PROVIDE_STREAM_OPTION = 990,
RESOURCE_COMPLETED_BEFORE_PRODUCING_VALUE = 991,

Expand Down
23 changes: 19 additions & 4 deletions packages/core/src/metadata/resource_loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {RuntimeError, RuntimeErrorCode} from '../errors';
import {Type} from '../interface/type';

import type {Component} from './directives';
Expand Down Expand Up @@ -43,7 +44,7 @@ import type {Component} from './directives';
* contents of the resolved URL. Browser's `fetch()` method is a good default implementation.
*/
export function resolveComponentResources(
resourceResolver: (url: string) => Promise<string | {text(): Promise<string>}>,
resourceResolver: (url: string) => Promise<string | {text(): Promise<string>; status?: number}>,
): Promise<void> {
// Store all promises which are fetching the resources.
const componentResolved: Promise<void>[] = [];
Expand All @@ -54,7 +55,7 @@ export function resolveComponentResources(
let promise = urlMap.get(url);
if (!promise) {
const resp = resourceResolver(url);
urlMap.set(url, (promise = resp.then(unwrapResponse)));
urlMap.set(url, (promise = resp.then((res) => unwrapResponse(url, res))));
}
return promise;
}
Expand Down Expand Up @@ -147,8 +148,22 @@ export function isComponentResourceResolutionQueueEmpty() {
return componentResourceResolutionQueue.size === 0;
}

function unwrapResponse(response: string | {text(): Promise<string>}): string | Promise<string> {
return typeof response == 'string' ? response : response.text();
function unwrapResponse(
url: string,
response: string | {text(): Promise<string>; status?: number},
): string | Promise<string> {
if (typeof response === 'string') {
return response;
}
if (response.status !== undefined && response.status !== 200) {
return Promise.reject(
new RuntimeError(
RuntimeErrorCode.EXTERNAL_RESOURCE_LOADING_FAILED,
ngDevMode && `Could not load resource: ${url}. Response status: ${response.status}`,
),
);
}
return response.text();
}

function componentDefResolved(type: Type<any>): void {
Expand Down
17 changes: 17 additions & 0 deletions packages/core/test/metadata/resource_loading_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,22 @@ Did you run and wait for 'resolveComponentResources()'?`.trim(),
expect(MyComponent.ɵcmp).toBeDefined();
expect(metadata.template).toBe('response for test://content');
});

it('should fail when fetch is resolving to a 404', async () => {
const MyComponent: ComponentType<any> = class MyComponent {} as any;
const metadata: Component = {templateUrl: 'test://content'};
compileComponent(MyComponent, metadata);

await expectAsync(
resolveComponentResources(async () => {
return {
async text() {
return 'File not found';
},
status: 404,
};
}),
).toBeRejectedWithError(/Could not load resource.*404/);
});
});
});