-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
48 lines (30 loc) · 1.12 KB
/
Copy pathexceptions.py
File metadata and controls
48 lines (30 loc) · 1.12 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
from pydantic import ValidationError
class WeatherAPIError(Exception):
"""Base exception for weather API errors."""
status_code = 500
def __init__(self, errors: dict | str):
self.errors = errors
super().__init__(errors)
class InvalidParameterError(WeatherAPIError):
"""Exception raised when invalid parameters are provided."""
status_code = 400
class CityNotFoundError(WeatherAPIError):
status_code = 404
class APIConnectionError(WeatherAPIError):
"""Exception raised when connection to the weather API fails."""
status_code = 503
class DataParsingError(WeatherAPIError):
"""Exception raised when the API response cannot be parsed."""
status_code = 500
class RateLimitExceededError(WeatherAPIError):
"""Exception raised when the API rate limit is exceeded."""
status_code = 429
def format_pydantic_errors(e: ValidationError) -> dict:
"""Turns Pydantic errors into a clean {field: message} dictionary."""
return {
"error":
{
str(err["loc"][0]):
err["msg"] for err in e.errors()
}
}