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

import {AbstractErrorHandlerInterceptor, HttpErrorDetails, Logger, NotificationService} from 'm2-core';


@Injectable({
    providedIn: 'root'
})
export class CdErrorHandlerInterceptor extends AbstractErrorHandlerInterceptor {

    constructor(
        private logger: Logger,
        private notificationService: NotificationService,
    ) {
        super();
    }

    protected handleErrorDetails(error: HttpErrorResponse, errorDetails: HttpErrorDetails): void {
        this.logger.error(`CD_ERROR_HANDLER_INTERCEPTOR: error occurred requesting backend, statusCode = ${errorDetails.statusCode}`, errorDetails.errorMsg, error);
        this.notificationService.error(errorDetails.errorMsg);
    }

}
