Azure Functions 错误处理Azure Functions error handling

处理 Azure Functions 中的错误对于避免丢失数据、错过事件以及监视应用程序的运行状况非常重要。Handling errors in Azure Functions is important to avoid lost data, missed events, and to monitor the health of your application.

本文介绍了用于错误处理的常规策略,并提供了指向绑定特定错误的链接。This article describes general strategies for error handling along with links to binding-specific errors.

处理错误Handling errors

Azure Functions 中引发的错误可能来自以下任一来源:Errors raised in an Azure Functions can come from any of the following origins:

  • 使用内置 Azure Functions 触发器和绑定Use of built-in Azure Functions triggers and bindings
  • 调用底层 Azure 服务的 APICalls to APIs of underlying Azure services
  • 调用 REST 终结点Calls to REST endpoints
  • 调用客户端库、包或第三方 APICalls to client libraries, packages, or third-party APIs

遵循可靠的错误处理做法对于避免数据丢失或遗漏消息非常重要。Following solid error handling practices is important to avoid loss of data or missed messages. 建议的错误处理做法包括下列操作:Recommended error handling practices include the following actions:

使用结构化错误处理Use structured error handling

捕获和发布错误对于监视应用程序的运行状况至关重要。Capturing and publishing errors is critical to monitoring the health of your application. 任何函数代码的最顶层应包含 try/catch 块。The top-most level of any function code should include a try/catch block. 在 catch 块中,可以捕获并发布错误。In the catch block, you can capture and publish errors.

重试支持Retry support

以下触发器具有内置重试支持:The following triggers have built-in retry support:

默认情况下,这些触发器最多重试请求五次。By default, these triggers retry requests up to five times. 第五次重试后,Azure 队列存储和 Azure 服务总线触发器会将消息写入 有害队列After the fifth retry, both the Azure Queue storage and Azure Service Bus triggers write a message to a poison queue.

需要为任何其他触发器或绑定类型手动实现重试策略。You need to manually implement retry policies for any other triggers or bindings types. 手动实现可能包括将错误信息写入到一个有害队列Manual implementations may include writing error information to a poison message queue. 写入到有害队列后,你就有机会在稍后重试操作。By writing to a poison queue, you have the opportunity to retry operations at a later time. Blob 存储触发器也使用此方法。This approach is the same one used by the Blob storage trigger.

绑定错误代码Binding error codes

与 Azure 服务集成时,错误可能源自底层服务的 API。When integrating with Azure services, errors may originate from the APIs of the underlying services. 以下文章的异常和返回代码部分中提供了与特定于绑定的错误有关的信息:Information relating to binding-specific errors is available in the Exceptions and return codes section of the following articles: