static FaultException CreateFaultException(InstancePersistenceException exception)
 {
     return new FaultException(OperationExecutionFault.CreateInstanceNotFoundFault(exception.Message));
 }
        public static Exception GetError(XName commandName, CommandResult result, SqlDataReader reader)
        {
            Exception returnValue = null;

            if (result != CommandResult.Success)
            {
                switch (result)
                {
                    case CommandResult.InstanceAlreadyExists:
                        returnValue = new InstanceCollisionException(commandName, reader.GetGuid(1));
                        break;
                    case CommandResult.InstanceLockNotAcquired:
                        returnValue = new InstanceLockedException(commandName, reader.GetGuid(1), reader.GetGuid(2), ReadLockOwnerMetadata(reader));
                        break;
                    case CommandResult.InstanceNotFound:
                        returnValue = new InstanceNotReadyException(commandName, reader.GetGuid(1));
                        break;
                    case CommandResult.KeyAlreadyExists:
                        returnValue = new InstanceKeyCollisionException(commandName, Guid.Empty,
                            new InstanceKey(reader.GetGuid(1)), Guid.Empty);
                        break;
                    case CommandResult.KeyNotFound:
                        returnValue = new InstanceKeyNotReadyException(commandName, new InstanceKey(reader.GetGuid(1)));
                        break;
                    case CommandResult.InstanceLockLost:
                        returnValue = new InstanceLockLostException(commandName, reader.GetGuid(1));
                        break;
                    case CommandResult.InstanceCompleted:
                        returnValue = new InstanceCompleteException(commandName, reader.GetGuid(1));
                        break;
                    case CommandResult.KeyDisassociated:
                        returnValue = new InstanceKeyCompleteException(commandName, new InstanceKey(reader.GetGuid(1)));
                        break;
                    case CommandResult.StaleInstanceVersion:
                        returnValue = new InstanceLockLostException(commandName, reader.GetGuid(1));
                        break;
                    case CommandResult.HostLockExpired:
                        returnValue = new InstancePersistenceException(SR.HostLockExpired);
                        break;
                    case CommandResult.HostLockNotFound:
                        returnValue = new InstancePersistenceException(SR.HostLockNotFound);
                        break;
                    case CommandResult.CleanupInProgress:
                        returnValue = new InstancePersistenceCommandException(SR.CleanupInProgress);
                        break;
                    case CommandResult.InstanceAlreadyLockedToOwner:
                        returnValue = new InstanceAlreadyLockedToOwnerException(commandName, reader.GetGuid(1), reader.GetInt64(2));
                        break;
                    default:
                        returnValue = new InstancePersistenceCommandException(SR.UnknownSprocResult(result));
                        break;
                }
            }

            return returnValue;
        }