X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fservices%2FRestService.ts;h=5f47fb19d268cf291de9766e46f19c7a2c75c642;hb=c84f1123f8ba69f9e2211b2d816bb415e595efaa;hp=708d91b43c15b5423cd2838cd000bbe54ed183a8;hpb=1db2e54394d3400d264e07e15e288f46f33eff65;p=osm%2FNG-UI.git diff --git a/src/services/RestService.ts b/src/services/RestService.ts index 708d91b..5f47fb1 100644 --- a/src/services/RestService.ts +++ b/src/services/RestService.ts @@ -28,7 +28,6 @@ import { NotifierService } from 'angular-notifier'; import { APIURLHEADER, ERRORDATA } from 'CommonModel'; import * as HttpStatus from 'http-status-codes'; import { Observable } from 'rxjs'; -import { isNullOrUndefined } from 'util'; /** * An Injectable is a class adorned with the @Injectable decorator function. @@ -41,6 +40,9 @@ export class RestService { private http: HttpClient; /** API URL. Disabled tslint since server doesn't support https protocol */ private apiURL: string = ''; + /** Method to handle null or undefined @private */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + private isNullOrUndefined = (data: any): boolean => data === null || data === undefined; /** Notifier service to popup notification @private */ private notifierService: NotifierService; /** Instance for active modal service @public */ @@ -62,7 +64,7 @@ export class RestService { public getResource(apiURL: string, httpHeaders?: { headers: HttpHeaders }): Observable<{}> { const getRequest: APIURLHEADER = this.getHttpoptions(apiURL); - if (!isNullOrUndefined(httpHeaders)) { + if (!this.isNullOrUndefined(httpHeaders)) { return this.http.get(apiURL, httpHeaders); } else { return this.http.get(getRequest.url, getRequest.httpOptions); @@ -77,7 +79,7 @@ export class RestService { public postResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable<{}> { const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url); - if (!isNullOrUndefined(apiURLHeader.httpOptions)) { + if (!this.isNullOrUndefined(apiURLHeader.httpOptions)) { return this.http.post(apiURLHeader.url, payload, apiURLHeader.httpOptions); } else { return this.http.post(getRequest.url, payload, getRequest.httpOptions); @@ -92,7 +94,7 @@ export class RestService { public patchResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable { const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url); - if (!isNullOrUndefined(apiURLHeader.httpOptions)) { + if (!this.isNullOrUndefined(apiURLHeader.httpOptions)) { return this.http.patch(apiURLHeader.url, payload, apiURLHeader.httpOptions); } else { return this.http.patch(getRequest.url, payload, getRequest.httpOptions); @@ -107,7 +109,7 @@ export class RestService { public putResource(apiURLHeader: { url: string, httpOptions?: { headers: HttpHeaders } }, payload: {}): Observable { const getRequest: APIURLHEADER = this.getHttpoptions(apiURLHeader.url); - if (!isNullOrUndefined(apiURLHeader.httpOptions)) { + if (!this.isNullOrUndefined(apiURLHeader.httpOptions)) { return this.http.put(apiURLHeader.url, payload, apiURLHeader.httpOptions); } else { return this.http.put(getRequest.url, payload, getRequest.httpOptions); @@ -121,7 +123,7 @@ export class RestService { public deleteResource(apiURL: string, httpHeaders?: { headers: HttpHeaders }): Observable { const getRequest: APIURLHEADER = this.getHttpoptions(apiURL); - if (!isNullOrUndefined(httpHeaders)) { + if (!this.isNullOrUndefined(httpHeaders)) { return this.http.delete(apiURL, httpHeaders); } else { return this.http.delete(getRequest.url, getRequest.httpOptions); @@ -133,7 +135,6 @@ export class RestService { * @param error The error response reecieved from API call. * @param method The http request method. */ - // tslint:disable-next-line: cyclomatic-complexity public handleError(err: ERRORDATA, method?: string): void { if (err.error.status === HttpStatus.UNAUTHORIZED) { if (method !== 'get') { @@ -142,8 +143,11 @@ export class RestService { this.notifierService.notify('error', err.error.detail !== undefined ? err.error.detail : this.translateService.instant('HTTPERROR.401')); } - this.activeModal.dismissAll(); + } else { + this.notifierService.notify('error', err?.error?.detail !== undefined ? + err?.error?.detail : this.translateService.instant('HTTPERROR.401')); } + this.activeModal.dismissAll(); } else if (err.error.status === HttpStatus.BAD_REQUEST) { this.notifierService.notify('error', err.error.detail !== undefined ? err.error.detail : this.translateService.instant('HTTPERROR.400')); @@ -178,7 +182,7 @@ export class RestService { }); return { url: apiURL, - httpOptions: {headers : apiHeaders} + httpOptions: { headers: apiHeaders } }; } }