I am sure there is a simple answer to this...
Supposing a very simple controller action:
[HttpGet]
[Route("FindItems")]
public async Task<PartialViewResult> FindItems([FromQuery] string order)
{
if(order != "")
PartialView("MyView");
else
throw new Exception("No order number entered");
}
This is called using the Fetch API
fetch(params.url, data)
.then(function (response) {
resp = response;
return response.text();
})
.then(body => {
if (resp.ok && params.success)
params.success(body, resp);
else if (!resp.ok && params.error)
params.error(body, resp);
});
Obviously in this example I could just read the order data before running the fetch api, but that isn't my question...
In this simple example, when the fetch api gets the partial view result, it is plain HTML and returned as the response.text() stream from fetch API and everything is fine.
However, when the C# throws an error, I get
System.Exception: No Order or Transfer note found - please check reference entered
at Sam3.Code._InternalApi.DeliveriesController.FindItems(String order) in PATHTOMYController.cs:line 92
at lambda_method(Closure , Object )
at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
etc etc.... AS TEXT
So I am unable to grab the actual exception object as a JSON object or similar to parse out the Message
property and display it in an alert.... which is the goal...