There may be many situations where a data service call might fail. Any error that is raised in the data service should be represented in the form of DataServiceException
in order to transfer a structured error to the client. The following code example illustrates how to change our custom data service class to handle exceptions:
public class PayrollDataService : WebDataService <PayrollModel.PayrollEntities> { public static void InitializeService (IDataServiceConfiguration config) { config.SetEntitySetAccessRule("*", EntitySetRights.All); config.UseVerboseErrors = false; } protected override void HandleException(HandleExceptionArgs args) { if(args.Exception is ArgumentException) { ArgumentException ex = (ArgumentException) args.Exception; args.Exception = new DataServiceException(400, "PropertySyntaxError:" + ex.ParamName, "Error Message: ", "en-US", ex); } } }