Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log exceptions and responses in Flask routes. #31

Merged
merged 3 commits into from
Sep 15, 2024

Conversation

xx12345798
Copy link
Contributor

No description provided.

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


@app.errorhandler(Exception)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! This avoids us explicitly logging every error in route handlers.

Can we also use add a @app.after_request handler to automatically log the responses? (We might need some special handling to avoid logging sensitive information like a route that parse VNC passwords, but we can think more about those in anther PR. )

@junhaoliao
Copy link
Owner

Please also help adding the sys.excepthook and ExceptionThread utilities we discussed offline (documented in some internal notes) to handle uncaught exceptions outside of Flask route handlers. Previously I believe @li-ruihao was assigned to integrate those, but we didn't end up submitting those in #28 . Let's coordinate and see if the changes are suitable to be made in this PR or another one.


@app.after_request
def handle_after_request(response: Flask.response_class):
if response.content_type == 'application/json':
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some routes that return json as a response do not necessarily mark the content_type as "application/json". e.g., in route /sftp/ls",

return json.dumps(result)

We shall replace all json.dumps() with jsonify() from the Flask library.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's postpone that to another PR. We will merge this PR first to unblock others.

Copy link
Owner

@junhaoliao junhaoliao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@junhaoliao junhaoliao changed the title Log exception with exc_info in custom Flask exception handler. Log exceptions and responses in Flask routes. Sep 15, 2024
@junhaoliao junhaoliao merged commit dba5f6c into junhaoliao:main Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants