Skip to content

Commit 5193361

Browse files
tracyboehreraxelsrz
authored andcommitted
Refactored to unbound on_error methods when accessing outer app.py va… (#385)
* Refactored to unbound on_error methods when accessing outer app.py variables. * Removed unused imports
1 parent a1d9887 commit 5193361

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

samples/05.multi-turn-prompt/app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import asyncio
55
import sys
66
from datetime import datetime
7-
from types import MethodType
87

98
from flask import Flask, request, Response
109
from botbuilder.core import (
@@ -59,7 +58,9 @@ async def on_error(context: TurnContext, error: Exception):
5958
# Clear out state
6059
await CONVERSATION_STATE.delete(context)
6160

62-
ADAPTER.on_turn_error = MethodType(on_error, ADAPTER)
61+
# Set the error handler on the Adapter.
62+
# In this case, we want an unbound method, so MethodType is not needed.
63+
ADAPTER.on_turn_error = on_error
6364

6465
# Create MemoryStorage, UserState and ConversationState
6566
MEMORY = MemoryStorage()

samples/06.using-cards/app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import asyncio
88
import sys
99
from datetime import datetime
10-
from types import MethodType
1110

1211
from flask import Flask, request, Response
1312
from botbuilder.core import (
@@ -35,7 +34,7 @@
3534

3635

3736
# Catch-all for errors.
38-
async def on_error(self, context: TurnContext, error: Exception):
37+
async def on_error(context: TurnContext, error: Exception):
3938
# This check writes out errors to console log .vs. app insights.
4039
# NOTE: In production environment, you should consider logging this to Azure
4140
# application insights.
@@ -61,7 +60,9 @@ async def on_error(self, context: TurnContext, error: Exception):
6160
# Clear out state
6261
await CONVERSATION_STATE.delete(context)
6362

64-
ADAPTER.on_turn_error = MethodType(on_error, ADAPTER)
63+
# Set the error handler on the Adapter.
64+
# In this case, we want an unbound method, so MethodType is not needed.
65+
ADAPTER.on_turn_error = on_error
6566

6667
# Create MemoryStorage, UserState and ConversationState
6768
MEMORY = MemoryStorage()

samples/21.corebot-app-insights/app.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import asyncio
1515
import sys
1616
from datetime import datetime
17-
from types import MethodType
1817

1918
from flask import Flask, request, Response
2019
from botbuilder.core import (
@@ -45,7 +44,7 @@
4544

4645

4746
# Catch-all for errors.
48-
async def on_error(self, context: TurnContext, error: Exception):
47+
async def on_error(context: TurnContext, error: Exception):
4948
# This check writes out errors to console log .vs. app insights.
5049
# NOTE: In production environment, you should consider logging this to Azure
5150
# application insights.
@@ -69,10 +68,11 @@ async def on_error(self, context: TurnContext, error: Exception):
6968
await context.send_activity(trace_activity)
7069

7170
# Clear out state
72-
nonlocal self
7371
await CONVERSATION_STATE.delete(context)
7472

75-
ADAPTER.on_turn_error = MethodType(on_error, ADAPTER)
73+
# Set the error handler on the Adapter.
74+
# In this case, we want an unbound method, so MethodType is not needed.
75+
ADAPTER.on_turn_error = on_error
7676

7777
# Create MemoryStorage, UserState and ConversationState
7878
MEMORY = MemoryStorage()

samples/44.prompt-users-for-input/app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import asyncio
55
import sys
66
from datetime import datetime
7-
from types import MethodType
87

98
from flask import Flask, request, Response
109
from botbuilder.core import (
@@ -31,7 +30,7 @@
3130

3231

3332
# Catch-all for errors.
34-
async def on_error(self, context: TurnContext, error: Exception):
33+
async def on_error(context: TurnContext, error: Exception):
3534
# This check writes out errors to console log .vs. app insights.
3635
# NOTE: In production environment, you should consider logging this to Azure
3736
# application insights.
@@ -57,7 +56,9 @@ async def on_error(self, context: TurnContext, error: Exception):
5756
# Clear out state
5857
await CONVERSATION_STATE.delete(context)
5958

60-
ADAPTER.on_turn_error = MethodType(on_error, ADAPTER)
59+
# Set the error handler on the Adapter.
60+
# In this case, we want an unbound method, so MethodType is not needed.
61+
ADAPTER.on_turn_error = on_error
6162

6263
# Create MemoryStorage and state
6364
MEMORY = MemoryStorage()

samples/45.state-management/app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import asyncio
55
import sys
66
from datetime import datetime
7-
from types import MethodType
87

98
from flask import Flask, request, Response
109
from botbuilder.core import (
@@ -31,7 +30,7 @@
3130

3231

3332
# Catch-all for errors.
34-
async def on_error(self, context: TurnContext, error: Exception):
33+
async def on_error(context: TurnContext, error: Exception):
3534
# This check writes out errors to console log .vs. app insights.
3635
# NOTE: In production environment, you should consider logging this to Azure
3736
# application insights.
@@ -57,7 +56,9 @@ async def on_error(self, context: TurnContext, error: Exception):
5756
# Clear out state
5857
await CONVERSATION_STATE.delete(context)
5958

60-
ADAPTER.on_turn_error = MethodType(on_error, ADAPTER)
59+
# Set the error handler on the Adapter.
60+
# In this case, we want an unbound method, so MethodType is not needed.
61+
ADAPTER.on_turn_error = on_error
6162

6263
# Create MemoryStorage and state
6364
MEMORY = MemoryStorage()

0 commit comments

Comments
 (0)