from flask import g from marshmallow import EXCLUDE, Schema from oto import response from oto.adaptors.flask import flaskify from charts.api import app class RequestSchema(Schema): """Base class for schemes used with @querystring_schema decorator. Allows raise Bad Request in case of having errors (instead of 422). """ class Meta: strict = True unknown = EXCLUDE @app.errorhandler(422) def handle_error(self, *args, **kwargs): """Overwrite original ValidationError with custom exception. Schema fails with 422 and we override it with 500 Example of create_fatal_response if exception occur: { "code": "internal_error", "message": "{'isrc': ['Missing data for required field.']}" message is stored in self.exc }. """ try: message = str(self.exc) app.logger.exception(message) except AttributeError: message = ( "The server encountered an internal error " "and was unable to complete your request." ) return flaskify(response.create_fatal_response(message))