File size: 1,022 Bytes
532466c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Reusable HTTP error helpers."""

from fastapi import HTTPException


def bad_request(detail: str, cause: Exception | None = None) -> HTTPException:
    exc = HTTPException(status_code=400, detail=detail)
    if cause is not None:
        raise exc from cause
    raise exc


def not_found(detail: str = "Not found.") -> HTTPException:
    raise HTTPException(status_code=404, detail=detail)


def conflict(detail: str) -> HTTPException:
    raise HTTPException(status_code=409, detail=detail)


def server_error(detail: str) -> HTTPException:
    raise HTTPException(status_code=500, detail=detail)


def service_unavailable(detail: str, cause: Exception | None = None) -> HTTPException:
    exc = HTTPException(status_code=503, detail=detail)
    if cause is not None:
        raise exc from cause
    raise exc


def bad_gateway(detail: str, cause: Exception | None = None) -> HTTPException:
    exc = HTTPException(status_code=502, detail=detail)
    if cause is not None:
        raise exc from cause
    raise exc