Error handling in Bot Framework dialogs

纵然是瞬间 提交于 2019-12-13 02:32:20

问题


How can I catch all exceptions in dialogs? Is there something like ASP.NET Exception Filter? I want to send different messages to user depending on exception type.

Thank you


回答1:


You are right about the fact that you can use ExceptionFilter.

You just have to do the following:

Create your ExceptionFilter class, for example to force the tracking of the exception in Application Insights (or in your case handle specific exception types):

using Microsoft.ApplicationInsights;
using System.Net.Http;
using System.Web.Http.Filters;

namespace BotDemo.App_Start
{
    public class ExceptionFilter : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext ctx)
        {
            HandleError(ctx);
        }

        private static void HandleError(HttpActionExecutedContext ctx)
        {
            ctx.Response = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError)
            {
                Content = new StringContent(ctx.Exception.Message)
            };

            var client = new TelemetryClient();
            client.TrackException(ctx.Exception);
        }
    }
}

Don't forget to define your exception filter in your Application_Start():

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configuration.Filters.Add(new ExceptionFilter());
         ...

That's it.

In fact Bot Framework template is using ASP.Net, so you have all the normal features.



来源:https://stackoverflow.com/questions/45068972/error-handling-in-bot-framework-dialogs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!