简体   繁体   English

Bot Framework Composer 中的自定义错误消息和异常处理

[英]Custom Error Messages and Exception Handling in Bot Framework Composer

I am trying to have Custom Error messages for any any Exceptions thrown by Bot Framework Composer Bot.我正在尝试为 Bot Framework Composer Bot 抛出的任何异常设置自定义错误消息。 As the custom runtime of Composer project does not have bot.cs file, it is not possible to implement OnturnError.由于 Composer 项目的自定义运行时没有 bot.cs 文件,因此无法实现 OnturnError。 I have tried to add it in Startup.cs but it is not working.我试图将它添加到 Startup.cs 中,但它不起作用。 Is there a way I can implement this?有什么办法可以实现吗?

I have also tried adding a Error handling class inheriting from cloudAdapter and adding it as a service in Startup.cs.我还尝试添加一个错误处理 class 从 cloudAdapter 继承并将其添加为 Startup.cs 中的服务。

 public class AdapterWithErrorHandler : CloudAdapter
    {
        public AdapterWithErrorHandler(BotFrameworkAuthentication auth, ILogger<IBotFrameworkHttpAdapter> logger, ConversationState conversationState = default)
            : base(auth, logger)
        {
            OnTurnError = async (turnContext, exception) =>
            {
                // Log any leaked exception from the application.
                logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");

                // Send a message to the user
                var errorMessageText = "The bot encountered an error or bug.";
                var errorMessage = MessageFactory.Text(errorMessageText, errorMessageText, InputHints.ExpectingInput);
                await turnContext.SendActivityAsync(errorMessage);

                errorMessageText = "To continue to run this bot, please fix the bot source code.";
                errorMessage = MessageFactory.Text(errorMessageText, errorMessageText, InputHints.ExpectingInput);
                await turnContext.SendActivityAsync(errorMessage);

                if (conversationState != null)
                {
                    try
                    {
                        // Delete the conversationState for the current conversation to prevent the
                        // bot from getting stuck in a error-loop caused by being in a bad state.
                        // ConversationState should be thought of as similar to "cookie-state" in a Web pages.
                        await conversationState.DeleteAsync(turnContext);
                    }
                    catch (Exception e)
                    {
                        logger.LogError(e, $"Exception caught on attempting to Delete ConversationState : {e.Message}");
                    }
                }

                // Send a trace activity, which will be displayed in the Bot Framework Emulator
                await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
            };
        }
    }

Startup.cs启动.cs

 services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();

You can use "Error occurred event" to handle errors and custom error message您可以使用“发生错误事件”来处理错误和自定义错误消息

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

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