import { HttpResponseBase , HttpResponse , HttpErrorResponse } from '@angular/common/http' ; import { Observable } from 'rxjs' ; export interface IResponseMessageHandler { getSuccessMessage ( apiDesc , response ) : string ; getErrorMessage ( apiDesc , response ) : string ; } export class GetResponseMessageHandler implements IResponseMessageHandler { public getSuccessMessage ( apiDesc , response ) { return null ; } public getErrorMessage ( apiDesc , response ) { if ( response . status === 404 ) { return null ; } return this . getErrorMessageInternal ( apiDesc , response ) ; } protected getErrorMessageInternal ( apiDesc , response ) { let message = `${ apiDesc } failed` ; if ( response . error ?. Error ?. Code && response . error ?. Error ?. Message ) { message += `.\r\nCode: ${ response . error . Error . Code }\r\nMessage: ${ response . error . Error . Message }` ; } else if ( response . statusText ) { message += ` (${ response . statusText })` ; } return message ; } } export class PostResponseMessageHandler extends GetResponseMessageHandler { public getSuccessMessage ( apiDesc , response ) { return `${ apiDesc } started.` ; } public getErrorMessage ( apiDesc , response ) { return this . getErrorMessageInternal ( apiDesc , response ) ; } } export class PutResponseMessageHandler extends GetResponseMessageHandler { public getSuccessMessage ( apiDesc , response ) { return `${ apiDesc }` ; } public getErrorMessage ( apiDesc , response ) { return this . getErrorMessageInternal ( apiDesc , response ) ; } } export class DeleteResponseMessageHandler extends GetResponseMessageHandler { public getSuccessMessage ( apiDesc , response ) { return `${ apiDesc }` ; } public getErrorMessage ( apiDesc , response ) { return this . getErrorMessageInternal ( apiDesc , response ) ; } } export class SilentResponseMessageHandler implements IResponseMessageHandler { public getSuccessMessage ( apiDesc , response ) { return null ; } public getErrorMessage ( apiDesc , response ) { return null ; } } export class ResponseMessageHandlers { public static getResponseMessageHandler : IResponseMessageHandler = new GetResponseMessageHandler ( ) ; public static postResponseMessageHandler : IResponseMessageHandler = new PostResponseMessageHandler ( ) ; public static putResponseMessageHandler : IResponseMessageHandler = new PostResponseMessageHandler ( ) ; public static silentResponseMessageHandler : IResponseMessageHandler = new SilentResponseMessageHandler ( ) ; public static deleteResponseMessageHandler : IResponseMessageHandler = new DeleteResponseMessageHandler ( ) ; } export class EventsStoreResponseMessageHandler implements IResponseMessageHandler { private innerHandler ? : IResponseMessageHandler ; public constructor ( innerHandler ? ) { this . innerHandler = innerHandler ; } public getSuccessMessage ( apiDesc , response ) { const handler = this . innerHandler ? this . innerHandler : ResponseMessageHandlers . getResponseMessageHandler ; return handler . getSuccessMessage ( apiDesc , response ) ; } public getErrorMessage ( apiDesc , response ) { if ( response . status === 400 ) { return 'Events API is not available on current cluster.' ; } if ( response . status === 404 && response . statusText === 'FABRIC_E_SERVICE_DOES_NOT_EXIST' ) { return 'EventStore system service is not found, please enable it.' ; } if ( response . status === 404 || response . status === 503 ) { return 'Events storage is not configured for current cluster.' ; } const handler = this . innerHandler ? this . innerHandler : ResponseMessageHandlers . getResponseMessageHandler ; return handler . getErrorMessage ( apiDesc , response ) ; } }