"""Base handlers not associated with any models.""" from typing import Any from flask import Response, g, jsonify from owsresponse import response from owsresponse.adaptors.flask import flaskify from collaborator import config from collaborator.api import app from collaborator.utils.error import OwsError @app.route(config.HEALTH_CHECK, methods=["GET"]) def health(): """Check the health of the application.""" return jsonify({"status": "ok"}) @app.errorhandler(500) def exception_handler(error: Any) -> Response: """Handle error when uncaught exception is raised. Default exception handler. 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)) @app.errorhandler(OwsError) def ows_error_handler(error: OwsError) -> Response: """Handle an OwsError exception, converting it to an HTTP response. Args: error (OwsError): The error to handle Returns: flask.Response """ g.log.exception(error) return flaskify( response.Response( message={ "message": error.message, "code": error.code, }, status=error.status, ) )