Skip to content

Commit

Permalink
Log exceptions and responses in Flask routes. (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
xx12345798 authored Sep 15, 2024
1 parent 441dad7 commit dba5f6c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion application/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import sys

import yaml
from flask import Flask, Blueprint
from flask import Flask, Blueprint, jsonify
from werkzeug.exceptions import HTTPException
from werkzeug.serving import WSGIRequestHandler

try:
Expand All @@ -46,6 +47,24 @@
else:
app = Flask(__name__, static_folder="../client/build")


@app.errorhandler(Exception)
def exception_handler(error):
if isinstance(error, HTTPException):
app.logger.exception(str(error))
return error.get_response(), error.code

app.logger.exception(error)
return jsonify(error=str(error)), 500


@app.after_request
def handle_after_request(response: Flask.response_class):
if response.content_type == 'application/json':
app.logger.info(response.get_data(as_text=True))
return response


app.secret_key = os.getenv('SECRET_KEY', os.urandom(16))

APP_PORT: int
Expand Down

0 comments on commit dba5f6c

Please sign in to comment.