Skip to content

Commit

Permalink
chore: add exception handlers to main
Browse files Browse the repository at this point in the history
  • Loading branch information
HAEKADI committed Aug 30, 2024
1 parent 0b932e1 commit 40e4a37
Showing 1 changed file with 4 additions and 79 deletions.
83 changes: 4 additions & 79 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import logging
from typing import Callable

import yaml
from elasticapm.contrib.starlette import ElasticAPM, make_apm_client
from elasticsearch_dsl import connections
from fastapi import FastAPI, Request
from fastapi.responses import ORJSONResponse
from sentry_sdk import capture_exception, push_scope
from fastapi import FastAPI

from app.config import (
APM_CONFIG,
Expand All @@ -16,12 +11,7 @@
ELASTIC_USER,
OPEN_API_PATH,
)
from app.exceptions.exceptions import (
InternalError,
InvalidParamError,
InvalidSirenError,
SearchApiError,
)
from app.exceptions.exception_handlers import add_exception_handlers
from app.logging import setup_logging, setup_sentry
from app.routers import admin, public

Expand Down Expand Up @@ -65,70 +55,5 @@ def custom_openapi():
app.include_router(public.router)
app.include_router(admin.router)


def create_exception_handler(
status_code: int = 500, initial_detail: str = "Service is unavailable"
) -> Callable[[Request, SearchApiError], ORJSONResponse]:
# Using a dictionary to hold the detail
detail = {"status_code": status_code, "message": initial_detail}

async def exception_handler(
request: Request, exc: SearchApiError
) -> ORJSONResponse:
if exc.message:
detail["message"] = exc.message

if exc.status_code:
detail["status_code"] = exc.status_code
if isinstance(exc, InvalidParamError):
with push_scope() as scope:
scope.fingerprint = ["TESTING"]
logging.warning(f"InvalidParamError: {exc.message}")
return ORJSONResponse(
status_code=detail["status_code"],
content={"status_code": detail["status_code"], "detail": detail["message"]},
)

return exception_handler


app.add_exception_handler(
exc_class_or_status_code=InvalidSirenError,
handler=create_exception_handler(),
)

app.add_exception_handler(
exc_class_or_status_code=InvalidParamError,
handler=create_exception_handler(),
)


# Add a catch-all exception handler for any unhandled exceptions
@app.exception_handler(Exception)
async def unhandled_exception_handler(
request: Request, exc: Exception
) -> ORJSONResponse:
# Log the full exception details
logging.error(f"Unhandled exception occurred: {exc}", exc_info=True)

# Use Sentry to capture the exception
with push_scope() as scope:
scope.set_context(
"request",
{
"url": str(request.url),
"method": request.method,
"headers": dict(request.headers),
"query_params": dict(request.query_params),
},
)
capture_exception(exc)

# Create an InternalError with a generic message
internal_error = InternalError(
"Une erreur inattendue s'est produite. Veuillez réessayer plus tard."
)

# Use the create_exception_handler to handle the InternalError
handler = create_exception_handler()
return await handler(request, internal_error)
# Add exception handlers
add_exception_handlers(app)

0 comments on commit 40e4a37

Please sign in to comment.