File size: 1,749 Bytes
bf71d0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from fastapi import HTTPException, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

from my_ghost_writer.constants import app_logger


def request_validation_exception_handler(request: Request, exc: RequestValidationError) -> JSONResponse:
    """
    Fastapi exception handler for Pydantic RequestValidationError.

    Args:
        request: fastapi request
        exc: RequestValidationError exception

    Returns:
        fastapi error JSONResponse (422 - HTTP_422_UNPROCESSABLE_ENTITY)

    """
    app_logger.error(f"exception errors: {exc.errors()}.")
    app_logger.error(f"exception body: {exc.body}.")
    headers = request.headers.items()
    app_logger.error(f'request header: {dict(headers)}.')
    params = request.query_params.items()
    app_logger.error(f'request query params: {dict(params)}.')
    return JSONResponse(
        status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
        content={"msg": "Error - Unprocessable Entity"}
    )


def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
    """
    Fastapi exception handler for a generic HTTPException.

    Args:
        request: fastapi request
        exc: HTTPException exception

    Returns:
        fastapi error JSONResponse (500 - HTTP_500_INTERNAL_SERVER_ERROR)

    """
    app_logger.error(f"exception: {str(exc)}.")
    headers = request.headers.items()
    app_logger.error(f'request header: {dict(headers)}.')
    params = request.query_params.items()
    app_logger.error(f'request query params: {dict(params)}.')
    return JSONResponse(
        status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
        content={"msg": "Error - Internal Server Error"}
    )