File size: 1,368 Bytes
1bdfad3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi.responses import JSONResponse
from starlette.status import HTTP_404_NOT_FOUND, HTTP_500_INTERNAL_SERVER_ERROR
from typing import Any

def handle_exception(e: Exception):
    """Handles unexpected exceptions with a standard 500 error response."""
    return JSONResponse(
        status_code=HTTP_500_INTERNAL_SERVER_ERROR,
        content={
            "error": "Internal Server Error",
            "message": "An unexpected error occurred.",
            "details": str(e)
        }
    )

def handle_error(e: Exception, message: str, status_code: int = HTTP_500_INTERNAL_SERVER_ERROR):
    """Generic error handler with custom message and status code."""
    return JSONResponse(
        status_code=status_code,
        content={
            "error": message,
            "details": str(e)
        }
    )

def not_found_error(message: str):
    """Handles 404 errors for not-found cases with a custom message."""
    return JSONResponse(
        status_code=HTTP_404_NOT_FOUND,
        content={
            "error": "Not Found",
            "message": message
        }
    )

def no_entries_found(message: str):
    """Handles 404 errors for cases where no entries are found."""
    return JSONResponse(
        status_code=HTTP_404_NOT_FOUND,
        content={
            "error": "No Entries Found",
            "message": message
        }
    )