I have a backend api to create new product. The frontend angular code needs to call a backend API. How to carry out error handling using ** .subscribe**. I'm using HTTPClient and using Observable and been reading about how RXJS utilizes error handling,
The app should call it as Observable and .subscribe().
the api /create should handle success and failure(error) - If API returns 200: it should print success - If API returns 400, should throw an error
pseudo code
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
ID = 'foo';
this.http.post('/api/create', {
productName: this.form.value.productName,
productValue: this.form.value.productValue,
}).subscribe(
resp => this.onSubmitSuccess(resp), err => this.onSubmitFailure(err)
);
private onSubmitSuccess(resp) {
console.log(resp);
this.ID = resp.ID;
this.submitSuccess = true;
this.submitFailed = false;
}
private onSubmitFailure(resp) {
console.log(resp);
this.submitFailed = true;
this.submitSuccess = false;
} ```