from flask import jsonify from core_notifications.logs import logger from . import api @api.errorhandler(422) @api.errorhandler(400) def validation_error(err): """Webargs validation errors handler.""" data = {"detail": err.description, "status_code": err.code} try: data["extra"] = err.data.get("messages") except Exception: # nosec pass # nosec return jsonify(data), err.code @api.errorhandler(Exception) def generic_error(err): """ Generic errors handler, that may process things like flask abort/exception or any generic exception. """ data = {"status_code": 500, "detail": "Internal server error."} is_bare = False try: data["status_code"] = err.code data["detail"] = err.description except Exception: is_bare = True if is_bare: # logging with stack trace if it is not a response # but the bare exception logger.exception(err) return jsonify(data), data["status_code"]