1

I have a controller that throws an exception as an example:

   [HttpPost]
    public JsonResult RetrieveTransactionStatuses() {
        throw new Exception("Test exceptioN");
        return this.Json(this.RetrieveStatusLines());
    }

I am calling the above using the angular script:

 $http.post('/Status/RetrieveTransactionStatuses')
        .then(function (response) {
            // success
            console.log('on success');
            vm.statusLines = response.data;
        },
        function () {
            // failure
            console.log('on failure');
            ExceptionHandlers.Methods.ShowError('Error Retrieving Data!', 'There has been an error retrieving the status data');
        });

However, the above method in the controller, will return a page with a 200 OK status. Is there a way to differentiate POST Methods and then have it return a proper HTTP error status code (500)?

Essentially, after the error on a post method, I will popup a warning in a dialog to notify that an error has occurred.

Should I override the OnException() method in the controller or is there an alternative and better way of handling this? If not, how would I be able to identify if a request is a Get or Post method?

I would like to handle all POST errors clientside if possible if that's the recommended method of handling errors.

Samuel Tambunan
  • 335
  • 1
  • 2
  • 11

1 Answers1

0

One way to do this is to catch the exception and send a response that can be interpreted as an error. Something like the following:

[HttpPost]
public JsonResult RetrieveTransactionStatuses() 
{
    object responseObject = null; 

    try
    {
        // your code that might raise an exception
        var statusLines = RetrieveStatusLines();

        // a type should be defined to ensure an homogeneous structure for the response 
        var ret = new 
            { 
                IsError = false,
                ErrorMessage = String.Empty,
                Data = statusLines 
            };
        return Json(ret);
    }
    // multiple catch handler may be used based on expected exception types
    catch (Exception exc)
    {
        // log the error in some place (file, database etc.)

        var ret = new 
            { 
                IsError = true,
                ErrorMessage = "RetrieveTransactionStatuses failed",
                Data = null         // this is irrelevant
            };
        return Json(ret);
    }
}

This ensures control over the way errors are sent to Angular and also can be used when you want to display expected errors (not exceptions).

Just in case, in Angular you should consider defining the request like this to cover errors not related to exceptions and also have the opportunity to execute some code regardless of error/success.

 $http.post('/Status/RetrieveTransactionStatuses')
        .then(function (response) {
            // success
            console.log('on success');
            vm.statusLines = response.data;
        }
        .error(function (data, status) {
            // Handle HTTP error - now only for other causes that your exceptions
        })
        .finally(function () {
            // Execute logic independent of success/error
        })
        .catch(function (error) {
            // Catch and handle exceptions from success/error/finally functions
        });
Community
  • 1
  • 1
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164