Created
September 2, 2016 15:46
-
-
Save qswinson/39a36e485df488b710f8a9ba224627af to your computer and use it in GitHub Desktop.
Configurable Exception Handling Dialog for Microsoft BotBuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Bot.Builder.Dialogs; | |
using System; | |
using System.Threading.Tasks; | |
namespace ChatBot.Dialogs | |
{ | |
[Serializable] | |
public class ExceptionHandlerDialog<T> : IDialog<T> | |
{ | |
private readonly Func<IDialog<T>> _makeDialog; | |
private readonly bool _displayException; | |
private readonly int _stackTraceLength; | |
public ExceptionHandlerDialog(Func<IDialog<T>> makeDialog, bool displayException, int stackTraceLength = 500) | |
{ | |
_makeDialog = makeDialog; | |
_displayException = displayException; | |
_stackTraceLength = stackTraceLength; | |
} | |
public async Task StartAsync(IDialogContext context) | |
{ | |
var dialog = _makeDialog(); | |
try | |
{ | |
context.Call<T>(dialog, ResumeAsync); | |
} | |
catch (Exception e) | |
{ | |
if (_displayException) | |
await DisplayException(context, e); | |
} | |
} | |
private async Task ResumeAsync(IDialogContext context, IAwaitable<T> result) | |
{ | |
try | |
{ | |
context.Done<T>(await result); | |
} | |
catch (Exception e) | |
{ | |
if (_displayException) | |
await DisplayException(context, e); | |
} | |
} | |
private async Task DisplayException(IDialogContext context, Exception e) | |
{ | |
var stackTrace = e.StackTrace; | |
if (stackTrace.Length > _stackTraceLength) | |
stackTrace = stackTrace.Substring(0, _stackTraceLength) + "…"; | |
stackTrace = stackTrace.Replace(Environment.NewLine, " \n"); | |
var message = e.Message.Replace(Environment.NewLine, " \n"); | |
var exceptionStr = $"**{message}** \n\n{stackTrace}"; | |
await context.PostAsync(exceptionStr); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment