@@ -144,54 +144,88 @@ def update_tennant_jwt(self, tennant_jwt):
144
144
145
145
146
146
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." )
154
161
155
- for listener in listeners :
156
- self .add_listener (listener )
157
162
158
163
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
+
160
170
161
171
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
+
166
181
167
182
def remove_all_listeners (self , topic : str = None ):
168
183
# Note advanced use of function can include both listenerFilter and topicFilter for this
169
184
# 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
+
171
191
172
192
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 )
173
196
try :
174
- app = web .Application ()
175
- app .add_routes ([web .post (self .webhook_base + "/topic/{topic}/" , self ._receive_webhook )])
176
- runner = web .AppRunner (app )
177
197
await runner .setup ()
178
198
self .webhook_site = web .TCPSite (runner , self .webhook_host , self .webhook_port )
179
199
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
+
182
204
183
205
async def _receive_webhook (self , request : ClientRequest ):
184
206
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
+
188
214
189
215
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
+
193
223
194
224
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." )
0 commit comments