|
8 | 8 |
|
9 | 9 | import type {ProviderToken} from '../di'; |
10 | 10 | import {isEnvironmentProviders} from '../di/interface/provider'; |
11 | | -import {RuntimeError, RuntimeErrorCode} from '../errors'; |
| 11 | +import {formatRuntimeError, RuntimeError, RuntimeErrorCode} from '../errors'; |
12 | 12 | import {Type} from '../interface/type'; |
| 13 | +import {assertDefined} from '../util/assert'; |
| 14 | +import {getClosureSafeProperty} from '../util/property'; |
13 | 15 | import {stringify} from '../util/stringify'; |
14 | 16 |
|
15 | 17 | import {stringifyForError} from './util/stringify_utils'; |
16 | 18 |
|
17 | | -/** Called when directives inject each other (creating a circular dependency) */ |
18 | | -export function throwCyclicDependencyError(token: string, path?: string[]): never { |
19 | | - throw new RuntimeError( |
20 | | - RuntimeErrorCode.CYCLIC_DI_DEPENDENCY, |
21 | | - ngDevMode |
22 | | - ? `Circular dependency in DI detected for ${token}${path ? `. Dependency path: ${path.join(' > ')} > ${token}` : ''}` |
23 | | - : token, |
24 | | - ); |
| 19 | +const NG_RUNTIME_ERROR_CODE = getClosureSafeProperty({'ngErrorCode': getClosureSafeProperty}); |
| 20 | +const NG_RUNTIME_ERROR_MESSAGE = getClosureSafeProperty({'ngErrorMessage': getClosureSafeProperty}); |
| 21 | +const NG_TOKEN_PATH = getClosureSafeProperty({'ngTokenPath': getClosureSafeProperty}); |
| 22 | + |
| 23 | +/** Creates a circular dependency runtime error. */ |
| 24 | +export function cyclicDependencyError(token: string, path?: string[]): Error { |
| 25 | + const message = ngDevMode ? `Circular dependency detected for \`${token}\`.` : ''; |
| 26 | + return createRuntimeError(message, RuntimeErrorCode.CYCLIC_DI_DEPENDENCY, path); |
| 27 | +} |
| 28 | + |
| 29 | +/** Creates a circular dependency runtime error including a dependency path in the error message. */ |
| 30 | +export function cyclicDependencyErrorWithDetails(token: string, path: string[]): Error { |
| 31 | + return augmentRuntimeError(cyclicDependencyError(token, path), null); |
25 | 32 | } |
26 | 33 |
|
27 | 34 | export function throwMixedMultiProviderError() { |
@@ -67,3 +74,89 @@ export function throwProviderNotFoundError( |
67 | 74 | `No provider for ${stringifyForError(token)} found${injectorName ? ` in ${injectorName}` : ''}`; |
68 | 75 | throw new RuntimeError(RuntimeErrorCode.PROVIDER_NOT_FOUND, errorMessage); |
69 | 76 | } |
| 77 | + |
| 78 | +/** |
| 79 | + * Given an Error instance and the current token - update the monkey-patched |
| 80 | + * dependency path info to include that token. |
| 81 | + * |
| 82 | + * @param error Current instance of the Error class. |
| 83 | + * @param token Extra token that should be appended. |
| 84 | + */ |
| 85 | +export function prependTokenToDependencyPath( |
| 86 | + error: any, |
| 87 | + token: ProviderToken<unknown> | {multi: true; provide: ProviderToken<unknown>}, |
| 88 | +): void { |
| 89 | + error[NG_TOKEN_PATH] ??= []; |
| 90 | + // Append current token to the current token path. Since the error |
| 91 | + // is bubbling up, add the token in front of other tokens. |
| 92 | + const currentPath = error[NG_TOKEN_PATH]; |
| 93 | + // Do not append the same token multiple times. |
| 94 | + let pathStr: string; |
| 95 | + if (typeof token === 'object' && 'multi' in token && token?.multi === true) { |
| 96 | + assertDefined(token.provide, 'Token with multi: true should have a provide property'); |
| 97 | + pathStr = stringifyForError(token.provide); |
| 98 | + } else { |
| 99 | + pathStr = stringifyForError(token); |
| 100 | + } |
| 101 | + |
| 102 | + if (currentPath[0] !== pathStr) { |
| 103 | + (error[NG_TOKEN_PATH] as string[]).unshift(pathStr); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Modifies an Error instance with an updated error message |
| 109 | + * based on the accumulated dependency path. |
| 110 | + * |
| 111 | + * @param error Current instance of the Error class. |
| 112 | + * @param source Extra info about the injector which started |
| 113 | + * the resolution process, which eventually failed. |
| 114 | + */ |
| 115 | +export function augmentRuntimeError(error: any, source: string | null): Error { |
| 116 | + const tokenPath: string[] = error[NG_TOKEN_PATH]; |
| 117 | + const errorCode = error[NG_RUNTIME_ERROR_CODE]; |
| 118 | + const message = error[NG_RUNTIME_ERROR_MESSAGE] || error.message; |
| 119 | + error.message = formatErrorMessage(message, errorCode, tokenPath, source); |
| 120 | + return error; |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Creates an initial RuntimeError instance when a problem is detected. |
| 125 | + * Monkey-patches extra info in the RuntimeError instance, so that it can |
| 126 | + * be reused later, before throwing the final error. |
| 127 | + */ |
| 128 | +export function createRuntimeError(message: string, code: number, path?: string[]): Error { |
| 129 | + // Cast to `any`, so that extra info can be monkey-patched onto this instance. |
| 130 | + const error = new RuntimeError(code, message) as any; |
| 131 | + |
| 132 | + // Monkey-patch a runtime error code and a path onto an Error instance. |
| 133 | + error[NG_RUNTIME_ERROR_CODE] = code; |
| 134 | + error[NG_RUNTIME_ERROR_MESSAGE] = message; |
| 135 | + if (path) { |
| 136 | + error[NG_TOKEN_PATH] = path; |
| 137 | + } |
| 138 | + return error; |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Reads monkey-patched error code from the given Error instance. |
| 143 | + */ |
| 144 | +export function getRuntimeErrorCode(error: any): number | undefined { |
| 145 | + return error[NG_RUNTIME_ERROR_CODE]; |
| 146 | +} |
| 147 | + |
| 148 | +function formatErrorMessage( |
| 149 | + text: string, |
| 150 | + code: number, |
| 151 | + path: string[] = [], |
| 152 | + source: string | null = null, |
| 153 | +): string { |
| 154 | + let pathDetails = ''; |
| 155 | + // If the path is empty or contains only one element (self) - |
| 156 | + // do not append additional info the error message. |
| 157 | + if (path && path.length > 1) { |
| 158 | + pathDetails = ` Path: ${path.join(' -> ')}.`; |
| 159 | + } |
| 160 | + const sourceDetails = source ? ` Source: ${source}.` : ''; |
| 161 | + return formatRuntimeError(code, `${text}${sourceDetails}${pathDetails}`); |
| 162 | +} |
0 commit comments