Azure / azure-cosmos-table-dotnet

.NET SDK for Azure Cosmos Table API
14 stars 5 forks source link

validate RowKey and PartitionKey in TableEntity for disallowed characters before making API call to Azure Storage #64

Open golfalot opened 3 years ago

golfalot commented 3 years ago

The error messages are spectacularly unhelpful in pinpointing the precise root cause when the error occurs. So instead of dealing with Storage Account errors, why not validate on the client side when the entity object is created in the first place ?

1.0.8 Azure Table Storage (legacy, not Cosmos), try to insert TableEntity with a forward slash in the text of the RowKey consistent Storage Emulator and Azure Storage Account and Azure Data Factory

I started this ugly journey due to Azure Data Factory job failing - incidentally ADF is still using the legacy Microsoft.WindowsAzure.Storage

In Azure Data Factory Operation on target Copy_cbm failed: Failure happened on 'Sink' side. ErrorCode=FailedStorageOperation,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=A storage operation failed with the following error 'Element 0 in the batch returned an unexpected response code.'.,Source=,''Type=Microsoft.WindowsAzure.Storage.StorageException,Message=Element 0 in the batch returned an unexpected response code.,Source=Microsoft.WindowsAzure.Storage,StorageExtendedMessage=0:Bad Request - Error in query syntax.

So I proceeded to knock up some code as a workaround using Microsoft.Azure.Cosmos.Table with the following outcomes

CloudTable.ExecuteBatchAsync

Microsoft.Azure.Cosmos.Table.StorageException
  HResult=0x80131500
  Message=Element 0 in the batch returned an unexpected response code.
  Source=Microsoft.Azure.Cosmos.Table
  StackTrace:
   at Microsoft.Azure.Cosmos.Table.RestExecutor.TableCommand.Executor.<ExecuteAsync>d__1`1.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

TableOperation.InsertOrReplace Microsoft.Azure.Cosmos.Table.StorageException: 'The remote server returned an error: (400) Bad Request.'

https://docs.microsoft.com/en-us/rest/api/storageservices/Understanding-the-Table-Service-Data-Model?redirectedfrom=MSDN#characters-disallowed-in-key-fields