Skip to content

Commit 65d1ded

Browse files
authored
Set traceback for job failed (#192)
* reset traceback for job failed + minor refactoring * update * fix type
1 parent 0042ffd commit 65d1ded

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

cads_processing_api_service/exceptions.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ def format_exception_content(
8282
instance=instance,
8383
trace_id=structlog.contextvars.get_contextvars().get("trace_id", "unset"),
8484
).model_dump(exclude_none=True)
85+
if isinstance(exc, ogc_api_processes_fastapi.exceptions.JobResultsFailed):
86+
exception_content["traceback"] = exc.traceback
8587

8688
return exception_content
8789

@@ -108,10 +110,11 @@ def exception_handler(
108110
exception="".join(traceback.TracebackException.from_exception(exc).format()),
109111
url=str(request.url),
110112
)
111-
return fastapi.responses.JSONResponse(
113+
out = fastapi.responses.JSONResponse(
112114
status_code=exc.status_code,
113115
content=format_exception_content(exc=exc, request=request),
114116
)
117+
return out
115118

116119

117120
def request_readtimeout_handler(
@@ -137,8 +140,7 @@ def request_readtimeout_handler(
137140
type="read timeout error",
138141
title="read timeout error",
139142
trace_id=structlog.contextvars.get_contextvars().get("trace_id", "unset"),
140-
detail=str(exc),
141-
),
143+
).model_dump(exclude_none=True),
142144
)
143145
return out
144146

@@ -165,14 +167,15 @@ def general_exception_handler(
165167
"internal server error",
166168
exception="".join(traceback.TracebackException.from_exception(exc).format()),
167169
)
168-
return fastapi.responses.JSONResponse(
170+
out = fastapi.responses.JSONResponse(
169171
status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR,
170172
content=models.Exception(
171173
type="internal server error",
172174
title="internal server error",
173175
trace_id=structlog.contextvars.get_contextvars().get("trace_id", "unset"),
174176
).model_dump(exclude_none=True),
175177
)
178+
return out
176179

177180

178181
def include_exception_handlers(app: fastapi.FastAPI) -> fastapi.FastAPI:
@@ -193,6 +196,9 @@ def include_exception_handlers(app: fastapi.FastAPI) -> fastapi.FastAPI:
193196
app.add_exception_handler(InvalidParameter, exception_handler) # type: ignore
194197
app.add_exception_handler(InvalidRequest, exception_handler) # type: ignore
195198
app.add_exception_handler(JobResultsExpired, exception_handler) # type: ignore
196-
app.add_exception_handler(requests.exceptions.ReadTimeout, exception_handler) # type: ignore
199+
app.add_exception_handler(
200+
requests.exceptions.ReadTimeout,
201+
request_readtimeout_handler, # type: ignore
202+
)
197203
app.add_exception_handler(Exception, general_exception_handler)
198204
return app

0 commit comments

Comments
 (0)