azure-functions-python-worker icon indicating copy to clipboard operation
azure-functions-python-worker copied to clipboard

Return specific status code for azure function with input binding event grid trigger

Open imbachb opened this issue 3 years ago • 5 comments

What language does your question apply to? (e.g., C#, JavaScript, Java, All)

Python

Question

In my application I have the issue that a failing event grid triggered azure function is continually triggered because of the event grid retry policy. As we can read in Azure Event Grid - Delivery And Retry, if the azure function fails with error code 500 the event grid continuously retries to deliver the event to the azure function.

In most cases this is fine, but in a specific case I would like to catch the specific exception and return the function with a status code 400, as this would stop the event grid from retrying to deliver the event.

Is this possible, or am I forced catch the exception and return the function normally, resulting in a status code of 200? I would prefer to mark this trigger as failing.

What I tried: Add an HTTP output binding to my function.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "eventGridTrigger",
      "name": "event",
      "direction": "in"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

However, when I return the azure function with return func.HttpResponse(e, status_code=400) the following casting exception is thrown later: System.Private.CoreLib: Exception while executing function: Functions.ImageUploadTrigger. Microsoft.Azure.WebJobs.Script: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Microsoft.AspNetCore.Http.HttpRequest'.

It seems an "in" "eventGridTrigger" is not compatible with an "http" "out" binding.

imbachb avatar Jul 06 '21 19:07 imbachb

Thank you for your feedback! We will investigate this scenario and update you with the possibilities.

v-anvari avatar Jul 09 '21 16:07 v-anvari

Hi @imbachb , We can return the status code like you are trying here. the issue looks like it is not correctly handling the parsing of exception "e" to JSON parsable message.

Specifically this -

return func.HttpResponse(e, status_code=400)

Can you try with a simpler message or try formatting the exception to make the translation happen correctly.

In case we find any sample code, we will update that here.

v-anvari avatar Jul 16 '21 10:07 v-anvari

Thanks for getting back to me. I've quickly tried to create a minimal azure function called EventGridTrigger1: __init__.py:

import azure.functions as func


def main(event: func.EventGridEvent):
    return func.HttpResponse(status_code=400)

function.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "eventGridTrigger",
      "name": "event",
      "direction": "in"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

When I then trigger the function via following event:

POST http://localhost:7071/runtime/webhooks/eventgrid?functionName=EventGridTrigger1
content-type: application/json
aeg-event-type: Notification

[{
  "topic": "/subscriptions/{subscription-id}/resourceGroups/SomeResourceGroup/providers/Microsoft.Storage/storageAccounts/someStorage",
  "subject": "/blobServices/default/containers/some-container/blobs/some-blob.jpg",
  "eventType": "Microsoft.Storage.BlobCreated",
  "id": "some-id",
  "data": {
    "api": "PutBlob",
    "clientRequestId": "some-id",
    "requestId": "some-id",
    "eTag": "0x8D93E1E762B18BE",
    "contentType": "image/jpeg",
    "contentLength": 53999,
    "blobType": "BlockBlob",
    "url": "https://somestore.blob.core.windows.net/some-container/some-blob.jpg",
    "sequencer": "000000000000000000000000000001740000000000b00311",
    "storageDiagnostics": {
      "batchId": "some-id"
    }
  },
  "dataVersion": "",
  "metadataVersion": "1",
  "eventTime": "2000-01-04T15:12:35.621399Z"
}]

I still get the following exception: System.Private.CoreLib: Exception while executing function: Functions.EventGridTrigger1. Microsoft.Azure.WebJobs.Script: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Microsoft.AspNetCore.Http.HttpRequest'.

I've tried following return values, all resulting in the same exception:

return func.HttpResponse(status_code=400)
return func.HttpResponse("Test", status_code=400)
return func.HttpResponse({"test": "test"}, status_code=400)
return func.HttpResponse(json.dumps({"test": "test"}), status_code=400)

How does the return statement look in your code to make it work?

imbachb avatar Jul 20 '21 10:07 imbachb

Transferring the issue to the azure-functions-python-worker for more insights

v-anvari avatar Jul 20 '21 11:07 v-anvari

Hi, currently it is not supported to return a different status code with the event grid binding.

shreyabatra4 avatar Jan 24 '22 20:01 shreyabatra4