从Azure函数HttpTrigger返回错误详细信息

编程入门 行业动态 更新时间:2024-10-11 11:20:30
本文介绍了从Azure函数HttpTrigger返回错误详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用HTTP触发器创建了一个Azure函数.我希望将详细的错误信息返回给调用者.有没有办法解决未捕获的异常?奇怪的是,Azure Functions在Visual Studio中运行时会返回详细的错误信息,但在部署时却不会返回.

I've created an Azure Function with an HTTP trigger. I would like detailed error information to be returned to callers. Is there a way of doing for uncaught exceptions? Strangely, Azure Functions does return the detailed error info when running in Visual Studio, but not when deployed.

[FunctionName("MyAzureFunction")] public static async Task<HttpResponseMessage> RunAsync( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage httpRequest, TraceWriter traceWriter, CancellationToken cancellationToken) { try { var response = await ProcessAsync(request, cancellationToken); return httpRequest.CreateResponse(HttpStatusCode.OK, response); } catch (ArgumentException ex) { traceWriter.LogWarning($"Argument error: {ex}"); return httpRequest.CreateResponse(HttpStatusCode.BadRequest, ex.Message); } catch (Exception ex) { traceWriter.LogError($"Error: {ex}"); throw; } }

推荐答案

通常,从安全角度来看,将异常详细信息返回给外部调用者被认为是不好的做法.因此,默认情况下可以屏蔽此信息.

Generally, returning exception details to the external caller is considered bad practice from security standpoint and otherwise. Because of that, it makes sense to block this information by default.

我将throw;语句替换为返回手动格式化错误的语句.如果您可以将异常消息公开给调用者,那么就很简单

I would replace throw; statement with the one returning manually formatted error. If you are OK with exposing the exception message to the caller, it's as simple as

return httpRequest.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);

如果调用方是外部的,我也不会这样做:相反,返回一般错误消息,然后依靠日志进行调试.

If the caller is external, again, I would not do that: instead, return a generic error message and then rely on logging for debugging.

更多推荐

从Azure函数HttpTrigger返回错误详细信息

本文发布于:2023-11-08 06:15:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568568.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:详细信息   函数   错误   Azure   HttpTrigger

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!