diff --git a/src/app/api/http/http.service.ts b/src/app/api/http/http.service.ts index 909f072..9f38d52 100644 --- a/src/app/api/http/http.service.ts +++ b/src/app/api/http/http.service.ts @@ -12,7 +12,7 @@ import {ErrorService} from '../../services/error.service'; export class HttpService { constructor(private httpClient: HttpClient, - protected localStorageService: LocalStorageService, + private localStorageService: LocalStorageService, private injector: Injector) { } @@ -70,6 +70,7 @@ export class HttpService { }, error: err => { errorService.httpError(err); + errorService.checkConnection(); this.subscriptionQueue.splice(this.subscriptionQueue.indexOf(subscription), 1) }, complete: () => this.subscriptionQueue.splice(this.subscriptionQueue.indexOf(subscription), 1) @@ -78,7 +79,7 @@ export class HttpService { return oob; } - private get(request: RequestObj) { + get(request: RequestObj) { return this.httpClient.get(request.url, { headers: request.header, @@ -87,7 +88,7 @@ export class HttpService { }); } - private post(request: RequestObj) { + post(request: RequestObj) { return this.httpClient.post(request.url, request.data, { headers: request.header, @@ -96,7 +97,7 @@ export class HttpService { }); } - private put(request: RequestObj) { + put(request: RequestObj) { return this.httpClient.put(request.url, request.data, { headers: request.header, @@ -105,7 +106,7 @@ export class HttpService { }); } - private delete(request: RequestObj) { + delete(request: RequestObj) { return this.httpClient.delete(request.url, { headers: request.header, diff --git a/src/app/services/error.service.ts b/src/app/services/error.service.ts index 6c52778..b68b35a 100644 --- a/src/app/services/error.service.ts +++ b/src/app/services/error.service.ts @@ -1,23 +1,52 @@ -import {Injectable, Injector} from '@angular/core'; -import {Response} from '../class/HttpReqAndResp'; +import {Injectable} from '@angular/core'; +import {RequestObj, Response} from '../class/HttpReqAndResp'; import {HttpService} from '../api/http/http.service'; +import {environment} from '../../environments/environment'; +import {Router} from '@angular/router'; +import {ComponentStateService} from './component-state.service'; @Injectable({ providedIn: 'root' }) export class ErrorService { - constructor(private httpService: HttpService) { + constructor(private httpService: HttpService, private router: Router, + private componentStateService: ComponentStateService) { } + private static HTTP_ERROR_COUNT: number = 0; + private readonly MAINTAIN_PAGE_PREFIX = '/maintain' + public httpError(err: any) { - console.log('error=>', err) + if (!environment.production) { + console.log('error=>', err) + } + ErrorService.HTTP_ERROR_COUNT++; // this.httpService.getSubscriptionQueue().map(a => a.unsubscribe()) } public httpException(response: Response) { - console.log('exception=>', response) + if (!environment.production) + console.log('exception=>', response) } - + public checkConnection() { + // The HTTP_ERROR_COUNT is start with 1 in this function + if (ErrorService.HTTP_ERROR_COUNT === 1) { + const req: RequestObj = { + path: '/headerInfo', + method: 'GET', + url: environment.host + '/headerInfo' + } + this.httpService.get(req).subscribe({ + next: data => null, + error: err => { + if (this.componentStateService.currentPath !== this.MAINTAIN_PAGE_PREFIX) { + this.router.navigateByUrl(this.MAINTAIN_PAGE_PREFIX) + } + ErrorService.HTTP_ERROR_COUNT = 0; + } + }) + } + } }