1

What I'm doing is to log in an user, and if the credentials are invalid I throw an exception from the server side. My code is the following:

this.http.post("/login", JSON.stringify(credentials))
  .map(result => result.json())
  .subscribe(
    data =>this.router.navigate(["home"]),
    () => this.router.navigate(["login"], {queryParams: {error: true}})
  );

In this case I'm authenticating the user if it doesn't throw an error, and change the route when it does. The problem is that even when I catch the error, the chrome console is still showing like it threw an error.

Is there anyway to avoid that? I don't want the user to see that. For me is not an error because I'm doing something with it, and I want to be able to use the message that I get from the server side.

Motomine
  • 4,205
  • 5
  • 20
  • 23
  • how do you catch it? and what produces the error? – Max Koretskyi Jul 01 '17 at 03:58
  • On the server side I set the response status to 400 and the message to the one that I want to show (this is not shown on the example). I catch it with the second parameter of the subscribe, which is triggered when the response status is not 200 – Motomine Jul 01 '17 at 04:00
  • it's a Chrome standard behavior with XHR, not Angular or observables. You cannot disable it – Max Koretskyi Jul 01 '17 at 04:16
  • Anda so you know if there is anyway to do something like that? It might be another way rathe than asking if it was successful with ifs. – Motomine Jul 01 '17 at 04:18
  • no, you can't do anything about it. If a server returns error status code, the chrome will log it even before your code is executed – Max Koretskyi Jul 01 '17 at 04:20
  • I got that. But what I want to know if there is any other good way to do something like that, I mean returning a successful status and have the same behavior as now without redundant ifs. – Motomine Jul 01 '17 at 04:23
  • it's a different and rather a complex question, check [this](https://stackoverflow.com/questions/12806386/standard-json-api-response-format) maybe – Max Koretskyi Jul 01 '17 at 04:29

0 Answers0