0

I'm looking how to intercept API network errors. In my API I created an error if the password sent by the frontend application isn't the good one. And in my frontend application i'm trying to catch this api return and hide the network error message in the console.

So I want to get rid of this message: Message to catch

I tried to catch the error with an axios

myApi.interceptors.response.use(
  response => response,
  error => {
  /* THE ERROR MESSAGE IS DISPLAYED BEFORE
   * THE INTERCEPTOR CONSOLE.LOG
   */
    console.log(error);
  }
);

I also tried to use a new Promise((resolve, catch)=> {}) but the message error is still displayed before my catch.

Can anyone help me?

K1nggor
  • 1
  • 1
  • Also have a look at [this](https://github.com/bunqCommunity/bunqJSClient/issues/49). – tao Jan 11 '21 at 11:02
  • Thank you for you answer. It's sad it's impossible to catch those errors. So do you know what is the proper way to test if a user as sent the good password to the API? Does it has to be an error in the console or is it bad practice ? – K1nggor Jan 11 '21 at 11:07
  • If you don't want to see an error in the console, all you have to do is setup the backend ***not*** to return an error response. It can return a proper response containing information about the current state, even if the current state is a "failure" from business logic. In that case, the browser won't display an error in the console, because the status of the response would be 200. – tao Jan 11 '21 at 11:13
  • Thanks, I'll do that. Sounds good to me. – K1nggor Jan 11 '21 at 11:14
  • Be warned that goes against best coding practices. Not throwing errors when they should be thrown makes the code harder to work with (at least in principle), especially for new coders who come in contact with your code. Having errors thrown when things go wrong is a very sane principle and most developers rely on it when coding. I'd say it's when you don't actually have it when one realizes how important having it is. – tao Jan 11 '21 at 11:16
  • So I should keep the error? It is good practice to show an error in the console even if this is a behavior I will manage? – K1nggor Jan 11 '21 at 11:21
  • It is best practice not to suppress errors. There's a misconception an app throwing errors in console is unprofessional but that's wrong. An app should show errors as they happen. It gives its users the ability to provide more info when reporting bugs. And really, if a user inputs wrong credentials and the server says: sorry, mate, those don't work, what's the problem with the server error being shown in the console? Now, obviously, you should show a nice error message to the user too and provide them with good UX. The two are somewhat unrelated. – tao Jan 11 '21 at 11:49

0 Answers0