I am using jquery-ajax-unobtrusive to make an ajax call to the controller endpoint in an ASP.NET Core 8 MVC web app.
The endpoint returns a PartialView
if successful and is working as expected on success 200.
However, where I am struggling with is best practice on how to deal with an unexpected exception. E.g. the database times out during save.
I could hook into the data-ajax-failure=”onFailure” and spit out the exception response info into an alert or div…
But in my mind, as it is likely in the future the project will configure custom error pages to deal with different types of exceptions and style those pages as preferred…
I don’t want to circumvent this just because I happen to be doing a partial view ajax post.
In other words I think the best thing would be for the whole page to display the intended error page. But I don’t know how best to handle this?
I did try to achieve this by writing the jqXHR to the document…
if (jqXHR.status !== 200) {
// Replace the entire body content with the server response
alert('replace error');
document.open();
document.write(jqXHR.responseText);
document.close();
}
But the jqXHR.responseText
appears to only be the exception details and not the html to render the error page (See Actual vs Expected).
Actual:
Expected:
I guess then that when doing a standard full page postback, somewhere along the pipeline a redirect is returned which is not being handled during the ajax call.
This makes me believe I am approaching this incorrectly, either in the controller endpoint or javascript?