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!
throw errMsg;instead ofreturn throwError(errMsg);