feat: integrate ngx-toastr in ErrorHandlingService to display error messages in toast notifications.
This commit is contained in:
parent
5422835c06
commit
76594ca28f
3 changed files with 26 additions and 17 deletions
|
|
@ -46,7 +46,6 @@ export class LoginComponent {
|
||||||
private tokenService: TokenService,
|
private tokenService: TokenService,
|
||||||
public sharedService: SharedService,
|
public sharedService: SharedService,
|
||||||
private overlayService: OverlayService,
|
private overlayService: OverlayService,
|
||||||
private errorHandlingService: ErrorHandlingService,
|
|
||||||
private route: ActivatedRoute,
|
private route: ActivatedRoute,
|
||||||
private router: Router
|
private router: Router
|
||||||
) {}
|
) {}
|
||||||
|
|
@ -81,8 +80,6 @@ export class LoginComponent {
|
||||||
this.sharedService.isBtnDisabled = false;
|
this.sharedService.isBtnDisabled = false;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.sharedService.isBtnDisabled = false;
|
this.sharedService.isBtnDisabled = false;
|
||||||
const errorMessage = this.errorHandlingService.handleHttpError(error);
|
|
||||||
alert(errorMessage);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ export class AuthService {
|
||||||
this.showLoginSuccessMessage();
|
this.showLoginSuccessMessage();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Login failed:', 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']);
|
this.router.navigate(['/logout']);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Logout failed:', error);
|
console.error('Logout failed:', error);
|
||||||
throw new Error(this.errorHandlingService.handleHttpError(error));
|
this.errorHandlingService.handleHttpError(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,44 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { HttpErrorResponse } from '@angular/common/http';
|
import { HttpErrorResponse } from '@angular/common/http';
|
||||||
|
import { ToastrService } from 'ngx-toastr';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
export class ErrorHandlingService {
|
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) {
|
if (error instanceof HttpErrorResponse) {
|
||||||
switch (error.status) {
|
switch (error.status) {
|
||||||
case 0:
|
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:
|
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:
|
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:
|
case 404:
|
||||||
return 'The requested resource was not found.';
|
errorMessage = 'The requested resource was not found.';
|
||||||
|
break;
|
||||||
case 500:
|
case 500:
|
||||||
return 'Server error: Please try again later.';
|
errorMessage = 'Server error: Please try again later.';
|
||||||
|
break;
|
||||||
default:
|
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) {
|
// Show the toast message with the error message
|
||||||
return `${error.message || error.toString()}`;
|
this.toastr.error(errorMessage, 'Error');
|
||||||
}
|
|
||||||
|
|
||||||
return 'An unknown error occurred.';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue