Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Api on Azure shows no error detail using 'return InternalServerError(ex)'

My Web Api when run locally (in Release mode) will return any errors in this format:

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "No text specified",
    "ExceptionType": "System.Exception",
    "StackTrace": null
}

But after deployment/publish to an Azure VM, only this remains:

{
    "Message": "An error has occurred."
}

API code:

try
{
    var msg = ...
    new MessageService().SaveMessage(msg)); // <-- does some checks; may throw.
    return Ok();
}
catch (Exception ex)
{
    return InternalServerError(ex);
}

I'd like it to be more detailed on Azure, like the local result.
Can this be achieved, and if so, how?

I already (temporarily) removed <compilation xdt:Transform="RemoveAttributes(debug)" /> from the <system.web> part of Web.Release.config, and then re-deployed, but that made no difference.

Or am I using the wrong approach/pattern?
Obviously technical details should be limited, but right now we get no details at all.

like image 901
Peter B Avatar asked Aug 21 '14 15:08

Peter B


3 Answers

You could try adding the following to your Global.asax:

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

Note: I wouldn't recommend that you keep this setting on in a production environment.

like image 95
Jon Susiak Avatar answered Nov 17 '22 01:11

Jon Susiak


If instead you use

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Default; 

then you can use the system.webServer error switch e.g.

<system.webServer>
    <httpErrors errorMode="Detailed" existingResponse="PassThrough">
    </httpErrors>
</system.webServer>

Note the existingResponse attribute to preserve the error message.

like image 33
Paul Hatcher Avatar answered Nov 16 '22 23:11

Paul Hatcher


I had the same problem, the post is three years old, things have changed a little. If you setup a new Azure Mobile App with Visual Studio 2017 there is no longer a Global.asax.cs. I searched for hours, where to put this IncludeErrorDetailPolicy. It won't work without that setting.

You do it in your Startup.MobileApp.cs:

  public partial class Startup
  {
        public static void ConfigureMobileApp(IAppBuilder app)
        {
            HttpConfiguration config = new HttpConfiguration();

            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

            new MobileAppConfiguration()
                .UseDefaultConfiguration()
                .ApplyTo(config);

Don't forget, in your Web.config you also need to set:

<system.webServer>
    <httpErrors errorMode="Detailed" existingResponse="PassThrough">
    </httpErrors>
</system.webServer>

Don't use that for production environment!

like image 4
Inna Avatar answered Nov 17 '22 01:11

Inna