forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdapterWithErrorHandler.cs
74 lines (64 loc) · 3.57 KB
/
AdapterWithErrorHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.TraceExtensions;
using Microsoft.BotBuilderSamples.Translation;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Extensions.Logging;
namespace Microsoft.BotBuilderSamples
{
public class AdapterWithErrorHandler : CloudAdapter
{
public AdapterWithErrorHandler(BotFrameworkAuthentication auth, ILogger<IBotFrameworkHttpAdapter> logger, TranslationMiddleware translationMiddleware, ConversationState conversationState = default)
: base(auth, logger)
{
if (translationMiddleware == null)
{
throw new NullReferenceException(nameof(translationMiddleware));
}
// Add translation middleware to the adapter's middleware pipeline
Use(translationMiddleware);
OnTurnError = async (turnContext, exception) =>
{
// Log any leaked exception from the application.
// NOTE: In production environment, you should consider logging this to
// Azure Application Insights. Visit https://aka.ms/bottelemetry to see how
// to add telemetry capture to your bot.
logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");
// Send a message to the user
await SendWithoutMiddleware(turnContext, "The bot encountered an error or bug.");
await SendWithoutMiddleware(turnContext, "To continue to run this bot, please fix the bot source code.");
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");
};
}
private static async Task SendWithoutMiddleware(ITurnContext turnContext, string message)
{
// Sending the Activity directly through the Adapter rather than through the TurnContext skips the middleware processing
// this might be important in this particular case because it might have been the TranslationMiddleware that is actually failing!
var activity = MessageFactory.Text(message);
// If we are skipping the TurnContext we must address the Activity manually here before sending it.
activity.ApplyConversationReference(turnContext.Activity.GetConversationReference());
// Send the actual Activity through the Adapter.
await turnContext.Adapter.SendActivitiesAsync(turnContext, new[] { activity }, CancellationToken.None);
}
}
}