1414# See the License for the specific language governing permissions and
1515# limitations under the License
1616
17+ import functools
1718import traceback
19+ from typing import Any , Callable
1820
1921import attrs
2022import fastapi
@@ -63,26 +65,28 @@ class RateLimitExceeded(ogc_api_processes_fastapi.exceptions.OGCAPIException):
6365 retry_after : int = 0
6466
6567
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
8690
8791
8892def format_exception_content (
@@ -135,11 +139,6 @@ def exception_handler(
135139 fastapi.responses.JSONResponse
136140 JSON response.
137141 """
138- logger .error (
139- exc .title ,
140- exception = "" .join (traceback .TracebackException .from_exception (exc ).format ()),
141- url = str (request .url ),
142- )
143142 out = fastapi .responses .JSONResponse (
144143 status_code = exc .status_code ,
145144 content = format_exception_content (exc = exc , request = request ),
@@ -164,11 +163,6 @@ def rate_limit_exceeded_exception_handler(
164163 fastapi.responses.JSONResponse
165164 JSON response.
166165 """
167- logger .error (
168- exc .title ,
169- exception = "" .join (traceback .TracebackException .from_exception (exc ).format ()),
170- url = str (request .url ),
171- )
172166 out = fastapi .responses .JSONResponse (
173167 status_code = exc .status_code ,
174168 content = format_exception_content (exc = exc , request = request ),
@@ -223,14 +217,6 @@ def general_exception_handler(
223217 -------
224218 fastapi.responses.JSONResponse
225219 """
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- )
234220 out = fastapi .responses .JSONResponse (
235221 status_code = fastapi .status .HTTP_500_INTERNAL_SERVER_ERROR ,
236222 content = models .Exception (
0 commit comments