1

I've read few other similar questions, but the information there didn't help me find the answer to my problem. I have trouble trying to pass errors that I manage to catch inside services to components. So this is my onSubmit method inside component, which registers an user:

    onSubmit({ value, valid }) {
      console.log(value, valid);
      if (valid) {
      this.userService.trySaveUser({ email: value.email, password: value.password.pass }).subscribe(
        (user) => {
          console.log('saved user ' + user.email);
        }),
        (error) => {
          console.log('inside error');
        };
      }
    }

It works as it should when everything is correct on backend side. But in case of error I can't manage to catch it here. However, it is caught in the service:

trySaveUser(body: User): Observable<any> {
    return this.http.post<any>(this.pathAPI + '/user', JSON.stringify(body), super.header()).pipe(
      catchError(super.handleError));
}

Inside super.HandleError method I retrieve data from the error and can print it into console without any problems. This method looks like this:

    public handleError(error: HttpErrorResponse | any) {
        //...fetching data from the error to errMsg

        //console.error(errMsg);
        return throwError(errMsg);
    }

I think I don't really know how it works, as I'd expect the error returned from the method above to be caught again in the component here:

(error) => {
  console.log('inside error');
};

...but it doesn't happen. Why so, and how can I correct this? I need to have it in component so that I can display the error message to the user. I'd be grateful for any help!

2
  • Can you try this throw errMsg; instead of return throwError(errMsg); Commented Dec 23, 2018 at 17:17
  • @AmitChigadani It won't let me do this, as catchError method requires Observable as argument. Commented Dec 23, 2018 at 17:22

1 Answer 1

1

You are actually closing the subscribe block after success callback. Remove the extra bracket ( from there, so that you enclose error callback within subscribe.

this.userService.trySaveUser({ email: value.email, password: value.password.pass }).subscribe(
        (user) => {
          console.log('saved user ' + user.email);
        }, // remove extra bracket here
        (error) => {
          console.log('inside error');
        };
 )}  // add that bracket in this line
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, good sir! I've spent way too much time looking for the issue, and it turns out to be the silliest thing possible. Not a best way to start posting on SO... :) Thanks again!
Welcome :) That happens and its a learning. Next time you will be quick to find it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.