Search code examples
azureazure-mobile-servicesazure-api-appsazure-app-api

Azure Service App [Mobileservice] - Custom error response


I've been working on an app and sometimes during my coding I receive 500 Internal Server Error with no content, which isn't really what it's suppose to show. I wrote a code to test the error response in the controller as following:

    [HttpGet]
    [Route("TryCatch")]
    public IHttpActionResult TestCatch()
    {
        try
        {
            object obj = null;
            int i = (int) obj;
            return Ok();
        }
        catch (Exception e)
        {
            return InternalServerError(e);
        }
    }

When I try this locally, I do receive the information regarding the error that I wished for. As for in production, all I see is (in postman) { "message": "An error has occurred." } and the status code.

I've looked around and found what I thought would be solution to my problem: Internal Server Error - Azure App Service Custom Controller but it wasn't.

And adding <httpErrors errorMode="Custom" defaultResponseMode="passThrough"> or <customErrors mode="On"/> or anything in web.config doesn't help either.

Any help is appreciated!


Solution

  • Please change the custom errors mode to Off instead of On.

    <customErrors mode="Off"/>
    

    And change IncludeErrorDetailPolicy property to Always in WebApiConfig.cs file(Azure Mobile Service) or Startup.cs file(Azure Mobile App).

    config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
    

    After that, I can get the detail exception message from the exceptionMessage property.

    {"message":"An error has occurred.","exceptionMessage":"Object reference not set to an instance of an object.","exceptionType":"System.NullReferenceException","stackTrace":""}