title: How can I change the default "Sorry, my bot code is having an issue" exception message?
body: <p>I need to change the default "Sorry, my bot code is having an issue" exception message. It seems like it is a somewhat complicated process. I have tried to do as it says in this blog post: <a href="http://wp.sjkp.dk/change-the-sorry-my-bot-code-is-having-an-issue-in-microsoft-bot-framework/" rel="nofollow noreferrer">http://wp.sjkp.dk/change-the-sorry-my-bot-code-is-having-an-issue-in-microsoft-bot-framework/</a></p>
  
  <p>I don't have knowledge of dependency injection or inversion of control, so this is becoming quite a challenge. I'm using the Bot Builder version 3.5.5.</p>
  
  <p>This is the code from the blog, which I tried to use on my bot:</p>
  
  <p>The PostUnhandledExceptionToUser class:</p>
  
  <pre><code>using Microsoft.Bot.Builder.Dialogs;
  using Microsoft.Bot.Builder.Dialogs.Internals;
  using Microsoft.Bot.Builder.Internals.Fibers;
  using Microsoft.Bot.Connector;
  using System;
  using System.Diagnostics;
  using System.Net.Mime;
  using System.Resources;
  using System.Threading;
  using System.Threading.Tasks;
  
  namespace Tiimo.Bot.BotFramework
  {
      public class PostUnhandledExceptionToUser : IPostToBot
      {
          private readonly ResourceManager resources;
          private readonly IPostToBot inner;
          private readonly IBotToUser botToUser;
          private readonly TraceListener trace;
  
          public PostUnhandledExceptionToUser(IPostToBot inner, IBotToUser botToUser, ResourceManager resources, TraceListener trace)
          {
              SetField.NotNull(out this.inner, nameof(inner), inner);
              SetField.NotNull(out this.botToUser, nameof(botToUser), botToUser);
              SetField.NotNull(out this.resources, nameof(resources), resources);
              SetField.NotNull(out this.trace, nameof(trace), trace);
          }
  
          async Task IPostToBot.PostAsync(IActivity activity, CancellationToken token)
          {
              try
              {
                  await this.inner.PostAsync(activity, token);
              }
              catch (Exception error)
              {
                  try
                  {
                      if (Debugger.IsAttached)
                      {
                          var message = this.botToUser.MakeMessage();
                          message.Text = $"Exception: { error.Message}";
                          message.Attachments = new[]
                          {
                              new Attachment(contentType: MediaTypeNames.Text.Plain, content: error.StackTrace)
                          };
  
                          await this.botToUser.PostAsync(message);
                      }
                      else
                      {
                          await this.botToUser.PostAsync("My Personal Error Message");
                      }
                  }
                  catch (Exception inner)
                  {
                      this.trace.WriteLine(inner);
                  }
  
                  throw;
              }
          }
      }
  }
  </code></pre>
  
  <p>The DefaultExceptionMessageOverrideModule class:</p>
  
  <pre><code>using Autofac;
  using Autofac.Builder;
  using Microsoft.Bot.Builder.Dialogs;
  using Microsoft.Bot.Builder.Dialogs.Internals;
  using Microsoft.Bot.Builder.History;
  using Microsoft.Bot.Builder.Internals.Fibers;
  using Microsoft.Bot.Builder.Scorables.Internals;
  using Microsoft.Bot.Connector;
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Resources;
  using System.Web;
  
  
  namespace Tiimo.Bot.BotFramework
  {
      public class DefaultExceptionMessageOverrideModule : Module
      {
          protected override void Load(ContainerBuilder builder)
          {
              builder.RegisterType&lt;PostUnhandledExceptionToUser&gt;().Keyed&lt;IPostToBot&gt;(typeof(PostUnhandledExceptionToUser)).InstancePerLifetimeScope();
  
  
              RegisterAdapterChain&lt;IPostToBot&gt;(builder, 
                  typeof(PersistentDialogTask),
                  typeof(ExceptionTranslationDialogTask),
                  typeof(SerializeByConversation),
                  typeof(SetAmbientThreadCulture),
                  typeof(PostUnhandledExceptionToUser),
                  typeof(LogPostToBot)
              )
              .InstancePerLifetimeScope();
          }
  
          public static IRegistrationBuilder&lt;TLimit, SimpleActivatorData, SingleRegistrationStyle&gt; RegisterAdapterChain&lt;TLimit&gt;(ContainerBuilder builder, params Type[] types)
          {
              return
                  builder
                  .Register(c =&gt;
                  {
                  // http://stackoverflow.com/questions/23406641/how-to-mix-decorators-in-autofac
                  TLimit service = default(TLimit);
                      for (int index = 0; index &lt; types.Length; ++index)
                      {
                      // resolve the keyed adapter, passing the previous service as the inner parameter
                      service = c.ResolveKeyed&lt;TLimit&gt;(types[index], TypedParameter.From(service));
                      }
  
                      return service;
                  })
                  .As&lt;TLimit&gt;();
          }
      }  
  }
  </code></pre>
  
  <p>After creating these classes I went to the Global.asax file and put this code:</p>
  
  <pre><code>var builder = new ContainerBuilder();
  builder.RegisterModule(new DefaultExceptionMessageOverrideModule());
  builder.Update(Conversation.Container);
  </code></pre>
  
  <p>It builds successfully, however when I send a message to the bot I get the following error:</p>
  
  <blockquote>
    <p>An exception was thrown while invoking the constructor ‘Void .ctor(Microsoft.Bot.Builder.Dialogs.Internals.IPostToBot,
    Microsoft.Bot.Builder.Dialogs.Internals.IBotData)’ on type
    ‘PersistentDialogTask’. —> Valor não pode ser nulo. Nome do parâmetro:
    inner (See inner exception for details.)</p>
  </blockquote>
  
  <p>Which I guess it translates to something like</p>
  
  <blockquote>
    <p>An exception was thrown while invoking the constructor ‘Void
    .ctor(Microsoft.Bot.Builder.Dialogs.Internals.IPostToBot,
    Microsoft.Bot.Builder.Dialogs.Internals.IBotData)’ on type
    ‘PersistentDialogTask’. —> Value cannot be null. Parameter name: inner
    (See inner exception for details.)</p>
  </blockquote>
  
  <p>Is there something wrong with the code? What am I doing wrong? Is there another way to do this?</p>