简体   繁体   English

Azure Function 应该记录或抛出错误异常?

[英]Azure Function should log or throw exception on error?

I have Azure Function (Http trigger) that can fail.我有可能会失败的 Azure Function(Http 触发器)。 I also have Application Insights configured for it.我还为它配置了 Application Insights。

In case of error (Which is better):如果出现错误(哪个更好):

  1. Catch exception, wrap with more info and rethrow it.捕获异常,用更多信息包装并重新抛出它。 (Sends 500 response) OR (发送 500 响应)或
  2. Catch exception, log it, wrap it, rethrow it to caller.捕获异常,记录,包装,重新抛出给调用者。 (Sends 500 response) OR (发送 500 响应)或
  3. Catch exception, log it (Don't throw it), Manual send 500 response.捕获异常,记录它(不要抛出它),手动发送 500 响应。

Application Insight is able log exceptions. Application Insight 能够记录异常。 I don't really see point in logging error and throwing exception at the same time.我并没有真正看到同时记录错误和抛出异常的意义。

What are the guidelines?指导方针是什么? What is good practise?什么是好的做法?

Based on the Azure functions best practices基于Azure functions best practices

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

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

From my experience which is building an HTTP triggered azure function with Python, I noticed that: When handling exceptions and then manually return something to the client you do have control over the function returned value but the framework marks the function's job as 'Succeeded', which then makes it more difficult to be tracked & monitored in the Application Insights. From my experience which is building an HTTP triggered azure function with Python, I noticed that: When handling exceptions and then manually return something to the client you do have control over the function returned value but the framework marks the function's job as 'Succeeded',这使得在 Application Insights 中跟踪和监控变得更加困难。

For your question, I ended up with a catch clause that mixes 1 & 3 ie a hybrid approach where I try to identify the origin of the exception, then for exceptions from my code I'm wrapping it and return 500, and for other exceptions that originated from code that I'm using I re-throw them and let the azure function framework return a 500 and to mark the function's job as 'Failed'.对于您的问题,我最终得到了一个混合 1 和 3 的 catch 子句,即一种混合方法,我尝试识别异常的来源,然后对于我的代码中的异常,我将其包装并返回 500,对于其他异常源自我正在使用的代码,我重新抛出它们并让 azure function 框架返回 500 并将函数的工作标记为“失败”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM