Oskars
Oskars

Reputation: 135

angular not getting exception message from .net core

So I would like to get in console an json array with message, what would be equal with "Your direct manager have no account for your area."

But all I am getting is "Http failure response for https://localhost:44385/api/Option/Post: 500 Internal Server Error"

Note: When looking at dev tools -> network -> post -> response, then it shows html and css with right error message included, but no idea how to get it out from there.

Controller

[HttpPost("[action]")]
public void Post(Option option)
{
    throw new NoBudgetAccountException();
}

Exception

[Serializable]
public class NoBudgetAccountException : Exception
{
    public NoBudgetAccountException() : base("Your direct manager have no account for your area.")
    {
    }
}

Angular apiService

addOrUpdateOption(option: ITrainingOption): Observable<Response> {
    return this.post<Response>("api/Option/Post", option);
  }

Angular component

submitOption(option: ITrainingOption) {
        this.apiService.addOrUpdateOption(option).subscribe(() => {
            this.isAddOptionShown = false;
            this.getHeadlines(this.selectedYear);
          },
          err => { console.log(err); });
      }

Upvotes: 2

Views: 3773

Answers (1)

dardardardar
dardardardar

Reputation: 344

usually with .net stuff when you do something like

throw new Exception("This is a custom exception");

It doesn't actually give you this message, instead it throws you an error html page enter image description here

If you want to return a message and parse it in angular use an IActionResult instead and return BadRequest("message")

    [HttpPost("[action]")]
    public IActionResult test(Option option)
    {
        return BadRequest("This is an error message");
    }

After doing this your error message should show in err.error

enter image description here

Upvotes: 3

Related Questions