Problem
I have an Azure Function HTTP triggered function written in Python 3.8. This function receives an incoming HTTP request and writes an entity to an Azure Table. If an incoming request tries to create a duplicate entry, Azure Table throws an EntityAlreadyExists
error to the Azure Function runner. I would like to catch this exception and handle it accordingly.
Can I catch this exception in python using a try
/except
block from within the Azure Function? If so, how? If not, do you know why?
Things I have tried
try
to run code, thenexcept ValueError as err:
to handle exceptiontry
to run code, thenexcept Exception as err:
to handle exceptiontry
to run code, thenexcept EntityAlreadyExists as err:
to handle exception
None of these were successful at catching the exception being thrown from Azure Table for a duplicate entry attempt.
Links
- Related question for c#: How to catch an Exception throw by Azure Table in an async Azure Function HTTP Triggered function.
- Table service error codes: https://learn.microsoft.com/en-us/rest/api/storageservices/table-service-error-codes
Error thrown
This is the error I am trying to catch from within my HTTP-triggered Azure Function
Executed 'Functions.myTable' (Failed, Id=xxx-xxx-xxx-xxx, Duration=1256ms)
System.Private.CoreLib: Exception while executing function: Functions.myTable. Microsoft.Azure.WebJobs.Host: Error while handling parameter _binder after function returned:. Microsoft.Azure.WebJobs.Extensions.Storage: The specified entity already exists.
RequestId:xxx-xxx-xxx-xxx
Time:2020-09-30T13:16:00.9339049Z (HTTP status code 409: EntityAlreadyExists. The specified entity already exists.
RequestId:xxx-xxx-xxx-xxx
Time:2020-09-30T13:16:00.9339049Z). Microsoft.WindowsAzure.Storage: The specified entity already exists.
RequestId:xxx-xxx-xxx-xxx
Time:2020-09-30T13:16:00.9339049Z.
--init--.py
Below is relevant portions of the py file for the Azure function. The question surrounds the try
/except
block in Lines 16-27 (line numbers not shown).
import logging
import json
import azure.functions as func
def main(req: func.HttpRequest, myTable: func.Out[str]) -> func.HttpResponse:
body = req.get_json()
data = { # Data to send to Azure Table
"PartitionKey": body.get('var1'),
"RowKey": body.get('var2'),
"Property1" : body.get('var3'),
"Property2" : body.get('var4')
}
try: # Try to send record to Azure Table
myTable.set(json.dumps(data))
except ValueError as err: # Respond with 409 if duplicate record
logging.error(err)
return func.HttpResponse(
body=f'Record already exists.',
status_code=409
)
else: # Otherwise, respond with 201 success
return func.HttpResponse(
body=f'Success.',
status_code=201
)
function.json
Below are the triggers and bindings json for the Azure function.
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"post"
]
},
{
"name": "myTable",
"type": "table",
"tableName": "myTable",
"connection": "AzureWebJobsStorage",
"direction": "out"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}