In angular documentation they stated that you have to specify observe: "response"
on the provided options to get the full http response, which I did here:
constructor(private service: new () => T,
private http: HttpClient,
private httpErrorHandler: HttpErrorHandlerService,
){
this._handleError = this.httpErrorHandler.createHandleError(this.service.name);
}
private _handleError: HandleError;
//...other code
delete(resourceNameOrId: number | string): Observable<HttpErrorResponse>{
return this.http.delete<T>(this._url + resourceNameOrId, {
observe: "response"
}).pipe(
catchError(this._handleError(`delete`, null))
);
}
The handleError function is defined in this service:
export type HandleError = <T> (operation?: string, result?: T) => (error: HttpErrorResponse) => Observable<T>;
@Injectable({
providedIn: 'root'
})
export class HttpErrorHandlerService {
constructor(private errorService: ErrorService, private modalService: NzModalService) { }
createHandleError = (serviceName = '') => <T> (operation = 'operation', result = {} as T) => this.handleError(serviceName, operation, result);
handleError<T> (serviceName = '', operation = 'operation', result = {} as T ){
return (error: HttpErrorResponse): Observable<T> => {
const message = (error.error instanceof ErrorEvent) ? error.error.message: `{error code: ${error.status}, body: "${error.message}"}`;
this.errorService.errorMessage = `${serviceName} -> ${operation} failed.\n Message: ${message}`;
console.error(this.errorService.errorMessage);
return of(result);
}
}
}
And this is an example of how I call my service delete function in a component:
this.service.delete(nom).subscribe(data => {
if(!this.errorService.errorMessage){
this.notificationService.success("Suppression", "L'enregistrement a été supprimée !");
}
});
But specifying the observe: "response"
didn't work in my case, and the error: HttpErrorResponse
only returns the error message and not the full response :
I have tried solutions in this thread : Angular 4.3.3 HttpClient : How get value from the header of a response? which didn't work for me, and the provided solution was to define the observe option.
How can I solve this ?
Update:
The the observe option only works when the delete http request returns a 200 code as shown in screenshot below, but when there is a 404 response status the response object in this case is null, and in the handleError function the response body is the only thing I can access to.