I don't get anything in response if an error is thrown from WEB API. but get a response in case of success and debugger hits.
I just want to receive an error response and debugger should be hit always whether an error is thrown or a success message is sent from the web API.
this.dataService.post(this.defaultURL + "changepassword", myObject ).subscribe(result => {
debugger // this is not hitting in case of error.
const res = result;
});
// Data Service contain HTTP post method. and i tested all three shown below.
// post 1
post(url: string, body: string): Observable<any> {
return this.httpClient.post<Response>(url, body);
}
// post 2
post(url: string, body): Observable<any> {
return this.http.post<Response>(url, body).pipe(catchError(error => {
return this.handleError(of(error.error));
} ));
}
// post 3
post(url: string, body): Observable<any> {
return this.httpClient.post<Response>(url, body).pipe(catchError(error => {
retry(1),
catchError(this.handleError)
return this.handleError(of(error.error));
} ));
}
handleError(error) {
debugger
let errorMessage = '';
if (error.error instanceof ErrorEvent) {
// client-side error
errorMessage = `Error: ${error.error.message}`;
} else {
// server-side error
errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
}
console.log(errorMessage);
return throwError(errorMessage);
}
API
[HttpPost("changepassword")]
public IActionResult ChangePassword([FromBody] ChangePasswordViewModel changePasswordModel)
{
if (_identityAdapter.ChangePassword( changePasswordModel.OldPassword, changePasswordModel.NewPassword))
return Ok("Password has been updated successfully");
return BadRequest();
}