5
5
)
6
6
from dataclasses import dataclass
7
7
from pubsub import pub
8
+ import sys
8
9
9
10
from .controllers .connections import ConnectionsController
10
11
from .controllers .messaging import MessagingController
@@ -191,16 +192,21 @@ def register_listeners(self, listeners, defaults=True):
191
192
Whether to connect to the default handlers for connections, basicmessage and present_proof
192
193
(default is True)
193
194
"""
194
- if defaults :
195
- if self .connections :
196
- pub .subscribe (self .connections .default_handler , "connections" )
197
- if self .messaging :
198
- pub .subscribe (self .messaging .default_handler , "basicmessages" )
199
- if self .proofs :
200
- pub .subscribe (self .proofs .default_handler , "present_proof" )
195
+ try :
196
+ if defaults :
197
+ if self .connections :
198
+ pub .subscribe (self .connections .default_handler , "connections" )
199
+ if self .messaging :
200
+ pub .subscribe (self .messaging .default_handler , "basicmessages" )
201
+ if self .proofs :
202
+ pub .subscribe (self .proofs .default_handler , "present_proof" )
203
+
204
+ for listener in listeners :
205
+ self .add_listener (listener )
206
+ except Exception as exc :
207
+ print (f"Register webhooks listeners failed! { exc !r} occurred." )
208
+ logger .warn (f"Register webhooks listeners failed! { exc !r} occurred." )
201
209
202
- for listener in listeners :
203
- self .add_listener (listener )
204
210
205
211
206
212
def add_listener (self , listener ):
@@ -211,7 +217,12 @@ def add_listener(self, listener):
211
217
listener : dict
212
218
A dictionary comprised of a "handler": handler (fct) and a "topic":"topicname" key-value pairs
213
219
"""
214
- pub .subscribe (listener ["handler" ], listener ["topic" ])
220
+ try :
221
+ pub .subscribe (listener ["handler" ], listener ["topic" ])
222
+ except Exception as exc :
223
+ print (f"Adding webhooks listener failed! { exc !r} occurred." )
224
+ logger .warn (f"Adding webhooks listener failed! { exc !r} occurred." )
225
+
215
226
216
227
217
228
def remove_listener (self , listener ):
@@ -222,10 +233,15 @@ def remove_listener(self, listener):
222
233
listener : dict
223
234
A dictionary comprised of a "handler": handler (fct) and a "topic":"topicname" key-value pairs
224
235
"""
225
- if pub .isSubscribed (listener ["handler" ], listener ["topic" ]):
226
- pub .unsubscribe (listener ["handler" ], listener ["topic" ])
227
- else :
228
- logger .debug ("Listener not subscribed" , listener )
236
+ try :
237
+ if pub .isSubscribed (listener ["handler" ], listener ["topic" ]):
238
+ pub .unsubscribe (listener ["handler" ], listener ["topic" ])
239
+ else :
240
+ logger .debug ("Listener not subscribed" , listener )
241
+ except Exception as exc :
242
+ print (f"Removing webhooks listener failed! { exc !r} occurred." )
243
+ logger .warn (f"Removing webhooks listener failed! { exc !r} occurred." )
244
+
229
245
230
246
231
247
def remove_all_listeners (self , topic : str = None ):
@@ -238,17 +254,27 @@ def remove_all_listeners(self, topic: str = None):
238
254
"""
239
255
# Note advanced use of function can include both listenerFilter and topicFilter for this
240
256
# Add when needed
241
- pub .unsubAll (topicName = topic )
257
+ try :
258
+ pub .unsubAll (topicName = topic )
259
+ except Exception as exc :
260
+ print (f"Removing all webhooks listeners failed! { exc !r} occurred." )
261
+ logger .warn (f"Removing all webhooks listeners failed! { exc !r} occurred." )
262
+
242
263
243
264
244
265
async def listen_webhooks (self ):
245
266
"""Create a server to listen to webhooks"""
246
- app = web .Application ()
247
- app .add_routes ([web .post (self .webhook_base + "/topic/{topic}/" , self ._receive_webhook )])
248
- runner = web .AppRunner (app )
249
- await runner .setup ()
250
- self .webhook_site = web .TCPSite (runner , self .webhook_host , self .webhook_port )
251
- await self .webhook_site .start ()
267
+ try :
268
+ app = web .Application ()
269
+ app .add_routes ([web .post (self .webhook_base + "/topic/{topic}/" , self ._receive_webhook )])
270
+ runner = web .AppRunner (app )
271
+ await runner .setup ()
272
+ self .webhook_site = web .TCPSite (runner , self .webhook_host , self .webhook_port )
273
+ await self .webhook_site .start ()
274
+ except Exception as exc :
275
+ print (f"Listening webhooks failed! { exc !r} occurred." )
276
+ logger .warn (f"Listening webhooks failed! { exc !r} occurred." )
277
+
252
278
253
279
254
280
async def _receive_webhook (self , request : ClientRequest ):
@@ -265,9 +291,13 @@ async def _receive_webhook(self, request: ClientRequest):
265
291
A response with status 200
266
292
"""
267
293
topic = request .match_info ["topic" ]
268
- payload = await request .json ()
269
- await self ._handle_webhook (topic , payload )
270
- return web .Response (status = 200 )
294
+ try :
295
+ payload = await request .json ()
296
+ await self ._handle_webhook (topic , payload )
297
+ return web .Response (status = 200 )
298
+ except Exception as exc :
299
+ logger .warn (f"Receiving webhooks failed! { exc !r} occurred." )
300
+
271
301
272
302
273
303
async def _handle_webhook (self , topic , payload ):
@@ -280,13 +310,21 @@ async def _handle_webhook(self, topic, payload):
280
310
payload : dict
281
311
A JSON-like dictionary representation of the payload
282
312
"""
283
- logging .debug (f"Handle Webhook - { topic } " , payload )
284
- pub .sendMessage (topic , payload = payload )
285
- # return web.Response(status=200)
313
+ try :
314
+ logging .debug (f"Handle Webhook - { topic } " , payload )
315
+ pub .sendMessage (topic , payload = payload )
316
+ # return web.Response(status=200)
317
+ except Exception as exc :
318
+ logger .warn (f"Handling webhooks failed! { exc !r} occurred when trying to handle this topic: { topic } " )
319
+
286
320
287
321
288
322
async def terminate (self ):
289
323
"""Terminate the controller client session and webhook listeners"""
290
- await self .client_session .close ()
291
- if self .webhook_site :
292
- await self .webhook_site .stop ()
324
+ try :
325
+ await self .client_session .close ()
326
+ if self .webhook_site :
327
+ await self .webhook_site .stop ()
328
+ except Exception as exc :
329
+ print (f"Terminating webhooks listener failed! { exc !r} occurred." )
330
+ logger .warn (f"Terminating webhooks listener failed! { exc !r} occurred." )
0 commit comments