Skip to content

Commit d14689e

Browse files
authored
Merge pull request #236 from ecmwf-projects/COPDS-2353-still-better-logs
in case of internal server error, log only the Exception Group Traceback (if present)
2 parents ebbab7c + 566def2 commit d14689e

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

cads_processing_api_service/clients.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ def get_jobs(
382382
List of jobs status information.
383383
"""
384384
structlog.contextvars.bind_contextvars(
385-
**auth_info.model_dump(), client_endpoint="get_jobs"
385+
**auth_info.model_dump(),
386+
client_endpoint="get_jobs",
386387
)
387388
logger.info("get_jobs")
388389
_ = limits.check_rate_limits(
@@ -505,7 +506,9 @@ def get_job(
505506
Job status information.
506507
"""
507508
structlog.contextvars.bind_contextvars(
508-
**auth_info.model_dump(), job_id=job_id, client_endpoint="get_job"
509+
**auth_info.model_dump(),
510+
job_id=job_id,
511+
client_endpoint="get_job",
509512
)
510513
logger.info("get_job")
511514
_ = limits.check_rate_limits(

cads_processing_api_service/exceptions.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,26 @@ class RateLimitExceeded(ogc_api_processes_fastapi.exceptions.OGCAPIException):
6363
retry_after: int = 0
6464

6565

66-
def find_last_context_line(lines: list[str]) -> int:
67-
index = 0
68-
for i, line in enumerate(lines):
69-
if "result = context.run(func, *args)" in line:
70-
index = len(lines) - i - 1
71-
return index
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
7286

7387

7488
def format_exception_content(
@@ -210,10 +224,12 @@ def general_exception_handler(
210224
fastapi.responses.JSONResponse
211225
"""
212226
exc_traceback = list(traceback.TracebackException.from_exception(exc).format())
213-
last_context_line = find_last_context_line(list(exc_traceback))
227+
exc_group_traceback = get_exc_group_tb(exc_traceback)
214228
logger.error(
215229
"internal server error",
216-
exception="".join(exc_traceback[-last_context_line:]),
230+
exception="".join(
231+
exc_group_traceback if exc_group_traceback else exc_traceback
232+
),
217233
)
218234
out = fastapi.responses.JSONResponse(
219235
status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR,

0 commit comments

Comments
 (0)