66import com .microsoft .bot .builder .ConversationState ;
77
88import java .util .concurrent .CompletableFuture ;
9+
10+ import com .microsoft .bot .builder .MessageFactory ;
11+ import com .microsoft .bot .builder .TurnContext ;
12+ import com .microsoft .bot .connector .Channels ;
13+ import com .microsoft .bot .schema .Activity ;
14+ import com .microsoft .bot .schema .ActivityTypes ;
15+ import org .apache .commons .lang3 .StringUtils ;
16+ import org .apache .commons .lang3 .exception .ExceptionUtils ;
917import org .slf4j .LoggerFactory ;
1018
1119/**
1220 * An Adapter that provides exception handling.
1321 */
1422public class AdapterWithErrorHandler extends BotFrameworkHttpAdapter {
15- private static final String ERROR_MSG = "Bot Framework encountered an error" ;
23+ private static final String ERROR_MSG_ONE = "The bot encountered an error or bug." ;
24+ private static final String ERROR_MSG_TWO = "To continue to run this bot, please fix the bot source code." ;
1625
1726 /**
1827 * Constructs an error handling BotFrameworkHttpAdapter by providing
@@ -28,8 +37,10 @@ public AdapterWithErrorHandler(Configuration withConfiguration) {
2837
2938 setOnTurnError ((turnContext , exception ) -> {
3039 LoggerFactory .getLogger (AdapterWithErrorHandler .class ).error ("onTurnError" , exception );
31- return turnContext .sendActivity (ERROR_MSG + ": " + exception .getLocalizedMessage ())
32- .thenApply (resourceResponse -> null );
40+
41+ return turnContext
42+ .sendActivities (MessageFactory .text (ERROR_MSG_ONE ), MessageFactory .text (ERROR_MSG_TWO ))
43+ .thenCompose (resourceResponse -> sendTraceActivity (turnContext , exception ));
3344 });
3445 }
3546
@@ -48,7 +59,9 @@ public AdapterWithErrorHandler(Configuration withConfiguration, ConversationStat
4859
4960 setOnTurnError ((turnContext , exception ) -> {
5061 LoggerFactory .getLogger (AdapterWithErrorHandler .class ).error ("onTurnError" , exception );
51- return turnContext .sendActivity (ERROR_MSG + ": " + exception .getLocalizedMessage ())
62+
63+ return turnContext
64+ .sendActivities (MessageFactory .text (ERROR_MSG_ONE ), MessageFactory .text (ERROR_MSG_TWO ))
5265 .thenCompose (resourceResponse -> {
5366 if (withConversationState != null ) {
5467 // Delete the conversationState for the current conversation to prevent the
@@ -62,7 +75,24 @@ public AdapterWithErrorHandler(Configuration withConfiguration, ConversationStat
6275 });
6376 }
6477 return CompletableFuture .completedFuture (null );
65- });
78+ })
79+ .thenCompose (stageResult -> sendTraceActivity (turnContext , exception ));
6680 });
6781 }
82+
83+ private CompletableFuture <Void > sendTraceActivity (TurnContext turnContext , Throwable exception ) {
84+ if (StringUtils .equals (turnContext .getActivity ().getChannelId (), Channels .EMULATOR )) {
85+ Activity traceActivity = new Activity (ActivityTypes .TRACE ) {{
86+ setLabel ("TurnError" );
87+ setName ("OnTurnError Trace" );
88+ setValue (ExceptionUtils .getStackTrace (exception ));
89+ setValueType ("https://www.botframework.com/schemas/error" );
90+ }};
91+
92+ return turnContext .sendActivity (traceActivity )
93+ .thenApply (resourceResponse -> null );
94+ }
95+
96+ return CompletableFuture .completedFuture (null );
97+ }
6898}
0 commit comments