Skip to content
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
refactor(core): Throw an error when the document is not initialized.
In case the document is accessed but not available we should throw ASAP an error to prevent non explicit errors.
  • Loading branch information
JeanMeche committed May 4, 2023
commit 53dbb0d48cb67a2b76867ae8c8ee4c8f26a1c248
2 changes: 2 additions & 0 deletions goldens/public-api/core/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export const enum RuntimeErrorCode {
// (undocumented)
INVALID_SKIP_HYDRATION_HOST = -504,
// (undocumented)
MISSING_DOCUMENT = 210,
// (undocumented)
MISSING_GENERATED_DEF = 906,
// (undocumented)
MISSING_HYDRATION_ANNOTATIONS = -505,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const enum RuntimeErrorCode {
PROVIDER_IN_WRONG_CONTEXT = 207,
MISSING_INJECTION_TOKEN = 208,
INVALID_MULTI_PROVIDER = -209,
MISSING_DOCUMENT = 210,

// Template Errors
MULTIPLE_COMPONENTS_MATCH = -300,
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/render3/interfaces/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/

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

/**
* Most of the use of `document` in Angular is from within the DI system so it is possible to simply
* inject the `DOCUMENT` token and are done.
Expand Down Expand Up @@ -47,10 +49,15 @@ export function getDocument(): Document {
} else if (typeof document !== 'undefined') {
return document;
}

throw new RuntimeError(
RuntimeErrorCode.MISSING_DOCUMENT,
(typeof ngDevMode === 'undefined' || ngDevMode) &&
`The document object is not available in this context. Make sure the DOCUMENT injection token is provided.`);

// No "document" can be found. This should only happen if we are running ivy outside Angular and
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: maybe move the comment before the throw.

// the current platform is not a browser. Since this is not a supported scenario at the moment
// this should not happen in Angular apps.
// Once we support running ivy outside of Angular we will need to publish `setDocument()` as a
// public API. Meanwhile we just return `undefined` and let the application fail.
return undefined!;
// public API.
}