I would like to return exception message to the AngularJS UI. As a back-end I use ASP.NET Core Web Api controller:
[Route("api/cars/{carNumber}")]
public string Get(string carNumber)
{
var jsonHttpResponse = _carInfoProvider.GetAllCarsByNumber(carNumber);
if (jsonHttpResponse.HasError)
{
var message = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(jsonHttpResponse.ErrorMessage)
};
throw new HttpResponseException(message);
}
return jsonHttpResponse.Content;
}
But on Angular side, failure promise sees only status and statusText "Internal server error":
How can I pass the error message to the Angular $http failure promise from Core Web Api?
HttpStatusCode.InternalServerError
in this code, which is HTTP 500. Instead, you should figure out what error is injsonHttpResponse
and setmessage
to that. – Filmer