Jose Tomas
Jose Tomas

Reputation: 117

Azure Function crashes upon missing url query

I have an Azure-Function with the following configuration

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "cosmosDB",
      "name": "inputDocument",
      "databaseName": "MeasureInputData",
      "collectionName": "hubs",
      "connectionStringSetting": "measure-cosmosdb_DOCUMENTDB",
      "direction": "in",
      "sqlQuery": "SELECT * FROM c WHERE c.uuid = {hub}"
    }
  ],
  "disabled": false
}

As you can see above, I have the {hub} in the sqlQuery passed as a url parameter.

Doing a request like

    https://<my-function-url>?code=<my-function-key>&hub=<my-hub-id>

returns the matching hub.

If leaving the value <my-hub-id> empty

    https://<my-function-url>?code=<my-function-key>&hub=

returns an empty object.

The issue is when I don´t provide the hub parameter in the URL the function crashes.

    https://<my-function-url>?code=<my-function-key>

requesting the above returns http status code 500

Shouldn't missing query parameters be handled in some way?

Thanks

Upvotes: 0

Views: 270

Answers (2)

Hury Shen
Hury Shen

Reputation: 15724

It seems the sql will be

SELECT * FROM c WHERE c.uuid =

but not

SELECT * FROM c WHERE c.uuid = "" or SELECT * FROM c WHERE c.uuid = null

when the parameter "hub" missing.

Azure function cosmos db binding will not handle the missing parameter in query. If you want to handle this error, I think we can create another api or function and put(call) the original function url in this new api/function. Then we can handle this error in the new api/function.

If you hope azure function to add this feature, you can raise your idea to the azure function feedback page.

Upvotes: 1

Thiago Custodio
Thiago Custodio

Reputation: 18387

No, you should handled that. If you check the hello world sample:

[FunctionName("HttpTriggerCSharp")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] 
    HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    return name != null
        ? (ActionResult)new OkObjectResult($"Hello, {name}")
        : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

It tries to read value from query string and from body. In case it's null, it will return

new BadRequestObjectResult("Please pass a name on the query string or in the request body")

so you should do the same, in case there's no hub provided in the querystring, return 400 (bad request)

Upvotes: 0

Related Questions