-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathvalidators.py
51 lines (41 loc) · 1.24 KB
/
validators.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
40
41
42
43
44
45
46
47
48
49
50
51
"""
Holds functions that convert backend errors into a pythonic format the application can understand.
"""
from typing import Any, Dict
from darwin.exceptions import NameTaken, ValidationError
def name_taken(code: int, body: Dict[str, Any]) -> None:
"""
Validates if a request to the backend errored out with a NameTaken error.
Parameters
----------
code : int
The response code.
body : Dict[str, Any]
The response body.
Raises
------
NameTaken
If both ``code`` and ``body`` indicate that the server request errored due to a name being
already taken.
"""
if code != 422:
return
if body.get("errors", {}).get("name") == ["has already been taken"]:
raise NameTaken
def validation_error(code: int, body: Dict[str, Any]) -> None:
"""
Validates if a request to the backend errored out with a Validation error.
Parameters
----------
code : int
The response code.
body : Dict[str, Any]
The response body.
Raises
------
ValidationError
If both ``code`` and ``body`` indicate that the server request errored because it failed
validation.
"""
if code == 422:
raise ValidationError(body)