44import platform
55import sys
66import threading
7- import traceback
7+ from traceback import TracebackException
8+ from types import TracebackType
89import uuid
910from functools import wraps
10- from threading import Lock
11- from typing import Dict , Optional
11+ from threading import ExceptHookArgs , Lock
12+ from typing import Dict , Optional , Type
1213
1314import requests
1415from mixpanel import Mixpanel
@@ -82,8 +83,8 @@ def get_dataset_properties(dataset):
8283 }
8384
8485
85- def _report_error (e , error_type = "python error" ):
86- exc = traceback . TracebackException .from_exception (e )
86+ def _report_error (e , error_type = "python error" ):
87+ exc = TracebackException .from_exception (e )
8788 is_giskard_error = False
8889 for frame in exc .stack :
8990 if not is_giskard_error and "giskard" in frame .filename :
@@ -225,7 +226,7 @@ def initialize_geo(self):
225226
226227
227228def add_exception_hook (original_hook = None ):
228- def _exception_hook (type , value , traceback ):
229+ def _exception_hook (type : Type [ BaseException ] , value : BaseException , traceback : Optional [ TracebackType ] ):
229230 try :
230231 _report_error (value )
231232 except BaseException : # noqa NOSONAR
@@ -239,8 +240,23 @@ def _exception_hook(type, value, traceback):
239240 return _exception_hook
240241
241242
243+ def add_thread_exception_hook (original_hook = None ):
244+ def _thread_exception_hook (args : ExceptHookArgs ):
245+ try :
246+ _report_error (args .exc_value )
247+ except BaseException : # noqa NOSONAR
248+ pass
249+
250+ if original_hook :
251+ return original_hook (args )
252+ else :
253+ return sys .__excepthook__ (args .exc_type , args .exc_value , args .exc_traceback )
254+
255+ return _thread_exception_hook
256+
257+
242258if not settings .disable_analytics :
243259 sys .excepthook = add_exception_hook (sys .excepthook )
244- threading .excepthook = add_exception_hook (threading .excepthook )
260+ threading .excepthook = add_thread_exception_hook (threading .excepthook )
245261
246262analytics = GiskardAnalyticsCollector ()
0 commit comments