Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
import re
import asyncio
import traceback
from typing import List, Union, Optional
from datetime import datetime
from contextlib import asynccontextmanager
Expand All @@ -35,7 +36,7 @@
from fastapi_users import FastAPIUsers
from beanie import PydanticObjectId
from pydantic import BaseModel
from kernelci.api.models import (

Check failure on line 39 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Unable to import 'kernelci.api.models'
Node,
Hierarchy,
PublishEvent,
Expand Down Expand Up @@ -1101,10 +1102,21 @@
)


"""Workaround to use global exception handlers for versioned API.
The issue has already been reported here:
https://github.com/DeanWay/fastapi-versioning/issues/30
"""
# traceback_exception_handler is a global exception handler that will be
# triggered for all exceptions that are not handled by specific exception
def traceback_exception_handler(request: Request, exc: Exception):
"""Global exception handler to print traceback"""
print(f"Exception: {exc}")
traceback.print_exception(type(exc), exc, exc.__traceback__)
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"message": "Internal server error, check container logs"}
)


# Workaround to use global exception handlers for versioned API.
# The issue has already been reported here:
# https://github.com/DeanWay/fastapi-versioning/issues/30
for sub_app in versioned_app.routes:
if hasattr(sub_app.app, "add_exception_handler"):
sub_app.app.add_exception_handler(
Expand All @@ -1113,6 +1125,10 @@
sub_app.app.add_exception_handler(
errors.InvalidId, invalid_id_exception_handler
)
# print traceback for all other exceptions
sub_app.app.add_exception_handler(
Exception, traceback_exception_handler
)


@versioned_app.middleware("http")
Expand Down
Loading