I am currently coding a script that iterates through an array to make HTTP requests.
Most of these requests are directed towards non-existent resources, but I do not have information on which ones specifically.
Here is the code snippet I am using:
langs.forEach(lang => {
this.http.get('assets/i18n/' + lang + '.json')
.pipe(
tap(data => console.log('server data:', data)),
catchError(this.handleError),
);
});
Despite the code above, I am not seeing any output in the console, although I should be expecting some data structures.
What I do not want to see are error messages like:
GET http://localhost: 4205/assets/i18n/[...] 404 (Not Found)
, rather just the successful outcomes of the calls.I am interested in finding out how to filter out errors and only retrieve valid data structures within a loop.
Thank you for your help.
UPDATE:
The method this.handleError
is sourced from the Angular documentation:
private handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
console.error('An error occurred:', error.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.error(
`Backend returned code ${error.status}, ` +
`body was: ${error.error}`);
}
// return an observable with a user-facing error message
return throwError(
'Something bad happened; please try again later.');
}