mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
refactor(router): Remove custom error message for invalid guards (#46678)
This custom error message for invalid guards requires a fair bit of extra logic in several places. This change reduces special logic for an error case that would fail anyways. Additionally, there were already a couple places that _did not_ have this special error and we have not seen any indication that there is more confusion for those (resolvers and canMatch). PR Close #46678
This commit is contained in:
committed by
Andrew Scott
parent
db631bebbf
commit
dd3e0968ec
@@ -13,9 +13,7 @@ export const enum RuntimeErrorCode {
|
||||
// (undocumented)
|
||||
INVALID_DOUBLE_DOTS = 4005,
|
||||
// (undocumented)
|
||||
INVALID_GUARD = 4014,
|
||||
// (undocumented)
|
||||
INVALID_ROUTE_CONFIG = 4015,
|
||||
INVALID_ROUTE_CONFIG = 4014,
|
||||
// (undocumented)
|
||||
MISPLACED_OUTLETS_COMMAND = 4004,
|
||||
// (undocumented)
|
||||
|
||||
@@ -24,6 +24,5 @@ export const enum RuntimeErrorCode {
|
||||
UNEXPECTED_VALUE_IN_URL = 4011,
|
||||
OUTLET_NOT_ACTIVATED = 4012,
|
||||
OUTLET_ALREADY_ACTIVATED = 4013,
|
||||
INVALID_GUARD = 4014,
|
||||
INVALID_ROUTE_CONFIG = 4015,
|
||||
INVALID_ROUTE_CONFIG = 4014,
|
||||
}
|
||||
|
||||
@@ -6,13 +6,12 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {EnvironmentInjector, Injector, ɵRuntimeError as RuntimeError} from '@angular/core';
|
||||
import {EnvironmentInjector, Injector} from '@angular/core';
|
||||
import {concat, defer, from, MonoTypeOperatorFunction, Observable, of, OperatorFunction, pipe} from 'rxjs';
|
||||
import {concatMap, filter, first, map, mergeMap, tap} from 'rxjs/operators';
|
||||
import {concatMap, first, map, mergeMap, tap} from 'rxjs/operators';
|
||||
|
||||
import {RuntimeErrorCode} from '../errors';
|
||||
import {ActivationStart, ChildActivationStart, Event} from '../events';
|
||||
import {CanActivateChildFn, CanActivateFn, CanDeactivateFn, CanLoadFn, CanMatch, CanMatchFn, Route} from '../models';
|
||||
import {CanLoad, CanLoadFn, CanMatch, CanMatchFn, Route} from '../models';
|
||||
import {NavigationTransition} from '../router';
|
||||
import {ActivatedRouteSnapshot, RouterStateSnapshot} from '../router_state';
|
||||
import {navigationCancelingError, REDIRECTING_CANCELLATION_REASON} from '../shared';
|
||||
@@ -23,8 +22,6 @@ import {isBoolean, isCanActivate, isCanActivateChild, isCanDeactivate, isCanLoad
|
||||
|
||||
import {prioritizedGuardValue} from './prioritized_guard_value';
|
||||
|
||||
const NG_DEV_MODE = typeof ngDevMode === 'undefined' || !!ngDevMode;
|
||||
|
||||
export function checkGuards(moduleInjector: Injector, forwardEvent?: (evt: Event) => void):
|
||||
MonoTypeOperatorFunction<NavigationTransition> {
|
||||
return mergeMap(t => {
|
||||
@@ -117,16 +114,9 @@ function runCanActivate(
|
||||
const canActivateObservables = canActivate.map((c: any) => {
|
||||
return defer(() => {
|
||||
const guard = getToken(c, futureARS, moduleInjector);
|
||||
let observable;
|
||||
if (isCanActivate(guard)) {
|
||||
observable = wrapIntoObservable(guard.canActivate(futureARS, futureRSS));
|
||||
} else if (isFunction<CanActivateFn>(guard)) {
|
||||
observable = wrapIntoObservable(guard(futureARS, futureRSS));
|
||||
} else {
|
||||
throw new RuntimeError(
|
||||
RuntimeErrorCode.INVALID_GUARD, NG_DEV_MODE && 'Invalid CanActivate guard');
|
||||
}
|
||||
return observable.pipe(first());
|
||||
const guardVal = isCanActivate(guard) ? guard.canActivate(futureARS, futureRSS) :
|
||||
guard(futureARS, futureRSS);
|
||||
return wrapIntoObservable(guardVal).pipe(first());
|
||||
});
|
||||
});
|
||||
return of(canActivateObservables).pipe(prioritizedGuardValue());
|
||||
@@ -146,16 +136,9 @@ function runCanActivateChild(
|
||||
return defer(() => {
|
||||
const guardsMapped = d.guards.map((c: any) => {
|
||||
const guard = getToken(c, d.node, moduleInjector);
|
||||
let observable;
|
||||
if (isCanActivateChild(guard)) {
|
||||
observable = wrapIntoObservable(guard.canActivateChild(futureARS, futureRSS));
|
||||
} else if (isFunction<CanActivateChildFn>(guard)) {
|
||||
observable = wrapIntoObservable(guard(futureARS, futureRSS));
|
||||
} else {
|
||||
throw new RuntimeError(
|
||||
RuntimeErrorCode.INVALID_GUARD, NG_DEV_MODE && 'Invalid CanActivateChild guard');
|
||||
}
|
||||
return observable.pipe(first());
|
||||
const guardVal = isCanActivateChild(guard) ? guard.canActivateChild(futureARS, futureRSS) :
|
||||
guard(futureARS, futureRSS);
|
||||
return wrapIntoObservable(guardVal).pipe(first());
|
||||
});
|
||||
return of(guardsMapped).pipe(prioritizedGuardValue());
|
||||
});
|
||||
@@ -170,16 +153,10 @@ function runCanDeactivate(
|
||||
if (!canDeactivate || canDeactivate.length === 0) return of(true);
|
||||
const canDeactivateObservables = canDeactivate.map((c: any) => {
|
||||
const guard = getToken(c, currARS, moduleInjector);
|
||||
let observable;
|
||||
if (isCanDeactivate(guard)) {
|
||||
observable = wrapIntoObservable(guard.canDeactivate(component!, currARS, currRSS, futureRSS));
|
||||
} else if (isFunction<CanDeactivateFn<any>>(guard)) {
|
||||
observable = wrapIntoObservable(guard(component, currARS, currRSS, futureRSS));
|
||||
} else {
|
||||
throw new RuntimeError(
|
||||
RuntimeErrorCode.INVALID_GUARD, NG_DEV_MODE && 'Invalid CanDeactivate guard');
|
||||
}
|
||||
return observable.pipe(first());
|
||||
const guardVal = isCanDeactivate(guard) ?
|
||||
guard.canDeactivate(component!, currARS, currRSS, futureRSS) :
|
||||
guard(component, currARS, currRSS, futureRSS);
|
||||
return wrapIntoObservable(guardVal).pipe(first());
|
||||
});
|
||||
return of(canDeactivateObservables).pipe(prioritizedGuardValue());
|
||||
}
|
||||
@@ -193,16 +170,8 @@ export function runCanLoadGuards(
|
||||
}
|
||||
|
||||
const canLoadObservables = canLoad.map((injectionToken: any) => {
|
||||
const guard = injector.get(injectionToken);
|
||||
let guardVal;
|
||||
if (isCanLoad(guard)) {
|
||||
guardVal = guard.canLoad(route, segments);
|
||||
} else if (isFunction<CanLoadFn>(guard)) {
|
||||
guardVal = guard(route, segments);
|
||||
} else {
|
||||
throw new RuntimeError(
|
||||
RuntimeErrorCode.INVALID_GUARD, NG_DEV_MODE && 'Invalid CanLoad guard');
|
||||
}
|
||||
const guard = injector.get<CanLoad|CanLoadFn>(injectionToken);
|
||||
const guardVal = isCanLoad(guard) ? guard.canLoad(route, segments) : guard(route, segments);
|
||||
return wrapIntoObservable(guardVal);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user