How to resolve internal service error with cosmosDb Azure Function bindings when item is not found

Brandon Wozniewicz 5 Reputation points
2023-08-14T17:09:55.2066667+00:00

Following this tutorial, I receive an internal service error when the item is not found. My app's runtime is 4.x, and I've made sure to update the configuration as appropriate.
{
"name": "bookmark",
"databaseName": "func-io-learn-db",
"containerName": "Bookmarks",
"connection": "-------",
"id":"{id}",
"partitionKey": "{id}",
"direction": "in",
"type": "cosmosDB"
}

It works fine if the item exists, but if the item does not, an internal service error is thrown, not even allowing my custom function to return a more graceful error. I've seen a number of issues on Github related to ensuring the partitionId and id values are {id}, which I have done. This issue can be found here: https://github.com/Azure/Azure-Functions/issues/1670.

Is the only solution to roll back to v2? I have a hard time believing 2 years later, this is still the solution.

Thanks for any help.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,929 questions
{count} vote

4 answers

Sort by: Most helpful
  1. Daniel Gelencser 25 Reputation points
    2024-01-16T10:41:38.5333333+00:00

    Basically the exercise assume that you are using a 3.x version of the extension bundle, however the default is 4.x. I am not sure why it is not yet working with the new version (4.x) yet as previous version (3.x) will be retired on August 31 2024 (https://learn.microsoft.com/en-us/azure/azure-functions/migrate-cosmos-db-version-3-version-4?tabs=isolated-process&pivots=programming-language-javascript).

    Found the fix on 3rd party a forum post, with the following steps

    1. After the HttpTrigger2 function is created open the function app.
    2. Click on App Service Editor (and Open Editor)
    3. Select host.json and change it to this:
    {
      "version": "2.0",
      "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[3.*, 4.0.0)"
      }
    }
       
    
    1. Go back to the function app overview and restart the app.

    After these steps you should be able to continue with the exercise (as of 16 January 2023).

    3 people found this answer helpful.

  2. Guta Daniel 10 Reputation points
    2023-10-03T08:09:42.46+00:00

    related your trainings the azure functions don't behave as expected : https://learn.microsoft.com/en-us/training/modules/chain-azure-functions-data-using-bindings/5-read-data-with-input-bindings-portal-lab?pivots=javascript

    I have Runtime Version ~4 and can not select another value. There are two issues here:
    1. When using an inputBinding with AzureCosmosDB NOSQL and the value is not present into the database the function will give an abort and put in the log and error 404 not found but for the HTTP response will raise a 500 Internal Server Error
    2. connecting to AzureCosmos using input/output Binding is not correct using the portal way of configuring the bindings (connection and containerName should be present)

    For the point 1:
    "2023-10-03T08:14:18.646 [Error] Executed 'Functions.myHttpTrigger2' (Failed, Id=75ef38cf-d474-41fa-bfab-24f407afbb08, Duration=105ms)Response status code does not indicate success: NotFound (404); Substatus: 0; ActivityId: 0d09d743-fd0d-40a0-bebf-dcfbbae0402e; Reason: (code : NotFoundmessage : Entity with the specified id does not exist in the system. More info: https://aka.ms/cosmosdb-tsg-not-found,RequestStartTime: 2023-10-03T08:14:18.5698536Z,.........."
    This is the message error when using in the input Json an "in" value that is not present into the AzureCosmosDB

    For the point 2:
    Instead of

    {
          "name": "inputDocument",
          "direction": "in",
          "type": "cosmosDB",
          "connectionStringSetting": "myfirstazurecosmos_DOCUMENTDB",
          "databaseName": "inDatabase",
          "collectionName": "MyCollection",
          "partitionKey": "{id}"
        }
    

    You need to have

    {
          "name": "inputDocument",
          "direction": "in",
          "type": "cosmosDB",
          "connection": "myfirstazurecosmos_DOCUMENTDB",
          "databaseName": "inDatabase",
          "containerName": "MyCollection",
          "partitionKey": "{id}"
        }
    

    Otherwise you get errors in the log and can not reach the cosmos DB

    2 people found this answer helpful.

  3. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2023-08-15T15:31:15.6166667+00:00

    Wozniewicz, Brandon R (FAA) Thanks for posting your question in Microsoft Q&A. I followed the tutorial and got 404 NotFound error when item doesn't exist (unable to reproduce the error).

    Output:
    2023-08-15T15:26:38.034 [Information] Executing 'Functions.HttpTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=66e1b5f5-3e19-400d-9de7-e059be8b84ac)

    2023-08-15T15:26:38.116 [Error] Executed 'Functions.HttpTrigger1' (Failed, Id=66e1b5f5-3e19-400d-9de7-e059be8b84ac, Duration=55ms)Response status code does not indicate success: NotFound (404); Substatus: 0; ActivityId: 7226f6b7-7883-45c1-b6c2-e9e673eab163; Reason: (code : NotFoundmessage : Entity with the specified id does not exist in the system. More info: https://aka.ms/cosmosdb-tsg-not-found,RequestStartTime: 2023-08-15T15:26:38.0563586Z, RequestEndTime: 2023-08-15T15:26:38.0807738Z, Number of regions attempted:1{"systemHistory":[{"dateUtc":"2023-08-15T15:25:39.3398045Z","cpu":0.670,"memory":472870132.000,"threadInfo":

    function.json

    {
          "name": "bookmark",
          "direction": "in",
          "type": "cosmosDB",
          "connection": "<db name>",
          "databaseName": "func-io-learn-db",
          "containerName": "Bookmarks",
          "id": "{id}",
          "partitionKey": "{id}"
        }
    

    If you still face internal server error, please share the code snippet, function.json, request url/activity id and the output of the error message. That would help in investigating the backend logs and find the cause.


    If you found the answer to your question helpful, please take a moment to mark it as "Yes" for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.


  4. humaira siddiqui 0 Reputation points
    2024-02-03T19:31:43.22+00:00

    I am also facing the same issue.. I have added logs to trace the cause - the function is not going in else statement, it seems the exception is raised from cosmos db. Not able to figure out a solution. Any help is appreciated User's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.