3

Is there a way with the HttpClient to remove error messages from the browser console?

My current code looks like this:

  getStuff(stuffId: string): Observable<Stuff[]> {
    return this.httpClient.get<Stuff[]>(this.stuff() + stuffId + '/stuff/').pipe(
      catchError((err) => {
        console.log(err.status);
        if (err.status === 404) {
          console.log('Not found');
        }
      })
    );
  }

My console.log('Not found') within the if statement is executed, but it still throws the standard error to the console.

My goal: nothing red in the console :)


UPDATE:

The errors are not thrown in Firefox, but in Google Chrome. Why..?

Codehan25
  • 2,704
  • 10
  • 47
  • 94

2 Answers2

4

The standard error you are seeing in console is actually not from code. Its from network, due to API error. The browsers consider and understand standard http response codes. So, whenever any response code other than 2xx is returned by any http request, they consider it an error and show it in red in console. Unfortunately you cannot remove it. That's done by browser.

Samarpan
  • 913
  • 5
  • 12
  • Are you sure about this? There must be a way to ignore the error in the code, so nothing is thrown into the console, or not? – Codehan25 Apr 23 '19 at 12:29
  • No. Only way to do this is respond with 2xx code from API. Just have a look at the network tab. You will see same red highlight there as well for that API which threw a non 2xx code. Though you can filter these errors in console using the filter option at the top of console. – Samarpan Apr 24 '19 at 14:11
  • So there is no way to remove the errors by code? Firefox does not throw these errors, just my console.log('Not found'). They are shown in Chrome and Safari, though.. :( – Codehan25 Apr 24 '19 at 14:21
  • Yep. Chrome and Safari has a different behaviour as I said. – Samarpan Apr 24 '19 at 14:46
1

Callback to catchError has to return an Observable so you can just return for example EMPTY that just completes and emits nothing:

import { EMPTY } from 'rxjs';

...

catchError((err) => {
  console.log(err.status);
  if (err.status === 404) {
    console.log('Not found');
  }
  return EMPTY;
});
martin
  • 93,354
  • 25
  • 191
  • 226
  • The errors are still thrown. I've already tried 'NEVER'. Unfortunately without success.. – Codehan25 Apr 23 '19 at 11:27
  • Using `EMPTY` will just ignore the error. Are you sure it's not another error somewhere else where you have `subscribe()` call without any error handler? – martin Apr 23 '19 at 11:32
  • No, I'm pretty sure. I subscribe to the observable in another place, which has already implemented an error handling. – Codehan25 Apr 23 '19 at 12:28