"""Base handlers.""" from flask import g from flask import jsonify from flask import request from oto import response from oto.adaptors.flask import flaskify from pympler import muppy from pympler import summary from backend import config from backend.api import app from backend.exceptions import RequestError @app.route(config.HEALTH_CHECK, methods=['GET']) def health(): """Check the health of the application.""" result = {'status': 'ok'} if config.ENVIRONMENT != config.PROD_ENVIRONMENT \ and 'debug' in request.args: all_objects = muppy.get_objects() sum1 = summary.summarize(all_objects) lines = [row for row in summary.format_(sum1)] result['_debug'] = lines return jsonify(result) @app.errorhandler(RequestError) def handle_request_error(error): """Handle request and validation errors.""" return error.make_flask_response() @app.errorhandler(500) def exception_handler(error): """Default handler when uncaught exception is raised. Note: Exception will also be sent to Sentry if config.SENTRY is set. Returns: flask.Response: A 500 response with JSON 'code' & 'message' payload. """ message = ( 'The server encountered an internal error ' 'and was unable to complete your request.') g.log.exception(error) return flaskify(response.create_fatal_response(message))