Skip to content

Commit

Permalink
Merge pull request #305 from amosproj/258-display-more-precise-error-…
Browse files Browse the repository at this point in the history
…messages-eg-when-trying-to-log-in-with-not-existing-mail-adress

Improve error messages, add new error message service
  • Loading branch information
annikakrause committed Jul 23, 2022
2 parents 6d5dafe + 8306464 commit 2280433
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public ResponseEntity<String> handleForgotPassword(String email) {

Optional<User> optional = userRepository.findByEmail(email);
if (optional.isPresent() == false) {
return ResponseEntity.status(500).body("\"Internal Server Error.\"");
return ResponseEntity.status(500).body("\"Email not found.\"");
}
User user = optional.get();
String old_password = user.getPassword();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
} from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { Params, Router } from '@angular/router';
import { InformationPopUpComponent } from 'src/app/shared/pop-up/information-pop-up/information-pop-up.component';
import { environment } from 'src/environments/environment';
import { ActivatedRoute } from '@angular/router';
import { ErrorMessageService } from 'src/app/services/error-message-service/error-message.service';

@Component({
selector: 'app-change-password-page',
Expand All @@ -29,7 +29,8 @@ export class ChangePasswordComponent implements OnInit {
public http: HttpClient,
public dialogRef: MatDialog,
public router: Router,
public route: ActivatedRoute
public route: ActivatedRoute,
public errorMessageService: ErrorMessageService,
) {
this.password = new FormControl('', [
Validators.required,
Expand Down Expand Up @@ -95,10 +96,7 @@ export class ChangePasswordComponent implements OnInit {
);
}
} else {
this.openDialog(
'Password change did not succeded!',
'Server response: ' + response.body
);
this.errorMessageService.openCustomErrorDialog('Server response: ' + response.body, 'Password change did not succeded!');
if (isDevMode()) {
console.log(
'Password change did not succeded! Server response: ' +
Expand All @@ -108,26 +106,14 @@ export class ChangePasswordComponent implements OnInit {
}
},
error: (error) => {
this.openDialog(
'Password change did not succeded!',
'Server response: ' + error.error
);
this.errorMessageService.openCustomErrorDialog('Server response: ' + error.error, 'Password change did not succeded!');
if (isDevMode()) {
console.log(error);
}
},
});
}
//opens a PopUp window of class InformationPopUpComponent
openDialog(header: string, text: string) {
console.log('open');
this.dialogRef.open(InformationPopUpComponent, {
data: {
header: header,
text: text,
},
});
}

// handling errors by validators
matchingError(): boolean {
return this.formGroup.errors != null && this.formGroup.errors['noMatch'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,6 @@ export class CreateCredDefComponent
});
} else {
this.isSuccessData = false;
this.openDialog(
'Creation of credential definition not successful!',
'Server response: ' + response.body,
this.isSuccessData
);
}
})
.catch((response) => {
Expand All @@ -252,11 +247,6 @@ export class CreateCredDefComponent
console.log('error');
console.log(response);
}
this.openDialog(
'Error during creation!',
'Server response: ' + response,
this.isSuccessData
);
});
}

Expand Down Expand Up @@ -290,14 +280,4 @@ export class CreateCredDefComponent
}
});
}

openDialog(header: string, text: string, isSuccessData: boolean) {
this.dialogRef.open(InformationPopUpComponent, {
data: {
header: header,
text: text,
isSuccessData: this.isSuccessData
},
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component, isDevMode, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { InformationPopUpComponent } from '../../shared/pop-up/information-pop-up/information-pop-up.component';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { BackendHttpService } from 'src/app/services/backend-http-service/backend-http-service.service';
import { ForgotPasswordPopUpComponent } from 'src/app/shared/pop-up/forgot-password-pop-up/forgot-password-pop-up.component';
Expand Down Expand Up @@ -59,16 +58,6 @@ export class LoginPageComponent implements OnInit {
}
}

//opens a PopUp window of class InformationPopUpComponent
openDialog(header: string, text: string) {
this.dialogRef.open(InformationPopUpComponent, {
data: {
header: header,
text: text,
},
});
}

openForgotPassword() {
this.dialogRef.open(ForgotPasswordPopUpComponent, {});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { InformationPopUpComponent } from 'src/app/shared/pop-up/information-pop-up/information-pop-up.component';
import { BackendHttpService } from 'src/app/services/backend-http-service/backend-http-service.service';

export interface attribute {
Expand Down Expand Up @@ -282,14 +281,4 @@ export class CreateSchemaPageComponent implements OnInit {

return params;
}

//opens a PopUp window of class InformationPopUpComponent
openDialog(header: string, text: string) {
this.dialogRef.open(InformationPopUpComponent, {
data: {
header: header,
text: text,
},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import {
import { Injectable, isDevMode } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { CookieService } from 'ngx-cookie-service';
import { catchError, Observable, of, timeout } from 'rxjs';
import { InformationPopUpComponent } from 'src/app/shared/pop-up/information-pop-up/information-pop-up.component';
import { environment } from 'src/environments/environment';
import { lastValueFrom } from 'rxjs';
import { ErrorMessageService } from '../error-message-service/error-message.service';
@Injectable({
providedIn: 'root',
})
Expand All @@ -23,8 +22,7 @@ export class BackendHttpService {
constructor(
private http: HttpClient,
private router: Router,
private dialogRef: MatDialog,
private cookieService: CookieService
private errorMessageService: ErrorMessageService,
) {}

async postRequest(
Expand Down Expand Up @@ -66,13 +64,7 @@ export class BackendHttpService {
);
}
const error = <HttpErrorResponse>(<any>response);

this.dialogRef.open(InformationPopUpComponent, {
data: {
header: 'Process failed',
text: 'Error ' + error.status + ' \n' + error.error,
},
});
this.errorMessageService.openHttpErrorDialog(error);
reject(response);
}
},
Expand All @@ -86,12 +78,7 @@ export class BackendHttpService {
}
}

this.dialogRef.open(InformationPopUpComponent, {
data: {
header: 'Process failed',
text: 'Error ' + error.status + ' \n' + error.error,
},
});
this.errorMessageService.openHttpErrorDialog(error);
reject(error);
},
})
Expand Down Expand Up @@ -132,12 +119,7 @@ export class BackendHttpService {
}
const error = <HttpErrorResponse>(<any>response);

this.dialogRef.open(InformationPopUpComponent, {
data: {
header: 'Process failed',
text: 'Error ' + response.status + ' \n' + error.error,
},
});
this.errorMessageService.openHttpErrorDialog(error);
reject(response);
}
},
Expand All @@ -151,12 +133,7 @@ export class BackendHttpService {
}
}

this.dialogRef.open(InformationPopUpComponent, {
data: {
header: 'Process failed',
text: 'Error ' + error.status + ' \n' + error.error,
},
});
this.errorMessageService.openHttpErrorDialog(error);
reject(error);
},
})
Expand Down Expand Up @@ -184,13 +161,13 @@ export class BackendHttpService {
return callback && callback();
}
},
error: () => {
error: (error) => {
this.authenticated = false;
this.dialogRef.open(InformationPopUpComponent, {
data: {
header: 'Login not successful!',
},
});
if (error.status == 401) {
this.errorMessageService.openCustomErrorDialog("Email or password incorrect.", "Login not successful!");
} else {
this.errorMessageService.openHttpErrorDialog(error);
}
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { ErrorMessageService } from './error-message.service';

describe('ErrorMessageService', () => {
let service: ErrorMessageService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ErrorMessageService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { InformationPopUpComponent } from 'src/app/shared/pop-up/information-pop-up/information-pop-up.component';

@Injectable({
providedIn: 'root',
})
export class ErrorMessageService {
private dialogOpen = false;

constructor(private matDialog: MatDialog) {}

openHttpErrorDialog(error: HttpErrorResponse) {
let data = {
header: 'Process failed!',
text: 'Error ' + error.status + ' \n' + error.error,
};

if (error.status == 401) {
data = {
header: 'Login session expired (401)',
text: 'Please log in to your account',
};
}

if (error.status >= 500) {
data = {
header: 'Internal Error (' + error.status + ')',
text:
"The server isn't configured correctly, is unavailable at the moment or a connected service doesn't work properly. This error message could help:\n\n\n" +
error.message +
'\n\n' +
error.error,
};
if (typeof error.error === 'string' || error.error instanceof String) {
if (error.error.length < 55) {
data.header = String(error.error);
data.text = '';
}
}

if (data.text.includes('Error occurred while trying to proxy:')) {
data.header = 'Backend is not reachable.';
data.text =
'Please contact your server administrator!\nIs the backend running? Is the backend reachable? Are the proxy settings correct?';
}
}

if (error.status == undefined) {
data = {
header: 'Internal Error',
text: "The server isn't configured correctly or is unavailable at the moment.",
};
}

this.openDialog(data);
}

openCustomErrorDialog(text = ``, header = `Process failed!`) {
this.openDialog({
header: header,
text: text,
});
}

private openDialog(data: any) {
if (this.dialogOpen) {
console.log(
'Another message should be displayed, but is rejected because another error message is still open. It will be displayed here: '
);
console.log(data);
return;
}

this.dialogOpen = true;
this.matDialog.open(InformationPopUpComponent, {
data: data,
});
this.matDialog.afterAllClosed.subscribe(() => {
this.dialogOpen = false;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export class EditWindowPopUpComponent implements OnInit {
}

init() {
if (this.id == undefined) {
return;
}
const params = new HttpParams().append('id', Number(this.id));
this.HttpService.getRequest(
'Load DI data',
Expand Down

0 comments on commit 2280433

Please sign in to comment.