diff --git a/packages/core/test/bundling/router/bundle.golden_symbols.json b/packages/core/test/bundling/router/bundle.golden_symbols.json index 08a5acf868f00..8e1abf3f975af 100644 --- a/packages/core/test/bundling/router/bundle.golden_symbols.json +++ b/packages/core/test/bundling/router/bundle.golden_symbols.json @@ -1436,6 +1436,9 @@ { "name": "isDirectiveHost" }, + { + "name": "isEmptyError" + }, { "name": "isFunction" }, diff --git a/packages/router/src/apply_redirects.ts b/packages/router/src/apply_redirects.ts index 7d920787ead13..bd54a9c0f680b 100644 --- a/packages/router/src/apply_redirects.ts +++ b/packages/router/src/apply_redirects.ts @@ -7,7 +7,7 @@ */ import {EnvironmentInjector, ɵRuntimeError as RuntimeError} from '@angular/core'; -import {EmptyError, from, Observable, of, throwError} from 'rxjs'; +import {from, Observable, of, throwError} from 'rxjs'; import {catchError, concatMap, first, last, map, mergeMap, scan, switchMap, tap} from 'rxjs/operators'; import {RuntimeErrorCode} from './errors'; @@ -21,6 +21,7 @@ import {createRoot, squashSegmentGroup, UrlSegment, UrlSegmentGroup, UrlSerializ import {forEach} from './utils/collection'; import {getOrCreateRouteInjectorIfNeeded, getOutlet, sortByMatchingOutlets} from './utils/config'; import {isImmediateMatch, match, matchWithChecks, noLeftoversInUrl, split} from './utils/config_matching'; +import {isEmptyError} from './utils/type_guards'; const NG_DEV_MODE = typeof ngDevMode === 'undefined' || ngDevMode; @@ -201,7 +202,7 @@ class ApplyRedirects { })); }), first((s): s is UrlSegmentGroup => !!s), catchError((e: any, _: any) => { - if (e instanceof EmptyError || e.name === 'EmptyError') { + if (isEmptyError(e)) { if (noLeftoversInUrl(segmentGroup, segments, outlet)) { return of(new UrlSegmentGroup([], {})); } diff --git a/packages/router/src/operators/resolve_data.ts b/packages/router/src/operators/resolve_data.ts index 550170ffa7d06..5b97b8fdb2824 100644 --- a/packages/router/src/operators/resolve_data.ts +++ b/packages/router/src/operators/resolve_data.ts @@ -7,7 +7,7 @@ */ import {EnvironmentInjector, ProviderToken} from '@angular/core'; -import {EMPTY, EmptyError, from, MonoTypeOperatorFunction, Observable, of, throwError} from 'rxjs'; +import {EMPTY, from, MonoTypeOperatorFunction, Observable, of, throwError} from 'rxjs'; import {catchError, concatMap, first, map, mapTo, mergeMap, takeLast, tap} from 'rxjs/operators'; import {ResolveData, Route} from '../models'; @@ -17,6 +17,7 @@ import {RouteTitleKey} from '../shared'; import {wrapIntoObservable} from '../utils/collection'; import {getClosestRouteInjector} from '../utils/config'; import {getTokenOrFunctionIdentity} from '../utils/preactivation'; +import {isEmptyError} from '../utils/type_guards'; export function resolveData( paramsInheritanceStrategy: 'emptyOnly'|'always', @@ -74,7 +75,7 @@ function resolveNode( }))), takeLast(1), mapTo(data), - catchError((e: unknown) => e instanceof EmptyError ? EMPTY : throwError(e)), + catchError((e: unknown) => isEmptyError(e as Error) ? EMPTY : throwError(e)), ); } diff --git a/packages/router/src/recognize.ts b/packages/router/src/recognize.ts index af9729fc07f3e..57af751fd53ed 100644 --- a/packages/router/src/recognize.ts +++ b/packages/router/src/recognize.ts @@ -6,9 +6,9 @@ * found in the LICENSE file at https://angular.io/license */ -import {createEnvironmentInjector, EnvironmentInjector, Type, ɵRuntimeError as RuntimeError} from '@angular/core'; +import {EnvironmentInjector, Type, ɵRuntimeError as RuntimeError} from '@angular/core'; import {EmptyError, from, Observable, Observer, of} from 'rxjs'; -import {catchError, concatMap, defaultIfEmpty, first, last as rxjsLast, map, scan, startWith, switchMap, takeLast, takeWhile} from 'rxjs/operators'; +import {catchError, concatMap, defaultIfEmpty, first, last as rxjsLast, map, scan, switchMap, takeWhile} from 'rxjs/operators'; import {RuntimeErrorCode} from './errors'; import {Data, ResolveData, Route, Routes} from './models'; @@ -16,9 +16,10 @@ import {ActivatedRouteSnapshot, inheritedParamsDataResolve, ParamsInheritanceStr import {PRIMARY_OUTLET} from './shared'; import {UrlSegment, UrlSegmentGroup, UrlSerializer, UrlTree} from './url_tree'; import {last} from './utils/collection'; -import {getOrCreateRouteInjectorIfNeeded, getOutlet, sortByMatchingOutlets} from './utils/config'; +import {getOutlet, sortByMatchingOutlets} from './utils/config'; import {isImmediateMatch, matchWithChecks, noLeftoversInUrl, split} from './utils/config_matching'; import {TreeNode} from './utils/tree'; +import {isEmptyError} from './utils/type_guards'; const NG_DEV_MODE = typeof ngDevMode === 'undefined' || !!ngDevMode; @@ -156,7 +157,7 @@ export class Recognizer { r._injector ?? injector, r, segmentGroup, segments, outlet); }), first((x): x is TreeNode[] => !!x), catchError(e => { - if (e instanceof EmptyError) { + if (isEmptyError(e)) { if (noLeftoversInUrl(segmentGroup, segments, outlet)) { return of([]); } diff --git a/packages/router/src/utils/type_guards.ts b/packages/router/src/utils/type_guards.ts index 11f9062c6c533..0a64b2cd15a65 100644 --- a/packages/router/src/utils/type_guards.ts +++ b/packages/router/src/utils/type_guards.ts @@ -6,6 +6,8 @@ * found in the LICENSE file at https://angular.io/license */ +import {EmptyError} from 'rxjs'; + import {CanActivate, CanActivateChild, CanDeactivate, CanLoad, CanMatch} from '../models'; import {NAVIGATION_CANCELING_ERROR, NavigationCancelingError, RedirectingNavigationCancelingError} from '../navigation_canceling_error'; import {isUrlTree} from '../url_tree'; @@ -59,3 +61,7 @@ export function isRedirectingNavigationCancelingError( export function isNavigationCancelingError(error: unknown): error is NavigationCancelingError { return error && (error as any)[NAVIGATION_CANCELING_ERROR]; } + +export function isEmptyError(e: Error): e is EmptyError { + return e instanceof EmptyError || e?.name === 'EmptyError'; +}