Search code examples
.netasp.net-web-apiodata

OData controller returns different error for local and different for remote machine


In my project I use OData 3.0 and I need to handle error that comes from SQL stored procedure. Here is the controller code:

public class StartProductionBatchListController : ODataController {
    private SitContext<StartProductionBatchModel> db = new SitContext<StartProductionBatchModel>();

    [EnableQuery]
    [SITAuthorize("/production/ordersOverview")]
    public DbRawSqlQuery<StartProductionBatchModel> GetStartProductionBatchList([FromODataUri] string entryId)
    {
        Dictionary<string, string> parameters = new Dictionary<string, string>();

        parameters.Add("ENTRY_ID", entryId);

        return db.ExecuteProcedure("StartProductionBatch", parameters);
    }
}

In case of error in the stored procedure the exception is raised using SQL command RAISERROR.

On my client side I receive two different responses - if I connect from localhost, I get this:

{
  "odata.error":{
    "code":"","message":{
      "lang":"en-US","value":"An error has occurred."
    },"innererror":{
      "message":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","type":"System.InvalidOperationException","stacktrace":"","internalexception":{

        "message":"The following materials are not available on the line: 562942",

"type":"System.Data.SqlClient.SqlException","stacktrace":"

    ... there is the entire stack here

      }
    }
  }
}

...where I can read the error message (The following materials are not available on the line: 562942)

If I do the same thing from the remote computer, here is what I get back:

{
  "odata.error":{
    "code":"","message":{
      "lang":"en-US","value":"An error has occurred."
    }
  }
}

I tried to setup my IIS to return the detailed error (setting <httpErrors errorMode="Detailed" /> in web.config) even though it may be a security risc (not really in this case, it is an intranet application not published to the Internet), but nothing helped.

Thanks for any help.


Solution

  • You need to set the IncludeErrorDetailPolicy property on the HttpConfiguration class like this:

    config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
    

    More details can be found here: https://msdn.microsoft.com/en-us/library/system.web.http.httpconfiguration.includeerrordetailpolicy(v=vs.108).aspx