Skip to content

Commit 4c83eab

Browse files
committed
Add try except to webhook listeners and handlers.
* For private methods catch exception and use the logger to log the Exception * For public methods use the logger and print to stdout
1 parent b8484d2 commit 4c83eab

File tree

3 files changed

+65
-31
lines changed

3 files changed

+65
-31
lines changed

libs/aries-basic-controller/aries_basic_controller/aries_controller.py

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -144,54 +144,88 @@ def update_tennant_jwt(self, tennant_jwt):
144144

145145

146146
def register_listeners(self, listeners, defaults=True):
147-
if defaults:
148-
if self.connections:
149-
pub.subscribe(self.connections.default_handler, "connections")
150-
if self.messaging:
151-
pub.subscribe(self.messaging.default_handler, "basicmessages")
152-
if self.proofs:
153-
pub.subscribe(self.proofs.default_handler, "present_proof")
147+
try:
148+
if defaults:
149+
if self.connections:
150+
pub.subscribe(self.connections.default_handler, "connections")
151+
if self.messaging:
152+
pub.subscribe(self.messaging.default_handler, "basicmessages")
153+
if self.proofs:
154+
pub.subscribe(self.proofs.default_handler, "present_proof")
155+
156+
for listener in listeners:
157+
self.add_listener(listener)
158+
except Exception as exc:
159+
print(f"Register webhooks listeners failed! {exc!r} occurred.")
160+
logger.debug(f"Register webhooks listeners failed! {exc!r} occurred.")
154161

155-
for listener in listeners:
156-
self.add_listener(listener)
157162

158163
def add_listener(self, listener):
159-
pub.subscribe(listener["handler"], listener["topic"])
164+
try:
165+
pub.subscribe(listener["handler"], listener["topic"])
166+
except Exception as exc:
167+
print(f"Adding webhooks listener failed! {exc!r} occurred.")
168+
logger.debug(f"Adding webhooks listener failed! {exc!r} occurred.")
169+
160170

161171
def remove_listener(self, listener):
162-
if pub.isSubscribed(listener["handler"], listener["topic"]):
163-
pub.unsubscribe(listener["handler"], listener["topic"])
164-
else:
165-
logger.debug("Listener not subscribed", listener)
172+
try:
173+
if pub.isSubscribed(listener["handler"], listener["topic"]):
174+
pub.unsubscribe(listener["handler"], listener["topic"])
175+
else:
176+
logger.debug("Listener not subscribed", listener)
177+
except Exception as exc:
178+
print(f"Removing webhooks listener failed! {exc!r} occurred.")
179+
logger.debug(f"Removing webhooks listener failed! {exc!r} occurred.")
180+
166181

167182
def remove_all_listeners(self, topic: str = None):
168183
# Note advanced use of function can include both listenerFilter and topicFilter for this
169184
# Add when needed
170-
pub.unsubAll(topicName=topic)
185+
try:
186+
pub.unsubAll(topicName=topic)
187+
except Exception as exc:
188+
print(f"Removing all webhooks listeners failed! {exc!r} occurred.")
189+
logger.debug(f"Removing all webhooks listeners failed! {exc!r} occurred.")
190+
171191

172192
async def listen_webhooks(self):
193+
app = web.Application()
194+
app.add_routes([web.post(self.webhook_base + "/topic/{topic}/", self._receive_webhook)])
195+
runner = web.AppRunner(app)
173196
try:
174-
app = web.Application()
175-
app.add_routes([web.post(self.webhook_base + "/topic/{topic}/", self._receive_webhook)])
176-
runner = web.AppRunner(app)
177197
await runner.setup()
178198
self.webhook_site = web.TCPSite(runner, self.webhook_host, self.webhook_port)
179199
await self.webhook_site.start()
180-
except:
181-
print("Listening webhooks failed!", sys.exc_info()[0], "occurred.")
200+
except Exception as exc:
201+
print(f"Listening webhooks failed! {exc!r} occurred.")
202+
logger.debug(f"Listening webhooks failed! {exc!r} occurred.")
203+
182204

183205
async def _receive_webhook(self, request: ClientRequest):
184206
topic = request.match_info["topic"]
185-
payload = await request.json()
186-
await self._handle_webhook(topic, payload)
187-
return web.Response(status=200)
207+
try:
208+
payload = await request.json()
209+
await self._handle_webhook(topic, payload)
210+
return web.Response(status=200)
211+
except Exception as exc:
212+
logger.debug(f"Receiving webhooks failed! {exc!r} occurred.")
213+
188214

189215
async def _handle_webhook(self, topic, payload):
190-
logging.debug(f"Handle Webhook - {topic}", payload)
191-
pub.sendMessage(topic, payload=payload)
192-
# return web.Response(status=200)
216+
try:
217+
logging.debug(f"Handle Webhook - {topic}", payload)
218+
pub.sendMessage(topic, payload=payload)
219+
# return web.Response(status=200)
220+
except Exception as exc:
221+
logger.debug(f"Handling webhooks failed! {exc!r} occurred.")
222+
193223

194224
async def terminate(self):
195-
await self.client_session.close()
196-
if self.webhook_site:
197-
await self.webhook_site.stop()
225+
try:
226+
await self.client_session.close()
227+
if self.webhook_site:
228+
await self.webhook_site.stop()
229+
except Exception as exc:
230+
print(f"Terminating webhooks listener failed! {exc!r} occurred.")
231+
logger.debug(f"Terminating webhooks listener failed! {exc!r} occurred.")

tutorials/1. Learning Aries, ACA-Py and the Basic Controller/notebooks/alice/3 Messages/Part 1 - Basic Message.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"metadata": {},
122122
"outputs": [],
123123
"source": [
124-
"await agent_controller.messaging.trust_ping(id)"
124+
"await agent_controller.messaging.trust_ping(id, \"hello, world!\")"
125125
]
126126
},
127127
{

tutorials/1. Learning Aries, ACA-Py and the Basic Controller/notebooks/bob/3 Messages/Part 1 - Basic Message.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
"name": "python",
167167
"nbconvert_exporter": "python",
168168
"pygments_lexer": "ipython3",
169-
"version": "3.7.6"
169+
"version": "3.8.5"
170170
}
171171
},
172172
"nbformat": 4,

0 commit comments

Comments
 (0)