-
-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathhandlers.py
More file actions
45 lines (38 loc) · 1.31 KB
/
handlers.py
File metadata and controls
45 lines (38 loc) · 1.31 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
"""OpenAPI core contrib django handlers module"""
from django.http import JsonResponse
from openapi_core.exceptions import MissingRequiredParameter
from openapi_core.templating.media_types.exceptions import MediaTypeNotFound
from openapi_core.templating.paths.exceptions import (
ServerNotFound, OperationNotFound, PathNotFound,
)
from openapi_core.validation.exceptions import InvalidSecurity
class DjangoOpenAPIErrorsHandler:
OPENAPI_ERROR_STATUS = {
MissingRequiredParameter: 400,
ServerNotFound: 400,
InvalidSecurity: 403,
OperationNotFound: 405,
PathNotFound: 404,
MediaTypeNotFound: 415,
}
@classmethod
def handle(cls, errors, req, resp=None):
data_errors = [
cls.format_openapi_error(err)
for err in errors
]
data = {
'errors': data_errors,
}
data_error_max = max(data_errors, key=cls.get_error_status)
return JsonResponse(data, status=data_error_max['status'])
@classmethod
def format_openapi_error(cls, error):
return {
'title': str(error),
'status': cls.OPENAPI_ERROR_STATUS.get(error.__class__, 400),
'class': str(type(error)),
}
@classmethod
def get_error_status(cls, error):
return error['status']