0

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...

Jamie Hartnoll
  • 7,231
  • 13
  • 58
  • 97
  • If your controller is throwing an exception it should be returning some HTTP status other than `400 OK`, can you not check for this on the receiving end? – madmonk46 Nov 04 '22 at 11:49
  • yes, it is returning 500, but the body returned is jsut a string – Jamie Hartnoll Nov 04 '22 at 11:50
  • Take a look at the following approach to dealing with exceptions during Ajax calls and returning a JSON response that has error info properties on it. It involves subclassing `HandleErrorAttribute` and extending its behavior which you can see a complete source code example of here: http://stackoverflow.com/questions/12705345/mvc3-return-json-on-error-instead-of-html – David Tansey Nov 05 '22 at 20:09
  • 1
    This StackOverflow post covers the same material with slightly different code: https://stackoverflow.com/questions/4707755/asp-net-mvc-ajax-error-handling – David Tansey Nov 05 '22 at 20:29
  • Hi, it seems your issue here is do not display the error when throw exception in the backend. I am doubt that why you use multiple `.then`, if you want to display the error, why not using `.catch` function instead? – Rena Dec 09 '22 at 02:34

0 Answers0