-
-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathexceptions.py
More file actions
46 lines (38 loc) · 1.33 KB
/
exceptions.py
File metadata and controls
46 lines (38 loc) · 1.33 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
"""OpenAPI core validation exceptions module"""
from dataclasses import dataclass
from typing import Any
from openapi_core.exceptions import OpenAPIError
def _schema_error_to_dict(schema_error: Exception) -> dict[str, Any]:
message = getattr(schema_error, "message", str(schema_error))
raw_path = getattr(schema_error, "path", ())
try:
path = list(raw_path)
except TypeError:
path = []
return {
"message": message,
"path": path,
}
@dataclass
class ValidationError(OpenAPIError):
@property
def details(self) -> dict[str, Any]:
cause = self.__cause__
schema_errors: list[dict[str, Any]] = []
if cause is not None:
cause_schema_errors = getattr(cause, "schema_errors", None)
if cause_schema_errors is not None:
schema_errors = [
_schema_error_to_dict(schema_error)
for schema_error in cause_schema_errors
]
return {
"message": str(self),
"error_type": self.__class__.__name__,
"cause_type": (
cause.__class__.__name__ if cause is not None else None
),
"schema_errors": schema_errors,
}
def __str__(self) -> str:
return f"{self.__class__.__name__}: {self.__cause__}"