Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS will be completely phased out by mid-2025. To see alternatives please check here

Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
http-error-handler.service.ts 1.24 KiB
import {Injectable} from '@angular/core';
import {HttpErrorResponse} from "@angular/common/http";
import {NavigationService} from "../../window/sidenav/navigation-model.service";
import {AlertMessageService} from "../alert-message/alert-message.service";

@Injectable()
export class HttpErrorHandlerService {

  constructor (private navigationService: NavigationService,
               private alertMessageService: AlertMessageService) {
  }

  public logoutOnInvalidSessionError(err: any): boolean {
    if (err instanceof HttpErrorResponse) {
      if (err.status === 401) {
        this.navigationService.navigateToLogin();
        this.alertMessageService.error("You have been logged out because of inactivity or missing access permissions.")
        return true;
      }
    }
    return false;
  }

  public handleHttpError(err: any) {
    if (err instanceof HttpErrorResponse) {
      if (this.logoutOnInvalidSessionError(err)) {
        return;
      }
      if (err.status === 0) {
        this.alertMessageService.error("Server is not reachable. Please try again later.");
      } else {
        this.alertMessageService.error(err.error.errorDescription);
      }
    } else {
      this.alertMessageService.error(err.error?.errorDescription);
    }

  }
}