From 625f42f64f26f067574a4f298096443e2a516a2c Mon Sep 17 00:00:00 2001 From: Xuan Xu Date: Thu, 25 Jul 2024 01:42:46 -0400 Subject: [PATCH 1/7] Log exception with exc_info in custom Flask exception handler. --- application/__init__.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/application/__init__.py b/application/__init__.py index 2040aba..78117c1 100644 --- a/application/__init__.py +++ b/application/__init__.py @@ -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: @@ -46,6 +47,17 @@ 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.secret_key = os.getenv('SECRET_KEY', os.urandom(16)) APP_PORT: int From 190dfe06d46aed873acca21f5c35c37b3d8a2119 Mon Sep 17 00:00:00 2001 From: Xuan Xu Date: Sun, 8 Sep 2024 18:08:03 -0400 Subject: [PATCH 2/7] Log response in custom Flask after request handler --- application/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/application/__init__.py b/application/__init__.py index 78117c1..860170e 100644 --- a/application/__init__.py +++ b/application/__init__.py @@ -57,6 +57,10 @@ def exception_handler(error): app.logger.exception(error) return jsonify(error=str(error)), 500 +@app.after_request +def AfterRequestFunc(response): + app.logger.info(response.status) + return response app.secret_key = os.getenv('SECRET_KEY', os.urandom(16)) From 4f351299526b94701b4d7eb62ffe4b17f671a707 Mon Sep 17 00:00:00 2001 From: Xuan Xu Date: Sun, 15 Sep 2024 10:27:27 -0400 Subject: [PATCH 3/7] addressed the comments of using response.get_data instead of response.status --- application/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/application/__init__.py b/application/__init__.py index 860170e..160468a 100644 --- a/application/__init__.py +++ b/application/__init__.py @@ -57,11 +57,14 @@ def exception_handler(error): app.logger.exception(error) return jsonify(error=str(error)), 500 + @app.after_request -def AfterRequestFunc(response): - app.logger.info(response.status) +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 From 392a42631fd232476fc821142081317e5e5f1d67 Mon Sep 17 00:00:00 2001 From: Xuan Xu Date: Fri, 27 Sep 2024 00:41:46 -0400 Subject: [PATCH 4/7] use jsonify to replace response dumped by json.dump --- application/routes/audio.py | 4 ++-- application/routes/common.py | 4 ++-- application/routes/sftp.py | 4 ++-- application/routes/term.py | 4 ++-- application/routes/vnc.py | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/application/routes/audio.py b/application/routes/audio.py index b38f7e2..a51ac3e 100644 --- a/application/routes/audio.py +++ b/application/routes/audio.py @@ -29,7 +29,7 @@ # import json -from flask import request, abort +from flask import request, abort, jsonify from .common import create_connection from .. import api @@ -53,4 +53,4 @@ def start_audio(): 'audio_id': audio.id } - return json.dumps(result) + return jsonify(result) diff --git a/application/routes/common.py b/application/routes/common.py index b4a8364..6dfb9be 100644 --- a/application/routes/common.py +++ b/application/routes/common.py @@ -21,7 +21,7 @@ import json import threading -from flask import request, abort +from flask import request, abort, jsonify from .. import api, profiles from ..codes import ICtrlError, ConnectionType @@ -80,7 +80,7 @@ def handle_session(): session_id = request.args.get('id') host, username, _, _, nickname = profiles.get_session_info(session_id) - return json.dumps({ + return jsonify({ 'host': host, 'username': username, 'nickname': nickname diff --git a/application/routes/sftp.py b/application/routes/sftp.py index 6495a48..b9a4080 100644 --- a/application/routes/sftp.py +++ b/application/routes/sftp.py @@ -23,7 +23,7 @@ from datetime import datetime from pathlib import Path -from flask import request, abort, stream_with_context +from flask import request, abort, stream_with_context, jsonify from .common import create_connection from .. import api, app @@ -48,7 +48,7 @@ def sftp_ls(session_id): 'cwd': cwd, 'files': file_list } - return json.dumps(result) + return jsonify(result) @api.route('/sftp_dl/') diff --git a/application/routes/term.py b/application/routes/term.py index 95c7776..d0070ba 100644 --- a/application/routes/term.py +++ b/application/routes/term.py @@ -20,7 +20,7 @@ import json -from flask import request, abort, stream_with_context +from flask import request, abort, stream_with_context, jsonify from .common import create_connection from .. import api, app @@ -59,7 +59,7 @@ def generate(): 'port': TERMINAL_PORT, 'term_id': term.id } - yield json.dumps(result) + yield jsonify(result) return app.response_class(stream_with_context(generate()), mimetype='application/octet-stream') diff --git a/application/routes/vnc.py b/application/routes/vnc.py index 7a6c06f..a1fd064 100644 --- a/application/routes/vnc.py +++ b/application/routes/vnc.py @@ -21,7 +21,7 @@ import json import logging -from flask import request, abort, stream_with_context +from flask import request, abort, stream_with_context, jsonify from .common import create_connection from .. import api, app, profiles @@ -93,7 +93,7 @@ def generate(): 'port': ws_port, 'credentials': credentials } - yield json.dumps(result) + yield jsonify(result) return app.response_class(stream_with_context(generate()), mimetype='application/octet-stream') From 4efa440ca664035e41abc1efac07ba3a92c17141 Mon Sep 17 00:00:00 2001 From: Xuan Xu Date: Sun, 5 Jan 2025 16:45:52 -0500 Subject: [PATCH 5/7] remove the unused json import and revert the response of type application/octet-stream back to json.dumps, change a log message argument to display failing reason --- application/routes/audio.py | 1 - application/routes/common.py | 1 - application/routes/term.py | 4 ++-- application/routes/vnc.py | 6 +++--- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/application/routes/audio.py b/application/routes/audio.py index a51ac3e..af233b7 100644 --- a/application/routes/audio.py +++ b/application/routes/audio.py @@ -27,7 +27,6 @@ # furnished to do so, subject to the following conditions: # # -import json from flask import request, abort, jsonify diff --git a/application/routes/common.py b/application/routes/common.py index 6dfb9be..7938801 100644 --- a/application/routes/common.py +++ b/application/routes/common.py @@ -18,7 +18,6 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. -import json import threading from flask import request, abort, jsonify diff --git a/application/routes/term.py b/application/routes/term.py index d0070ba..95c7776 100644 --- a/application/routes/term.py +++ b/application/routes/term.py @@ -20,7 +20,7 @@ import json -from flask import request, abort, stream_with_context, jsonify +from flask import request, abort, stream_with_context from .common import create_connection from .. import api, app @@ -59,7 +59,7 @@ def generate(): 'port': TERMINAL_PORT, 'term_id': term.id } - yield jsonify(result) + yield json.dumps(result) return app.response_class(stream_with_context(generate()), mimetype='application/octet-stream') diff --git a/application/routes/vnc.py b/application/routes/vnc.py index a1fd064..621a6ba 100644 --- a/application/routes/vnc.py +++ b/application/routes/vnc.py @@ -21,7 +21,7 @@ import json import logging -from flask import request, abort, stream_with_context, jsonify +from flask import request, abort, stream_with_context from .common import create_connection from .. import api, app, profiles @@ -93,7 +93,7 @@ def generate(): 'port': ws_port, 'credentials': credentials } - yield jsonify(result) + yield json.dumps(result) return app.response_class(stream_with_context(generate()), mimetype='application/octet-stream') @@ -104,7 +104,7 @@ def change_vncpasswd(): vnc, reason = create_connection(session_id, ConnectionType.VNC) if reason != '': - logger.error("create_connection() failed with status=", status) + logger.error("create_connection() failed with status=", reason) abort(403, description=reason) passwd = request.json.get('passwd') From abf9d307c28257483679bc80a371ae255133ab23 Mon Sep 17 00:00:00 2001 From: Xuan Xu Date: Sun, 5 Jan 2025 17:17:01 -0500 Subject: [PATCH 6/7] fixed missing %s --- application/routes/vnc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/routes/vnc.py b/application/routes/vnc.py index 621a6ba..d0bd9e5 100644 --- a/application/routes/vnc.py +++ b/application/routes/vnc.py @@ -104,7 +104,7 @@ def change_vncpasswd(): vnc, reason = create_connection(session_id, ConnectionType.VNC) if reason != '': - logger.error("create_connection() failed with status=", reason) + logger.error("create_connection() failed with status=%s", reason) abort(403, description=reason) passwd = request.json.get('passwd') From ada79e7175908ae98e7f4eb6f1a6e18be88bbe91 Mon Sep 17 00:00:00 2001 From: Xuan Xu Date: Sun, 5 Jan 2025 17:59:00 -0500 Subject: [PATCH 7/7] add missing %s into the log message of file application/routes/vnc.py as a fix of PR #29 --- application/routes/vnc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/routes/vnc.py b/application/routes/vnc.py index 621a6ba..d0bd9e5 100644 --- a/application/routes/vnc.py +++ b/application/routes/vnc.py @@ -104,7 +104,7 @@ def change_vncpasswd(): vnc, reason = create_connection(session_id, ConnectionType.VNC) if reason != '': - logger.error("create_connection() failed with status=", reason) + logger.error("create_connection() failed with status=%s", reason) abort(403, description=reason) passwd = request.json.get('passwd')