-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.py
More file actions
98 lines (76 loc) · 2.41 KB
/
Copy patherrors.py
File metadata and controls
98 lines (76 loc) · 2.41 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Dict, Optional
from fastapi import HTTPException, status
@dataclass(frozen=True)
class ApiError:
code: str
message: str
http_status: int
def to_http_exception(self, *, extra: Optional[Dict[str, Any]] = None) -> HTTPException:
"""
Convert this error to FastAPI's HTTPException.
If extra is provided, it will be included in the detail payload.
"""
detail: Any
detail = {"code": self.code, "message": self.message}
if extra:
detail.update(extra)
return HTTPException(status_code=self.http_status, detail=detail)
def raise_api_error(err: ApiError, *, extra: Optional[Dict[str, Any]] = None) -> None:
raise err.to_http_exception(extra=extra)
# ----------------------------
# Room errors
# ----------------------------
ROOM_NOT_FOUND = ApiError(
code="ROOM_NOT_FOUND",
message="Room not found.",
http_status=status.HTTP_404_NOT_FOUND,
)
# ----------------------------
# Booking validation errors (400)
# ----------------------------
BOOKING_START_AFTER_END = ApiError(
code="BOOKING_START_AFTER_END",
message="Start time must be before end time.",
http_status=status.HTTP_400_BAD_REQUEST,
)
BOOKING_IN_PAST = ApiError(
code="BOOKING_IN_PAST",
message="You can't book a room in the past.",
http_status=status.HTTP_400_BAD_REQUEST,
)
BOOKING_INVALID_REQUEST = ApiError(
code="BOOKING_INVALID_REQUEST",
message="Invalid booking request.",
http_status=status.HTTP_400_BAD_REQUEST,
)
# ----------------------------
# Booking conflict errors (409)
# ----------------------------
BOOKING_OVERLAPS = ApiError(
code="BOOKING_OVERLAPS",
message="Booking overlaps with an existing booking.",
http_status=status.HTTP_409_CONFLICT,
)
BOOKING_CONFLICT = ApiError(
code="BOOKING_CONFLICT",
message="Booking conflict.",
http_status=status.HTTP_409_CONFLICT,
)
# ----------------------------
# Booking not found (404)
# ----------------------------
BOOKING_NOT_FOUND = ApiError(
code="BOOKING_NOT_FOUND",
message="Booking not found.",
http_status=status.HTTP_404_NOT_FOUND,
)
# ----------------------------
# Route not found (404)
# ----------------------------
ROUTE_NOT_FOUND = ApiError(
code="ROUTE_NOT_FOUND",
message="Route not found.",
http_status=status.HTTP_404_NOT_FOUND,
)