Skip to content

Instantly share code, notes, and snippets.

@qswinson
Created September 2, 2016 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qswinson/39a36e485df488b710f8a9ba224627af to your computer and use it in GitHub Desktop.
Save qswinson/39a36e485df488b710f8a9ba224627af to your computer and use it in GitHub Desktop.
Configurable Exception Handling Dialog for Microsoft BotBuilder
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