Azure 函式錯誤處理Azure Functions error handling

本主題提供您的函式在執行時所發生錯誤的處理之一般指引。This topic provides general guidance for handling errors that occur when your functions execute. 它也提供描述,可能會發生的繫結特定錯誤之主題的連結。It also provides links to the topics that describe binding-specific errors that may occur.

處理函數中的錯誤Handling errors in functions

Azure Functions 觸發程序和繫結會與各種 Azure 服務通訊。Azure Functions triggers and bindings communicate with various Azure services. 在與這些服務整合時,您可能會引發錯誤,而這些錯誤是源自基礎 Azure 服務的 API。When integrating with these services, you may have errors raised that originate from the APIs of the underlying Azure services. 當您嘗試使用 REST 或用戶端程式庫,透過您的函式程式碼與其他服務通訊時,也可能會發生錯誤。Errors can also occur when you try to communicate with other services from your function code by using REST or client libraries. 若要避免遺失資料並確保函式有正常行為,請務必要處理來自上述任一來源的錯誤。To avoid loss of data and ensure good behavior of your functions, it is important to handle errors from either source.

下列觸發程序有內建的重試支援:The following triggers have built-in retry support:

根據預設,這些觸發程序最多會重試五次。By default, these triggers are retried up to five times. 在第五次重試之後,這些觸發程序就會在特殊的有害佇列中寫入一則訊息。After the fifth retry, these triggers write a message to a special poison queue.

其他 Functions 觸發程序並未內建可在函式執行期間發生錯誤時進行重試的機制。For the other Functions triggers, there is no built-in retry when errors occur during function execution. 為了避免在萬一函式發生錯誤時遺失觸發程序資訊,建議您在函式程式碼中使用 try-catch 區塊,以擷取任何錯誤。To prevent loss of trigger information should an error occur in your function, we recommend that you use try-catch blocks in your function code to catch any errors. 當錯誤發生時,請將觸發程序傳遞至函式的資訊寫入至特殊的「有害」訊息佇列。When an error occurs, write the information passed into the function by the trigger to a special "poison" message queue. 這個方法和 Blob 儲存體觸發程序所使用的方法相同。This approach is the same one used by the Blob storage trigger.

如此一來,您就可以擷取可能會因錯誤而遺失的觸發程序事件,並於稍後使用其他函式來重試這些事件,以使用所儲存的資訊處理有害佇列中的訊息。In this way, you can capture trigger events that could be lost due to errors and retry them at a later time using another function to process messages from the poison queue using the stored information.

繫結錯誤碼Binding error codes

在與 Azure 服務整合時,您可能會遇到來自基礎服務 API 發出的錯誤。When integrating with Azure services, you may have errors raised that originate from the APIs of the underlying services. 這些服務的錯誤碼文件之連結,可以在下列觸發程序和繫結參考主題的<例外狀況和傳回碼>小節中找到:Links to the error code documentation for these services can be found in the Exceptions and return codes section of the following trigger and binding reference topics: