I am working on an asp.net core webApi along with an Angular9 WebApp.
My goal is to retrieve the error in a subscribe as an object rather than just a string.
this.http.post<TestSystem>(this.url, testsystem).subscribe((result) => {
// do something
}, err => {
console.log(typeof err);
});
The asp.net core backend method returns
[HttpPost(template: "save")]
public ActionResult<TestSystemBean> Save(TestSystemModel model)
{
// Validate reportRequest
if (ModelState.IsValid == false)
{
return BadRequest("Invalid ModelState");
}
}
However, I am only receiving the string 'Bad Request' and not an object containing the message 'Invalid Modelstate'. How can I retrieve the error as an object?
Within the ErrorInterceptor of the AngularApp, I am getting a detailed HttpErrorResponse:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
debugger;
if ([401, 403].includes(err.status) && this.authenticationService.userValue) {
// auto logout if 401 or 403 response returned from api
this.authenticationService.logout();
}
const error = (err && err.error && err.error.message) || err.statusText;
return throwError(error);
}))
}