feat: integrate ngx-toastr in ErrorHandlingService to display error messages in toast notifications.

This commit is contained in:
Chneemann 2025-03-26 13:14:34 +01:00
parent 5422835c06
commit 76594ca28f
3 changed files with 26 additions and 17 deletions

View file

@ -46,7 +46,6 @@ export class LoginComponent {
private tokenService: TokenService,
public sharedService: SharedService,
private overlayService: OverlayService,
private errorHandlingService: ErrorHandlingService,
private route: ActivatedRoute,
private router: Router
) {}
@ -81,8 +80,6 @@ export class LoginComponent {
this.sharedService.isBtnDisabled = false;
} catch (error) {
this.sharedService.isBtnDisabled = false;
const errorMessage = this.errorHandlingService.handleHttpError(error);
alert(errorMessage);
}
}
}

View file

@ -45,7 +45,7 @@ export class AuthService {
this.showLoginSuccessMessage();
} catch (error) {
console.error('Login failed:', error);
throw new Error(this.errorHandlingService.handleHttpError(error));
this.errorHandlingService.handleHttpError(error);
}
}
@ -60,7 +60,7 @@ export class AuthService {
this.router.navigate(['/logout']);
} catch (error) {
console.error('Logout failed:', error);
throw new Error(this.errorHandlingService.handleHttpError(error));
this.errorHandlingService.handleHttpError(error);
}
}

View file

@ -1,32 +1,44 @@
import { Injectable } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';
import { ToastrService } from 'ngx-toastr';
@Injectable({
providedIn: 'root',
})
export class ErrorHandlingService {
handleHttpError(error: unknown): string {
constructor(private toastr: ToastrService) {}
handleHttpError(error: unknown): void {
let errorMessage = 'An unknown error occurred.';
if (error instanceof HttpErrorResponse) {
switch (error.status) {
case 0:
return 'Network issue: Unable to reach the server.\nPlease check your internet connection or the server status.';
errorMessage =
'Network issue: Unable to reach the server. Please check your internet connection or the server status.';
break;
case 401:
return 'Invalid login credentials.\nPlease check your email and password.';
errorMessage =
'Invalid login credentials. Please check your email and password.';
break;
case 403:
return 'You do not have permission to access this resource.';
errorMessage = 'You do not have permission to access this resource.';
break;
case 404:
return 'The requested resource was not found.';
errorMessage = 'The requested resource was not found.';
break;
case 500:
return 'Server error: Please try again later.';
errorMessage = 'Server error: Please try again later.';
break;
default:
return `Unknown error: ${error.statusText || error.message}`;
errorMessage = `Unknown error: ${error.statusText || error.message}`;
break;
}
} else if (error instanceof Error) {
errorMessage = `${error.message || error.toString()}`;
}
if (error instanceof Error) {
return `${error.message || error.toString()}`;
}
return 'An unknown error occurred.';
// Show the toast message with the error message
this.toastr.error(errorMessage, 'Error');
}
}