-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7f3f27
commit db3a172
Showing
6 changed files
with
67 additions
and
12 deletions.
There are no files selected for viewing
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
56 changes: 56 additions & 0 deletions
56
generators/python/app/templates/core/{{cookiecutter.bot_name}}/adapter_with_error_handler.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import sys | ||
import traceback | ||
from datetime import datetime | ||
|
||
from botbuilder.core import ( | ||
BotFrameworkAdapter, | ||
BotFrameworkAdapterSettings, | ||
ConversationState, | ||
TurnContext, | ||
) | ||
from botbuilder.schema import ActivityTypes, Activity | ||
|
||
|
||
class AdapterWithErrorHandler(BotFrameworkAdapter): | ||
def __init__( | ||
self, | ||
settings: BotFrameworkAdapterSettings, | ||
conversation_state: ConversationState, | ||
): | ||
super().__init__(settings) | ||
self._conversation_state = conversation_state | ||
|
||
# Catch-all for errors. | ||
async def on_error(context: TurnContext, error: Exception): | ||
# This check writes out errors to console log | ||
# NOTE: In production environment, you should consider logging this to Azure | ||
# application insights. | ||
print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) | ||
traceback.print_exc() | ||
|
||
# Send a message to the user | ||
await context.send_activity("The bot encountered an error or bug.") | ||
await context.send_activity( | ||
"To continue to run this bot, please fix the bot source code." | ||
) | ||
# Send a trace activity if we're talking to the Bot Framework Emulator | ||
if context.activity.channel_id == "emulator": | ||
# Create a trace activity that contains the error object | ||
trace_activity = Activity( | ||
label="TurnError", | ||
name="on_turn_error Trace", | ||
timestamp=datetime.utcnow(), | ||
type=ActivityTypes.trace, | ||
value=f"{error}", | ||
value_type="https://www.botframework.com/schemas/error", | ||
) | ||
# Send a trace activity, which will be displayed in Bot Framework Emulator | ||
await context.send_activity(trace_activity) | ||
|
||
# Clear out state | ||
nonlocal self | ||
await self._conversation_state.delete(context) | ||
|
||
self.on_turn_error = on_error |
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
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
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
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