mirror of
https://github.com/correl/openapi-core.git
synced 2024-11-25 19:19:55 +00:00
42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
|
"""OpenAPI core contrib flask handlers module"""
|
||
|
from flask.globals import current_app
|
||
|
from flask.json import dumps
|
||
|
|
||
|
from openapi_core.schema.media_types.exceptions import InvalidContentType
|
||
|
from openapi_core.schema.servers.exceptions import InvalidServer
|
||
|
|
||
|
|
||
|
class FlaskOpenAPIErrorsHandler(object):
|
||
|
|
||
|
OPENAPI_ERROR_STATUS = {
|
||
|
InvalidServer: 500,
|
||
|
InvalidContentType: 415,
|
||
|
}
|
||
|
|
||
|
@classmethod
|
||
|
def handle(cls, errors):
|
||
|
data_errors = [
|
||
|
cls.format_openapi_error(err)
|
||
|
for err in errors
|
||
|
]
|
||
|
data = {
|
||
|
'errors': data_errors,
|
||
|
}
|
||
|
status = max(
|
||
|
range(len(data_errors)),
|
||
|
key=lambda idx: data_errors[idx]['status'],
|
||
|
)
|
||
|
return current_app.response_class(
|
||
|
dumps(data),
|
||
|
status=status,
|
||
|
mimetype='application/json'
|
||
|
)
|
||
|
|
||
|
@classmethod
|
||
|
def format_openapi_error(cls, error):
|
||
|
return {
|
||
|
'title': str(error),
|
||
|
'status': cls.OPENAPI_ERROR_STATUS.get(error.__class__, 400),
|
||
|
'class': str(type(error)),
|
||
|
}
|