Data Access has been discontinued. Please refer to this page for more information.

Error Handling

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

When developing an N-tier applications, it is important that you are able to flow error information through the tiers without losing any context or details in the tiers. With WCF Data Services, you have an error contract which guarantees that all errors thrown from the service, wrapped inside a DataServiceException, will be represented in a standard way on the wire when we send the error down to the client.

Overriding the HandleException Method

WCF Data Services exposes the virtual HandleException method. This lets you capture any exception that happens in the service, analyze it and construct your own DataServiceException to return to the caller.

public partial class EntitiesModelService
{
   protected override void HandleException( HandleExceptionArgs args )
   {
       if ( args.Exception.GetType().Equals( typeof( Telerik.OpenAccess.Exceptions.DataStoreException ) ) )
       {
           args.Exception = new DataServiceException( 500, "Cannot save item. Please try again later." );
       }
       base.HandleException( args );
   }
}
Partial Public Class EntitiesModelService
 Protected Overrides Sub HandleException(ByVal args As HandleExceptionArgs)
  If args.Exception.GetType().Equals(GetType(Telerik.OpenAccess.Exceptions.DataStoreException)) Then
   args.Exception = New DataServiceException(500, "Cannot save item. Please try again later.")
  End If
  MyBase.HandleException(args)
 End Sub
End Class

The HandleExceptionArgs type exposes several properties. The most important is the Exception property. This is where you can capture and identify exceptions thrown by Telerik Data Access. The code in this method checks to see if the exception is of type thrown by Telerik Data Access. If it is, a custom string is passed back to the calling application in a new DataServiceException.

Practically, to override the HandleException method, you need to extend the data service class. As it is described in How to: Extend Data Service, the recommended way to do this it to use partial classes.

Processing the New Exception on the Client

On the client side, you will get a DataServiceRequestException saying: "An error occurred while processing this request". The inner exception, which is of type DataServiceClientException, contains the concrete message, e.g. "Cannot save item. Please try again later.". However, the message is not a simple string, it is formatted:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
 <code></code>
 <message xml:lang="en-US">Cannot save item. Please try again later.</message>
</error>