14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License
16
16
17
+ import functools
17
18
import traceback
19
+ from typing import Any , Callable
18
20
19
21
import attrs
20
22
import fastapi
@@ -63,26 +65,28 @@ class RateLimitExceeded(ogc_api_processes_fastapi.exceptions.OGCAPIException):
63
65
retry_after : int = 0
64
66
65
67
66
- def get_exc_group_tb (exc_traceback : list [str ]) -> list [str ]:
67
- """Get and return only the Exception Group Traceback.
68
-
69
- Parameters
70
- ----------
71
- exc_traceback : list[str]
72
- List of lines in the traceback.
73
-
74
- Returns
75
- -------
76
- list[str]
77
- List of lines in the traceback.
78
- """
79
- exc_group_tb = []
80
- if "Exception Group Traceback" in exc_traceback [0 ]:
81
- exc_group_tb .append (exc_traceback [0 ])
82
- for line in exc_traceback [1 :]:
83
- if line .lstrip (" " ).startswith (("+" , "|" )):
84
- exc_group_tb .append (line )
85
- return exc_group_tb
68
+ def exception_logger (f : Callable [..., Any ]) -> Callable [..., Any ]:
69
+ @functools .wraps (f )
70
+ def wrapper (* args : Any , ** kwargs : Any ) -> Any :
71
+ try :
72
+ res = f (* args , ** kwargs )
73
+ except requests .exceptions .ReadTimeout :
74
+ raise
75
+ except Exception as exc :
76
+ if isinstance (exc , ogc_api_processes_fastapi .exceptions .OGCAPIException ):
77
+ exc_title = exc .title
78
+ else :
79
+ exc_title = "internal server error"
80
+ logger .error (
81
+ exc_title ,
82
+ exception = "" .join (
83
+ traceback .TracebackException .from_exception (exc ).format ()
84
+ ),
85
+ )
86
+ raise
87
+ return res
88
+
89
+ return wrapper
86
90
87
91
88
92
def format_exception_content (
@@ -135,11 +139,6 @@ def exception_handler(
135
139
fastapi.responses.JSONResponse
136
140
JSON response.
137
141
"""
138
- logger .error (
139
- exc .title ,
140
- exception = "" .join (traceback .TracebackException .from_exception (exc ).format ()),
141
- url = str (request .url ),
142
- )
143
142
out = fastapi .responses .JSONResponse (
144
143
status_code = exc .status_code ,
145
144
content = format_exception_content (exc = exc , request = request ),
@@ -164,11 +163,6 @@ def rate_limit_exceeded_exception_handler(
164
163
fastapi.responses.JSONResponse
165
164
JSON response.
166
165
"""
167
- logger .error (
168
- exc .title ,
169
- exception = "" .join (traceback .TracebackException .from_exception (exc ).format ()),
170
- url = str (request .url ),
171
- )
172
166
out = fastapi .responses .JSONResponse (
173
167
status_code = exc .status_code ,
174
168
content = format_exception_content (exc = exc , request = request ),
@@ -223,14 +217,6 @@ def general_exception_handler(
223
217
-------
224
218
fastapi.responses.JSONResponse
225
219
"""
226
- exc_traceback = list (traceback .TracebackException .from_exception (exc ).format ())
227
- exc_group_traceback = get_exc_group_tb (exc_traceback )
228
- logger .error (
229
- "internal server error" ,
230
- exception = "" .join (
231
- exc_group_traceback if exc_group_traceback else exc_traceback
232
- ),
233
- )
234
220
out = fastapi .responses .JSONResponse (
235
221
status_code = fastapi .status .HTTP_500_INTERNAL_SERVER_ERROR ,
236
222
content = models .Exception (
0 commit comments