diff --git a/src/app/core/dspace-rest/dspace-rest.service.ts b/src/app/core/dspace-rest/dspace-rest.service.ts index bd9da4cf294..b27527cba6c 100644 --- a/src/app/core/dspace-rest/dspace-rest.service.ts +++ b/src/app/core/dspace-rest/dspace-rest.service.ts @@ -3,6 +3,7 @@ import { HttpErrorResponse, HttpHeaders, HttpParams, + HttpResponse, } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { @@ -16,6 +17,7 @@ import { import { hasNoValue, + hasValue, isNotEmpty, } from '../../shared/empty.util'; import { RequestError } from '../data/request-error.model'; @@ -53,7 +55,21 @@ export class DspaceRestService { * An Observable containing the response from the server */ get(absoluteURL: string): Observable { - return this.request(RestRequestMethod.GET, absoluteURL); + const requestOptions = { + observe: 'response' as any, + headers: new HttpHeaders({ 'Content-Type': DEFAULT_CONTENT_TYPE }), + }; + return this.http.get(absoluteURL, requestOptions).pipe( + map((res: HttpResponse) => ({ + payload: res.body, + statusCode: res.status, + statusText: res.statusText, + })), + catchError((err: unknown) => observableThrowError(() => { + console.log('Error: ', err); + return this.handleHttpError(err); + })), + ); } /** @@ -110,20 +126,7 @@ export class DspaceRestService { statusText: res.statusText, })), catchError((err: unknown) => observableThrowError(() => { - console.log('Error: ', err); - if (err instanceof HttpErrorResponse) { - const error = new RequestError( - (isNotEmpty(err?.error?.message)) ? err.error.message : err.message, - ); - - error.statusCode = err.status; - error.statusText = err.statusText; - - return error; - } else { - console.error('Cannot construct RequestError from', err); - return err; - } + return this.handleHttpError(err); })), ); } @@ -149,4 +152,20 @@ export class DspaceRestService { return form; } + protected handleHttpError(err: unknown): RequestError | unknown { + if (err instanceof HttpErrorResponse) { + const error = new RequestError( + (isNotEmpty(err?.error?.message)) ? err.error.message : err.message, + ); + + error.statusCode = err.status; + error.statusText = err.statusText; + + return error; + } else { + console.error('Cannot construct RequestError from', err); + return err; + } + } + }