74053: redirectOn404Or401

This commit is contained in:
Kristof De Langhe
2020-10-27 11:25:12 +01:00
parent d4bd128b67
commit 736704aae8
8 changed files with 36 additions and 24 deletions

View File

@@ -171,16 +171,20 @@ export const getAllSucceededRemoteListPayload = () =>
);
/**
* Operator that checks if a remote data object contains a page not found error
* When it does contain such an error, it will redirect the user to a page not found, without altering the current URL
* Operator that checks if a remote data object returned a 401 or 404 error
* When it does contain such an error, it will redirect the user to the related error page, without altering the current URL
* @param router The router used to navigate to a new page
*/
export const redirectToPageNotFoundOn404 = (router: Router) =>
export const redirectOn404Or401 = (router: Router) =>
<T>(source: Observable<RemoteData<T>>): Observable<RemoteData<T>> =>
source.pipe(
tap((rd: RemoteData<T>) => {
if (rd.hasFailed && rd.error.statusCode === 404) {
router.navigateByUrl('/404', { skipLocationChange: true });
if (rd.hasFailed) {
if (rd.error.statusCode === 404) {
router.navigateByUrl('/404', {skipLocationChange: true});
} else if (rd.error.statusCode === 401) {
router.navigateByUrl('/401', {skipLocationChange: true});
}
}
}));