File

projects/prestations-ng/src/gesdem/gesdem-error-handler.service.ts

Index

Methods

Constructor

constructor(growlService: GrowlBrokerService, router: Router, iamExpiredInterceptorService: IamExpiredInterceptorService)
Parameters :
Name Type Optional
growlService GrowlBrokerService No
router Router No
iamExpiredInterceptorService IamExpiredInterceptorService No

Methods

handleGesdemError
handleGesdemError(prefix: string, reference: string, reCaptchaByPassUUID: string, response: HttpErrorResponse | any)
Parameters :
Name Type Optional
prefix string No
reference string No
reCaptchaByPassUUID string No
response HttpErrorResponse | any No
Returns : Observable<never>
navigateTo
navigateTo(url: string)
Parameters :
Name Type Optional
url string No
Returns : void
Private onOtherError
onOtherError(prefix: string, reference: string, errorCode: string, reCaptchaByPassUUID: string)
Parameters :
Name Type Optional
prefix string No
reference string No
errorCode string No
reCaptchaByPassUUID string No
Returns : Observable<never>
Private onPdfGenerationError
onPdfGenerationError(message: string)
Parameters :
Name Type Optional
message string No
Returns : Observable<never>
import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Params, Router } from '@angular/router';
import { Observable, switchMap, throwError } from 'rxjs';

import { GrowlBrokerService } from '../foehn-growl/growl-broker.service';
import { GrowlType } from '../foehn-growl/growl-types';
import { IamExpiredInterceptorService } from '../sdk-redirect/iam-expired-interceptor.service';

export const SESSION_EXPIRED = 'SESSION_EXPIRED';
export const DEMANDE_SHOULD_EXIST = 'DEMANDE_SHOULD_EXIST';
export const WRONG_ETAPE_REQUESTED = 'WRONG_ETAPE_REQUESTED';
export const DEMANDE_NOT_FOUND = 'DEMANDE_NOT_FOUND';

@Injectable({
    providedIn: 'root'
})
export class GesdemErrorHandlerService {
    constructor(
        private growlService: GrowlBrokerService,
        private router: Router,
        private iamExpiredInterceptorService: IamExpiredInterceptorService
    ) {}

    handleGesdemError(
        prefix: string,
        reference: string,
        reCaptchaByPassUUID: string,
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        response: HttpErrorResponse | any
    ): Observable<never> {
        const isPdfGenerationError =
            response.status === 500 &&
            response.error &&
            response.error.code === 'PDF_GENERATION_ERROR';

        if (isPdfGenerationError) {
            return this.onPdfGenerationError(response.error.message);
        }

        const isSessionExpired =
            response.status === 401 &&
            response.error &&
            response.error.code === 'Unauthorized';

        const errorCode = isSessionExpired
            ? SESSION_EXPIRED
            : response.error
              ? response.error.code
              : null;

        return this.iamExpiredInterceptorService.isIamSessionExpired.pipe(
            switchMap(isIamSessionExpired => {
                if (isIamSessionExpired) {
                    // iamExpiredInterceptorService will display the modal (RedirectComponent)
                    // here we just avoid being redirected to the error page
                    return throwError(() => true);
                } else {
                    return this.onOtherError(
                        prefix,
                        reference,
                        errorCode,
                        reCaptchaByPassUUID
                    );
                }
            })
        );
    }

    navigateTo(url: string): void {
        window.location.assign(url);
    }

    private onPdfGenerationError(message: string): Observable<never> {
        this.growlService.addWithType(GrowlType.DANGER, message);
        return throwError(() => true);
    }

    private onOtherError(
        prefix: string,
        reference: string,
        errorCode: string,
        reCaptchaByPassUUID: string
    ): Observable<never> {
        // redirect to error page while preserving reference in URL
        const commands: string[] = [];
        if (prefix) {
            commands.push(prefix);
        }
        if (reference) {
            commands.push(reference);
        }
        commands.push('erreur');

        const params: Params = {};
        if (errorCode) {
            params.errorCode = errorCode;
        }
        if (!!reCaptchaByPassUUID) {
            params.reCaptchaByPassUUID = reCaptchaByPassUUID;
        }

        this.router.navigate(commands, {
            replaceUrl: true,
            queryParams: params
        });

        return throwError(() => true);
    }
}

results matching ""

    No results matching ""