Azure函数在缺少网址查询时崩溃

问题描述 投票:0回答:2

我具有具有以下配置的Azure功能

{
  "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
}

如您在上面看到的,我将{hub}中的sqlQuery作为url参数传递。

提出类似要求

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

返回匹配的集线器。

如果将值<my-hub-id>留空

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

返回一个空对象。

问题是当我在函数崩溃的URL中不提供hub参数时。

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

请求以上返回http status code 500

是否应该以某种方式处理缺少的查询参数?

谢谢

azure crash azure-functions http-status-code-500
2个回答
0
投票

不,您应该处理。如果检查hello world示例:

[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");
}

它试图从查询字符串和正文中读取值。如果它为null,它将返回

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

因此,如果查询字符串中没有提供集线器,请返回400(错误请求)


0
投票

似乎SQL将是

SELECT * FROM c WHERE c.uuid =

但不是

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

缺少参数“集线器”时。

Azure函数cosmos db绑定将无法处理查询中缺少的参数。如果您想处理此错误,我想我们可以创建另一个api或函数,并将原始函数url(调用)放在此新api /函数中。然后,我们可以在新的api /函数中处理此错误。

如果您希望使用天蓝色函数添加此功能,则可以将想法转至天蓝色函数feedback页面。

© www.soinside.com 2019 - 2024. All rights reserved.