DataServiceClientException : How to parse Error Message ?

Answered DataServiceClientException : How to parse Error Message ?

  • Wednesday, October 22, 2008 12:22 PM
     
     

    Hello,

    I'm using an ADO.NET Data Services in an intranet scenario, so IDataServiceConfiguration.UseVerboseErrors is set to 'true'.

    I'm wondering how I could parse the message property of an DataServiceClientException in order to display the underlying database error if applicable ?

    Consider the following Exception:

     

    Code Snippet
    System.Data.Services.Client.DataServiceRequestException was caught
      Message="An error occurred while processing this request."
      Source="System.Data.Services.Client"
      StackTrace:
           at System.Data.Services.Client.DataServiceContext.SaveAsyncResult.HandleBatchResponse()
           at System.Data.Services.Client.DataServiceContext.SaveAsyncResult.EndRequest()
           at System.Data.Services.Client.DataServiceContext.EndSaveChanges(IAsyncResult asyncResult)
           at RTE.Payment.Client.BankTransactionStore.<Create>d__2.MoveNext()
      InnerException: System.Data.Services.Client.DataServiceClientException
           Message="<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n<error xmlns=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\">\r\n  <code></code>\r\n  <message xml:lang=\"fr-FR\">Une erreur s'est produite lors du traitement de cette requête.</message>\r\n  <innererror xmlns=\"xmlns\">\r\n    <message>Une erreur s'est produite lors de la mise à jour des entrées. Pour plus d'informations, consultez InnerException.</message>\r\n    <type>System.Data.UpdateException</type>\r\n    <stacktrace>   à System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)&#xD;\n   à System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)&#xD;\n   à System.Data.Services.Providers.ObjectContextServiceProvider.SaveChanges()&#xD;\n   à System.Data.Services.DataService`1.HandleNonBatchRequest(RequestDescription description)&#xD;\n   à System.Data.Services.DataService`1.HandleRequest()</stacktrace>\r\n    <internalexception>\r\n      <message>String or binary data would be truncated.&#xD;\nThe statement has been terminated.</message>\r\n      <type>System.Data.SqlClient.SqlException</type>\r\n      <stacktrace>   à System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)&#xD;\n   à System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)&#xD;\n   à System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)&#xD;\n   à System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)&#xD;\n   à System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)&#xD;\n   à System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)&#xD;\n   à System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)&#xD;\n   à System.Data.SqlClient.SqlCommand.ExecuteNonQuery()&#xD;\n   à System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)&#xD;\n   à System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)</stacktrace>\r\n    </internalexception>\r\n  </innererror>\r\n</error>\r\n"
           Source="System.Data.Services.Client"
           StatusCode=500
           StackTrace:
                at System.Data.Services.Client.DataServiceContext.SaveAsyncResult.<HandleBatchResponse>d__1d.MoveNext()
           InnerException:

     

     

    There is an inner exception of type System.Data.SqlClient.SqlException that was serialized in the message.

    Any guidelines to reflect this error to the underlying data type ?

    TIA.


All Replies

  • Tuesday, October 28, 2008 2:12 AM
    Answerer
     
     Answered

    Hi Olivier,

     

    You can write a function to parse the XML to construct the InnerException contained in DataServiceClientException.  Unfortunately we don't have an easier way for you right now.  The <innererror> node represents the InnerException of the DataServiceClientException.

     

    Hope this helps!

    -J

  • Friday, September 21, 2012 8:23 PM
     
     
    This is not an acceptable answer!
  • Monday, September 24, 2012 7:18 AM
    Moderator
     
     

    Hi,

    The XML of the error payload is pretty simple, so I don't see a problem parsing it.

    But if you want a library to do it for you, you can use ODataLib (Microsoft.Data.OData.dll, available on NuGet). But honestly, it's probably more work to setup everything so that ODataLib will read it, than to use XLinq to extract the four elements which are of interest.

    Thanks,


    Vitek Karas [MSFT]