0

I am sending GET requests from AngularJs and I want to display actual server errors on failure (this is a proof-of-concept, so I can display technical errors to the testers). This works correctly on localhost, but fails for remote requests (when deployed on a server).

Note: irrelevant code is stripped for better readability

Client code

$http(loadReqData).success(function(data, status ) {
    if (status !== 200) {
        // non OK responses handling ...
        return;
    }

    // ok handling
}).error(function(data, status, headers, config) {
    // error handling ...
    // here I do not receive expected information (in data variable)
}).finally(function() {
    $loading.finish(loaderName);
});

Server-side

This is an MVC controller, not an Web.API one

[Route("GetEnvironmentTableRowCount")]
public JsonResult GetEnvironmentTableRowCount(int environmentId, string tableName, string whereClause)
{
    try
    {
        long? rowCount = TeradataUtilsService.GetRowCount(environmentId, tableName, whereClause);
        return Json(new { RowCount = rowCount }, JsonRequestBehavior.AllowGet);
    }
    catch (Exception exc)
    {
        Logger.Log(LogLevel.Error, exc, "Failed to get row count");
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return Json($"Failed to get row count - {exc.Message}", JsonRequestBehavior.AllowGet);
    }
}

When running on localhost and I have an error data will receive the actual error:

Failed to get row count - ...

When running on a server and I have an error data will only contain

Bad request

I have allowed custom errors to be shown (web.config):

<system.web>      
    <customErrors mode="Off" />       
</system.web>  

Question: why don't I receive the http response content when deployed on a server?

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
  • Is the error content the standard IIS response? The first thing that sprang to mind is that although you have `CustomErrors` turned off, the remote web server's error handling might be picking up the error and displaying it's default error content. also, does your web.config have ` – scgough Jul 14 '17 at 08:25
  • Can you try returning string not JsonResult? In this case, you have to serialize object to json of course. – ali Jul 14 '17 at 08:31
  • @scgough - `httpErrors` is not specified within web.config. customErrors presence or its value does not seem to change the outcome. – Alexei - check Codidact Jul 14 '17 at 08:37
  • @Alexei ok - for sanity, can you try adding the `httpErrors` node to web.config and testing that 'live'. See: https://stackoverflow.com/questions/28989026/how-do-i-catch-the-http-status-code-of-server-errors-into-a-generic-custom-error/28991628#28991628 (IIS7+ uses httperrrors over customerrors I think) – scgough Jul 14 '17 at 08:41
  • @ali - I am tried returning a ContentResult instead of a JsonResult, but the result is the same. – Alexei - check Codidact Jul 14 '17 at 08:42

1 Answers1

1

Please try adding this to your web.config:

<httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough" />

Note, this will apply to all error responses but you should achieve the desired result of setting the Response.Code and the custom response.

scgough
  • 5,099
  • 3
  • 30
  • 48
  • Yes, that did the trick for me. Thanks. Also, not setting `httpErrors` at all, but setting `TryToSkipCustomErrors` and removing `filters.Add(new HandleErrorAttribute());` will have the same result. – Alexei - check Codidact Jul 14 '17 at 08:57