-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #411 from annuaire-entreprises-data-gouv-fr/handle-exceptions
- Loading branch information
Showing
9 changed files
with
174 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import logging | ||
from typing import Callable | ||
|
||
from fastapi import FastAPI, Request | ||
from fastapi.responses import ORJSONResponse | ||
from sentry_sdk import capture_exception, push_scope | ||
|
||
from app.exceptions.exceptions import ( | ||
InternalError, | ||
InvalidParamError, | ||
InvalidSirenError, | ||
NotFoundError, | ||
SearchApiError, | ||
) | ||
|
||
|
||
def create_exception_handler( | ||
status_code: int = 500, initial_detail: str = "Service is unavailable" | ||
) -> Callable[[Request, SearchApiError], ORJSONResponse]: | ||
async def exception_handler( | ||
request: Request, exc: SearchApiError | ||
) -> ORJSONResponse: | ||
detail = { | ||
"status_code": exc.status_code or status_code, | ||
"message": exc.message or initial_detail, | ||
} | ||
|
||
if isinstance(exc, InvalidParamError): | ||
with push_scope() as scope: | ||
scope.fingerprint = ["InvalidParamError"] | ||
logging.warning(f"Bad Request: {exc.message}") | ||
|
||
return ORJSONResponse( | ||
status_code=detail["status_code"], | ||
content={"erreur": detail["message"]}, | ||
) | ||
|
||
return exception_handler | ||
|
||
|
||
async def unhandled_exception_handler( | ||
request: Request, exc: Exception | ||
) -> ORJSONResponse: | ||
logging.error(f"Unhandled exception occurred: {exc}", exc_info=True) | ||
|
||
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) | ||
|
||
internal_error = InternalError( | ||
"Une erreur inattendue s'est produite. Veuillez réessayer plus tard." | ||
) | ||
|
||
handler = create_exception_handler() | ||
return await handler(request, internal_error) | ||
|
||
|
||
def add_exception_handlers(app: FastAPI) -> None: | ||
app.add_exception_handler(InvalidSirenError, create_exception_handler()) | ||
app.add_exception_handler(InvalidParamError, create_exception_handler()) | ||
app.add_exception_handler(NotFoundError, create_exception_handler()) | ||
app.add_exception_handler(Exception, unhandled_exception_handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from fastapi import status | ||
|
||
|
||
class SearchApiError(Exception): | ||
"""Base exception class""" | ||
|
||
def __init__( | ||
self, | ||
message: str = "Service is unavailable.", | ||
name: str = "API Recherche des entreprises", | ||
status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
): | ||
self.message = message | ||
self.name = name | ||
self.status_code = status_code | ||
super().__init__(self.message, self.name) | ||
|
||
|
||
class InvalidSirenError(SearchApiError): | ||
"""Custom exception for invalid SIREN number""" | ||
|
||
def __init__(self): | ||
super().__init__( | ||
message="Numéro Siren invalide.", | ||
name="", | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
|
||
class InvalidParamError(SearchApiError): | ||
"""Invalid parameters in request""" | ||
|
||
def __init__(self, message): | ||
super().__init__( | ||
message=message, | ||
name="", | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
|
||
class InternalError(SearchApiError): | ||
"""Internal service error""" | ||
|
||
def __init__(self, message): | ||
super().__init__( | ||
message=message, | ||
name="", | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
) | ||
|
||
|
||
class NotFoundError(SearchApiError): | ||
"""Resource not found error""" | ||
|
||
def __init__(self, message="Ressource non trouvée."): | ||
super().__init__( | ||
message=message, | ||
name="", | ||
status_code=status.HTTP_404_NOT_FOUND, | ||
) |
Oops, something went wrong.