-
Notifications
You must be signed in to change notification settings - Fork 4
/
error_handling.py
39 lines (33 loc) · 1.35 KB
/
error_handling.py
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
from typing import Final
from fastapi import HTTPException
invalid_date_message: Final[
str
] = "Invalid date format for '{}'. Expected ISO 8601 format, example: '2021-01-01T00:00:00Z'"
invalid_bounding_coordinates: Final[str] = "Invalid bounding coordinates {} {}"
invalid_bounding_method: Final[str] = "Invalid bounding_filter_method {}"
feed_not_found: Final[str] = "Feed '{}' not found"
gtfs_feed_not_found: Final[str] = "GTFS feed '{}' not found"
gtfs_rt_feed_not_found: Final[str] = "GTFS realtime Feed '{}' not found"
dataset_not_found: Final[str] = "Dataset '{}' not found"
def raise_http_error(status_code: int, error: str):
"""Raise a HTTPException.
:param status_code: The status code of the error.
:param error: The error message to be raised.
example of output:
{
"detail": "Invalid date format for 'field_name'. Expected ISO 8601 format, example: '2021-01-01T00:00:00Z'"
}
"""
raise HTTPException(
status_code=status_code,
detail=error,
)
def raise_http_validation_error(error: str):
"""Raise a HTTPException with status code 422 and the error message.
:param error: The error message to be raised.
example of output:
{
"detail": "Invalid date format for 'field_name'. Expected ISO 8601 format, example: '2021-01-01T00:00:00Z'"
}
"""
raise_http_error(422, error)