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

Add error page for invalid urls #1085

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion ui/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NgModule } from '@angular/core'
import { RouterModule, Routes } from '@angular/router'
import { navbarRoute } from './layout/navbar/navbar.route'
import { errorRoutes } from './error/error.route'

const routes: Routes = [
{
Expand All @@ -14,7 +15,7 @@ const routes: Routes = [
]

@NgModule({
imports: [RouterModule.forRoot([...routes, navbarRoute])],
imports: [RouterModule.forRoot([...routes, ...errorRoutes, navbarRoute])],
exports: [RouterModule],
})
export class AppRoutingModule {}
5 changes: 3 additions & 2 deletions ui/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import { HomeModule } from './home/home.module'
import { FooterComponent } from './layout/footer/footer.component'
import { SharedModule } from './shared/shared.module'
import { HeaderInterceptor } from './shared/interceptor/header.interceptor'
import { ErrorService } from './shared/service/error.service'
import { ErrorService } from './error/service/error.service';
import { ErrorComponent } from './error/error.component'

@NgModule({
declarations: [AppComponent, NavbarComponent, HasAnyAuthorityDirective, FooterComponent],
declarations: [AppComponent, NavbarComponent, HasAnyAuthorityDirective, FooterComponent, ErrorComponent],
imports: [
FontAwesomeModule,
AccountModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'

import { ErrorAlertComponent } from './error-alert.component'
import { ErrorService } from '../service/error.service'
import { ErrorService } from './service/error.service'
import { AppModule } from 'src/app/app.module'

describe('ErrorAlertComponent', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { ChangeDetectorRef, Component, ErrorHandler, HostListener, Inject, OnInit } from '@angular/core'
import { ErrorService } from '../service/error.service'
import { ErrorService } from './service/error.service'
import { Subscription } from 'rxjs'
import { ErrorAlert } from '../model/error-alert'
import { AppError } from '../model/error.model'
import { AppError, ErrorAlert } from './model/error.model'

@Component({
selector: 'app-error-alert',
templateUrl: './error-alert.component.html',
styleUrls: ['./error-alert.component.scss'],
templateUrl: './error-alert.component.html'
})
export class ErrorAlertComponent implements OnInit {
alerts: any[] = []
Expand All @@ -20,7 +18,6 @@ export class ErrorAlertComponent implements OnInit {

ngOnInit(): void {
this.sub = this.errorService.on().subscribe((err: AppError) => {
const alerts = [...this.alerts]
const alert: ErrorAlert = {
type: 'danger',
msg: err.message,
Expand Down
15 changes: 15 additions & 0 deletions ui/src/app/error/error.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class="container">
<div class="row">
<div class="col-md-12 mt-4">
<h1 i18n="@@error.title.string" class="mb-3.string">Your request cannot be processed</h1>

<div [hidden]="!errorMessage">
<div class="alert alert-danger">{{ errorMessage }}</div>
</div>
<div [hidden]="!error403" class="alert alert-danger" i18n="@@error.http.403.string">
You are not authorized to access this page.
</div>
<div [hidden]="!error404" class="alert alert-danger" i18n="@@error.http.404.string">The page does not exist.</div>
</div>
</div>
</div>
23 changes: 23 additions & 0 deletions ui/src/app/error/error.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { ErrorComponent } from './error.component';
import { AppModule } from '../app.module';

describe('ErrorComponent', () => {
let component: ErrorComponent;
let fixture: ComponentFixture<ErrorComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [AppModule],
declarations: [ErrorComponent]
});
fixture = TestBed.createComponent(ErrorComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
29 changes: 29 additions & 0 deletions ui/src/app/error/error.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AccountService } from '../account';

@Component({
selector: 'app-error',
templateUrl: './error.component.html'
})
export class ErrorComponent implements OnInit {
errorMessage: string | undefined;
error403: boolean | undefined;
error404: boolean | undefined;

constructor(private route: ActivatedRoute, private accountService: AccountService) {}

ngOnInit() {
this.route.data.subscribe(routeData => {
if (routeData['error403']) {
this.error403 = routeData['error403'];
}
if (routeData['error404']) {
this.error404 = routeData['error404'];
}
if (routeData['errorMessage']) {
this.errorMessage = routeData['errorMessage'];
}
});
}
}
36 changes: 36 additions & 0 deletions ui/src/app/error/error.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Routes } from '@angular/router';

import { ErrorComponent } from './error.component';

export const errorRoutes: Routes = [
{
path: 'error',
component: ErrorComponent,
data: {
authorities: [],
pageTitle: 'error.title.string'
}
},
{
path: 'accessdenied',
component: ErrorComponent,
data: {
authorities: [],
pageTitle: 'error.title.string',
error403: true
}
},
{
path: '404',
component: ErrorComponent,
data: {
authorities: [],
pageTitle: 'error.title.string',
error404: true
}
},
{
path: '**',
redirectTo: '/404'
}
];
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ export class AppError {
public i18nKey: string | null
) {}
}

export class ErrorAlert {
constructor(
public type: 'danger',
public msg: string,
public toast: boolean
) {}
}
Empty file.
9 changes: 0 additions & 9 deletions ui/src/app/shared/model/error-alert.ts

This file was deleted.

2 changes: 1 addition & 1 deletion ui/src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NgModule } from '@angular/core'
import { FindLanguageFromKeyPipe } from './pipe/find-language-from-key'
import { ErrorAlertComponent } from './error/error-alert.component'
import { ErrorAlertComponent } from '../error/error-alert.component'
import { CommonModule } from '@angular/common'
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'

Expand Down
Loading