@@ -59,57 +59,70 @@ def _uninstrument(self, **kwargs):
59
59
ExceptionInstrumentor ().instrument ()
60
60
61
61
# Automatic exception handling for flask
62
- from flask import Flask , request
63
- from flask .signals import got_request_exception , appcontext_pushed
64
- import traceback
65
-
66
- def _capture_exception (sender , exception , ** extra ):
67
- exc_type , exc_value , exc_traceback = sys .exc_info () # Get exception details
68
- if exc_type and exc_value and exc_traceback :
69
- record_exception (exc_type , exc_value , exc_traceback )
70
- else :
71
- print ("Unable to capture exception details." )
62
+ try :
63
+ from flask import Flask , request
64
+ from flask .signals import got_request_exception , appcontext_pushed
65
+ except ImportError :
66
+ Flask = None
67
+ got_request_exception = None
68
+ appcontext_pushed = None
69
+
70
+ if Flask and got_request_exception and appcontext_pushed :
71
+ def _capture_exception (sender , exception , ** extra ):
72
+ exc_type , exc_value , exc_traceback = sys .exc_info () # Get exception details
73
+ if exc_type and exc_value and exc_traceback :
74
+ record_exception (exc_type , exc_value , exc_traceback )
75
+ else :
76
+ print ("Unable to capture exception details." )
72
77
73
- def try_register_flask_handler (app : Flask ):
74
- """Registers the exception handler using Flask signals."""
75
- got_request_exception .connect (_capture_exception , app )
76
- print ("✅ Flask error handler registered via signal." )
78
+ def try_register_flask_handler (app : Flask ):
79
+ """Registers the exception handler using Flask signals."""
80
+ got_request_exception .connect (_capture_exception , app )
81
+ print ("✅ Flask error handler registered via signal." )
77
82
78
- def _auto_register (sender , ** extra ):
79
- """Automatically registers the handler when a Flask app context is pushed."""
80
- try_register_flask_handler (sender )
83
+ def _auto_register (sender , ** extra ):
84
+ """Automatically registers the handler when a Flask app context is pushed."""
85
+ try_register_flask_handler (sender )
81
86
82
- # Connect to Flask's appcontext_pushed to register automatically
83
- appcontext_pushed .connect (_auto_register )
87
+ # Connect to Flask's appcontext_pushed to register automatically
88
+ appcontext_pushed .connect (_auto_register )
84
89
85
90
86
91
# Automatic exception handling for FastAPI
87
- from starlette .middleware .base import BaseHTTPMiddleware
88
- from starlette .requests import Request
89
- from starlette .responses import JSONResponse
90
-
91
- class ExceptionMiddleware (BaseHTTPMiddleware ):
92
- """Middleware to catch unhandled exceptions globally."""
93
- async def dispatch (self , request : Request , call_next ):
94
- try :
95
- return await call_next (request )
96
- except Exception as exc :
97
- exc_type , exc_value , exc_traceback = exc .__class__ , exc , exc .__traceback__
98
- record_exception (exc_type , exc_value , exc_traceback )
92
+ try :
93
+ from starlette .middleware .base import BaseHTTPMiddleware
94
+ from starlette .requests import Request
95
+ from starlette .responses import JSONResponse
96
+ from fastapi import FastAPI
97
+ except ImportError :
98
+ BaseHTTPMiddleware = None
99
+ Request = None
100
+ JSONResponse = None
101
+ FastAPI = None
102
+
103
+ if BaseHTTPMiddleware and FastAPI :
104
+ class ExceptionMiddleware (BaseHTTPMiddleware ):
105
+ """Middleware to catch unhandled exceptions globally."""
106
+ async def dispatch (self , request : Request , call_next ):
107
+ try :
108
+ return await call_next (request )
109
+ except Exception as exc :
110
+ exc_type , exc_value , exc_traceback = exc .__class__ , exc , exc .__traceback__
111
+ record_exception (exc_type , exc_value , exc_traceback )
112
+
113
+ return JSONResponse (
114
+ status_code = 500 ,
115
+ content = {"detail" : "Internal Server Error" },
116
+ )
99
117
100
- return JSONResponse (
101
- status_code = 500 ,
102
- content = {"detail" : "Internal Server Error" },
103
- )
104
-
105
- from fastapi import FastAPI
106
- # from starlette.middleware import ExceptionMiddleware
107
-
108
- _original_init = FastAPI .__init__
109
-
110
- def new_fastapi_init (self , * args , ** kwargs ):
111
- _original_init (self , * args , ** kwargs )
112
- print ("✅ FastAPI instance created, registering ExceptionMiddleware." )
113
- self .add_middleware (ExceptionMiddleware )
114
-
115
- FastAPI .__init__ = new_fastapi_init
118
+ from fastapi import FastAPI
119
+ # from starlette.middleware import ExceptionMiddleware
120
+
121
+ _original_init = FastAPI .__init__
122
+
123
+ def new_fastapi_init (self , * args , ** kwargs ):
124
+ _original_init (self , * args , ** kwargs )
125
+ print ("✅ FastAPI instance created, registering ExceptionMiddleware." )
126
+ self .add_middleware (ExceptionMiddleware )
127
+
128
+ FastAPI .__init__ = new_fastapi_init
0 commit comments