Repository navigation
[Backport 21.2.X ] fix(@angular/ssr): wrap non-Error values thrown while rendering #34805
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
Open
SkyZeroZx
wants to merge
2
commits into
angular:21.2.x
Choose a base branch
from
SkyZeroZx:fix/ssr-non-error-render-rejection-21.2.x
base: 21.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+241
−17
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| /** | ||
| * Ensures that a value thrown while handling a request or rendering the application is an `Error`. | ||
| * | ||
| * An application can throw or reject with any value, such as an `HttpErrorResponse` from a failed | ||
| * `HttpClient` request. Passing non-`Error` values to server frameworks can cause unexpected | ||
| * behavior, such as Express's `finalhandler` setting non-header properties from `err.headers` or | ||
| * Fastify serializing the value as a `200 OK` JSON response. | ||
| * | ||
| * @param error - The value thrown by the application. | ||
| * @returns The value itself when it is an `Error`, otherwise an `Error` with the value as its `cause`. | ||
| */ | ||
| export function ensureError(error: unknown): Error { | ||
| return error instanceof Error | ||
| ? error | ||
| : new Error( | ||
| 'The application failed to render with a value that is not an Error instance. See its cause for the original value.', | ||
| { cause: error }, | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { ensureError } from '../../src/utils/error'; | ||
|
|
||
| describe('Error Utils', () => { | ||
| describe('ensureError', () => { | ||
| it('should return the same instance when the value is an Error', () => { | ||
| const error = new Error('Render error'); | ||
|
|
||
| expect(ensureError(error)).toBe(error); | ||
| }); | ||
|
|
||
| it('should return the same instance when the value is an Error subclass', () => { | ||
| class HttpError extends Error { | ||
| readonly status = 503; | ||
| readonly headers = { 'retry-after': '120' }; | ||
| } | ||
|
|
||
| const typeError = new TypeError('Type error'); | ||
| const httpError = new HttpError('Service unavailable'); | ||
|
|
||
| expect(ensureError(typeError)).toBe(typeError); | ||
| expect(ensureError(httpError)).toBe(httpError); | ||
| }); | ||
|
|
||
| it('should return the same instance when the value is an AbortError DOMException', () => { | ||
| const abortError = new DOMException('Request was aborted.', 'AbortError'); | ||
|
|
||
| expect(ensureError(abortError)).toBe(abortError); | ||
| }); | ||
|
|
||
| it('should wrap primitives, null and undefined in an Error with the value as its cause', () => { | ||
| for (const value of ['Render error', 503, false, Symbol('error'), null, undefined]) { | ||
| const error = ensureError(value); | ||
|
|
||
| expect(error).toBeInstanceOf(Error); | ||
| expect(error.message).toMatch(/not an Error/); | ||
| expect(error.cause).toBe(value); | ||
| } | ||
| }); | ||
|
|
||
| it('should wrap error-like plain objects in an Error with the object as its cause', () => { | ||
| const errorLike = { | ||
| name: 'HttpErrorResponse', | ||
| message: 'Http failure response for http://localhost/api: 503 Service Unavailable', | ||
| status: 503, | ||
| headers: { lazyInit: undefined }, | ||
| }; | ||
|
|
||
| const error = ensureError(errorLike); | ||
|
|
||
| expect(error).toBeInstanceOf(Error); | ||
| expect(error).not.toBe(errorLike as unknown as Error); | ||
| expect(error.message).toMatch(/not an Error/); | ||
| expect(error.cause).toBe(errorLike); | ||
| // The message and the properties read by server frameworks are not copied from the value. | ||
| expect(error.message).not.toContain(errorLike.message); | ||
| expect('status' in error).toBeFalse(); | ||
| expect('headers' in error).toBeFalse(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.