如何使用自定义错误响应在 azure 函数 http 触发器中触发失败

How to trigger a failure in an azure function http trigger, WITH a custom error response

我找不到使对 nodejs azure 函数的 http 调用失败并包含自定义错误响应的方法。

调用 context.done() 允许自定义响应(但不会在 Application Insights 中指示为失败)

调用 context.done(true, XXX) 确实会导致失败,但 returns 对用户来说是一个一般性错误(无论我在 XXX 中输入什么):

{"id":"b6ca6fb0-686a-4a9c-8c66-356b6db51848","requestId":"7599d94b-d3f2-48fe-80cd-e067bb1c8557","statusCode":500,"errorCode":0,"message":"An error has occurred. For more information, please check the logs for error ID b6ca6fb0-686a-4a9c-8c66-356b6db51848"}

这只是我 运行 在尝试在 Azure 函数上获得快速网络 api 运行 时遇到的最新麻烦。如果你不能跟踪错误,那么它就不应该被称为 "Application Insights"。有任何想法吗?

为什么不使用 context.res 来 return HTTP 触发函数的客户响应?

成功将是真实的,但 resultCode 将被设置为您的值。

尝试这样的 AppInsights 查询:

// Get all errors
requests
| where toint(resultCode) >= 400
| limit 10

[更新] Requests 中的 Id 值是 'function instance id',它唯一标识该调用。

还有一个 'traces' table 包含来自您的 azure 函数的日志消息。您可以通过 operation_Id 在请求和跟踪之间加入。

requests 
| where toint(resultCode) >= 400
| take 10 
| join (traces) on operation_Id 
| project id, message, operation_Id

响应正文不会自动记录到 AppInsights。您需要添加一些明确的日志语句来捕获它。