Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Angular navigation errors #24517

Merged
Merged
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
20 changes: 16 additions & 4 deletions generators/angular/templates/src/main/webapp/app/app.config.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
import { ApplicationConfig, LOCALE_ID, importProvidersFrom } from '@angular/core';
import { ApplicationConfig, LOCALE_ID, importProvidersFrom, inject } from '@angular/core';
import { BrowserModule, Title } from '@angular/platform-browser';
import { RouterFeatures, TitleStrategy, provideRouter, withComponentInputBinding, withDebugTracing } from '@angular/router';
import { Router, RouterFeatures, TitleStrategy, provideRouter, withComponentInputBinding, withDebugTracing, withNavigationErrorHandler, NavigationError } from '@angular/router';
import { ServiceWorkerModule } from '@angular/service-worker';
import { HttpClientModule } from '@angular/common/http';

Expand All @@ -29,14 +29,26 @@ import './config/dayjs';
<%_ if (enableTranslation) { _%>
import { TranslationModule } from 'app/shared/language/translation.module';
<%_ } _%>
import { httpInterceptorProviders } from 'app/core/interceptor/index';
import { httpInterceptorProviders } from './core/interceptor';
import FindLanguageFromKeyPipe from 'app/shared/language/find-language-from-key.pipe';
import routes from './app.routes';
// jhipster-needle-angular-add-module-import JHipster will add new module here
import { NgbDateDayjsAdapter } from './config/datepicker-adapter';
import { AppPageTitleStrategy } from './app-page-title-strategy';

const routerFeatures: Array<RouterFeatures> = [withComponentInputBinding()];
const routerFeatures: Array<RouterFeatures> = [withComponentInputBinding(),
withNavigationErrorHandler((e: NavigationError) => {
const router = inject(Router);
if (e.error.status === 403) {
router.navigate(['/accessdenied'])
Copy link
Contributor

@mraible mraible Dec 10, 2023

Choose a reason for hiding this comment

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

IntelliJ doesn't like that there's no await or .then() here, but it works, so I'm OK with not having it.

Screenshot 2023-12-10 at 9 35 30 AM

Copy link
Contributor

Choose a reason for hiding this comment

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

It also says the httpInterceptorProviders import can be shorted to:

import { httpInterceptorProviders } from './core/interceptor';

} else if (e.error.status === 404) {
router.navigate(['/404'])
} else if (e.error.status === 401) {
router.navigate(['/login']);
} else {
router.navigate(['/error']);
}
})];
if (DEBUG_INFO_ENABLED) {
routerFeatures.push(withDebugTracing());
}
Expand Down
Loading