Skip to content

Commit

Permalink
Only log errors for DspaceRestService.get
Browse files Browse the repository at this point in the history
When refactoring to meet the rxjs/no-implicit-any-catch ESLint rule, I'd made it so errors were logged for DspaceRestService.request calls as well, making it a lot more noisy
The console.log in .get is explicitly required by tests though
  • Loading branch information
ybnd committed Mar 8, 2024
1 parent 86885f7 commit 33d5253
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/app/core/dspace-rest/dspace-rest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
HttpErrorResponse,
HttpHeaders,
HttpParams,
HttpResponse,
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import {
Expand Down Expand Up @@ -53,7 +54,21 @@ export class DspaceRestService {
* An Observable<string> containing the response from the server
*/
get(absoluteURL: string): Observable<RawRestResponse> {
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<any>) => ({
payload: res.body,
statusCode: res.status,
statusText: res.statusText,
})),
catchError((err: unknown) => observableThrowError(() => {
console.log('Error: ', err);
return this.handleHttpError(err);
})),
);
}

/**
Expand Down Expand Up @@ -110,20 +125,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);
})),
);
}
Expand All @@ -149,4 +151,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;
}
}

}

0 comments on commit 33d5253

Please sign in to comment.