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 0f2e776
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 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 All @@ -16,6 +17,7 @@ import {

import {
hasNoValue,
hasValue,

Check failure on line 20 in src/app/core/dspace-rest/dspace-rest.service.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

'hasValue' is defined but never used

Check failure on line 20 in src/app/core/dspace-rest/dspace-rest.service.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

'hasValue' is defined but never used
isNotEmpty,
} from '../../shared/empty.util';
import { RequestError } from '../data/request-error.model';
Expand Down Expand Up @@ -53,7 +55,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 +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);
})),
);
}
Expand All @@ -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;
}
}

}

0 comments on commit 0f2e776

Please sign in to comment.