import { Injectable } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class ErrorHandlerService {

  public handleError(error: HttpErrorResponse): void {
    if (error.error instanceof ErrorEvent) {
      // Client-side or network error
      //console.error('An error occurred:', error.error.message);
    } else {
      // Backend returned an unsuccessful response code
      //console.error(`Backend returned code ${error.status}, body was: `, error.error);
      this.handleServerError(error);
    }
  }

  private handleServerError(error: HttpErrorResponse): void {
    switch (error.status) {
      case 401:
        alert(error.message);
        break;
      case 403:
        alert(error.error.message);
        break;
      case 404:
        alert(error.message);
        break;
      case 500:
        alert(error.message);
        break;
      // Add more cases as needed
      default:
        alert(error.message);
        break;
    }
  }
}