Implement frontend-backend API integration for kitchen game progress management - #77
Implement frontend-backend API integration for kitchen game progress management#77moulongzhang with Copilot wants to merge 4 commits into
Conversation
Co-authored-by: moulongzhang <39043782+moulongzhang@users.noreply.github.com>
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| }), 500 |
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 9 months ago
To fix the problem, the code should not return the variable exception message str(e) in the HTTP response to the user. Instead, a generic error message (such as "An internal error has occurred") should be sent to the client. Optionally, details about the exception (error type, stack trace, etc.) can be logged on the server side for debugging purposes—using Python's logging module—without leaking these details to the user.
This change is needed in each API endpoint that exposes internal exception messages. In this snippet, it affects:
- The
/api/progressendpoint, lines 63-67 - The
/api/recipesendpoint, lines 89-93
Changes required:
- Import Python's
logginglibrary (standard, safe to add) - Initialize or configure logging (if not already configured)
- In error handlers, log the exception as
logging.exception(e)(or equivalent, to capture stack trace) - Return a generic error message to the user: do not include
str(e)in the response, use a hardcoded string like"An internal error has occurred."
| @@ -7,7 +7,10 @@ | ||
| import threading | ||
| import time | ||
| import os | ||
| import logging | ||
|
|
||
| logging.basicConfig(level=logging.INFO) | ||
|
|
||
| app = Flask(__name__) | ||
| CORS(app) | ||
|
|
||
| @@ -61,9 +63,10 @@ | ||
| 'data': progress_data | ||
| }), 200 | ||
| except Exception as e: | ||
| logging.exception("Exception in /api/progress endpoint") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': "An internal error has occurred." | ||
| }), 500 | ||
|
|
||
|
|
||
| @@ -87,9 +88,10 @@ | ||
| 'data': recipes_data | ||
| }), 200 | ||
| except Exception as e: | ||
| logging.exception("Exception in /api/recipes endpoint") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': "An internal error has occurred." | ||
| }), 500 | ||
|
|
||
|
|
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| }), 500 |
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 9 months ago
To fix the problem, all API endpoints should stop returning the string representation of exceptions (str(e)) in the HTTP response. Instead, the code should log the detailed exception information server-side (preferably including the stack trace for debugging) and return a generic error message to the client. This can be implemented by using Python's logging module to log details, and returning "An internal error has occurred." (or similar) to the user.
The changes required are as follows:
- In all exception handlers where
return jsonify({'success': False, 'error': str(e)})is used, replace with logging the error (including its stack trace) and returning a generic error message. - Import the
loggingmodule. - Configure logging if not already done (for example, with
logging.basicConfig(level=logging.ERROR)or similar). - The relevant exception handlers are found in the endpoints
/api/progress,/api/recipes, and/api/deliver.
| @@ -7,7 +7,10 @@ | ||
| import threading | ||
| import time | ||
| import os | ||
| import logging | ||
|
|
||
| logging.basicConfig(level=logging.ERROR) | ||
|
|
||
| app = Flask(__name__) | ||
| CORS(app) | ||
|
|
||
| @@ -61,9 +63,10 @@ | ||
| 'data': progress_data | ||
| }), 200 | ||
| except Exception as e: | ||
| logging.exception("Exception in get_progress endpoint") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': "An internal error has occurred." | ||
| }), 500 | ||
|
|
||
|
|
||
| @@ -87,9 +88,10 @@ | ||
| 'data': recipes_data | ||
| }), 200 | ||
| except Exception as e: | ||
| logging.exception("Exception in get_recipes endpoint") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': "An internal error has occurred." | ||
| }), 500 | ||
|
|
||
|
|
||
| @@ -138,9 +138,10 @@ | ||
| } | ||
| }), 200 | ||
| except Exception as e: | ||
| logging.exception("Exception in deliver_recipe endpoint") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': "An internal error has occurred." | ||
| }), 500 | ||
|
|
||
|
|
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| }), 500 |
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 9 months ago
To fix the problem, replace the direct use of str(e) in the API responses with a generic error message for the client, and log the exception details on the server side for later debugging. Since we should only make changes inside the provided code snippet, the best approach is to use Python's built-in logging module to log the exception (with stack trace), and return a generic error to the client.
Specifically, you should:
- Add an import for the
loggingmodule if not already present. - In each exception handler (lines 140, 156, 172), replace the current
jsonify({ 'success': False, 'error': str(e) })with:- A call to log the exception (including its traceback).
- A JSON response with a generic error message, like "An internal error occurred."
- Ensure the logger is appropriately configured (at least at the module level).
These changes should be made in the main.py file, specifically in the exception handlers in the /api/deliver, /api/start, and /api/stop endpoints and relevant import/configuration locations.
| @@ -7,10 +7,13 @@ | ||
| import threading | ||
| import time | ||
| import os | ||
| import logging | ||
|
|
||
| app = Flask(__name__) | ||
| CORS(app) | ||
|
|
||
| # Configure logging | ||
| logging.basicConfig(level=logging.INFO) | ||
| # Initialize game data | ||
| tomato = KitchenObjectSO("Tomato", 1) | ||
| lettuce = KitchenObjectSO("Lettuce", 2) | ||
| @@ -138,9 +137,10 @@ | ||
| } | ||
| }), 200 | ||
| except Exception as e: | ||
| logging.exception("Exception occurred in /api/deliver endpoint") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': 'An internal error has occurred.' | ||
| }), 500 | ||
|
|
||
|
|
||
| @@ -154,9 +152,10 @@ | ||
| 'message': 'Game started' | ||
| }), 200 | ||
| except Exception as e: | ||
| logging.exception("Exception occurred in /api/start endpoint") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': 'An internal error has occurred.' | ||
| }), 500 | ||
|
|
||
|
|
||
| @@ -170,9 +167,10 @@ | ||
| 'message': 'Game stopped' | ||
| }), 200 | ||
| except Exception as e: | ||
| logging.exception("Exception occurred in /api/stop endpoint") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': 'An internal error has occurred.' | ||
| }), 500 | ||
|
|
||
|
|
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| }), 500 |
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 9 months ago
To fix this issue, the API endpoint's exception handling should be modified to avoid exposing internal exception messages (str(e)) to clients. Instead, the application should return a generic error message (such as "An internal error has occurred."), while logging the detailed exception information server-side for debugging. The logging should be performed using Python's standard logging module. The fix requires:
- Importing the
loggingmodule at the top of the file. - Initializing a logger for the Flask app or module.
- In every affected route (
/api/deliver,/api/start,/api/stop), replace theerror: str(e)field in the exception handler response with a generic message, and calllogger.exception(...)to log the actual error and stack trace, ideally indicating which endpoint or operation failed.
Only main.py needs editing:
- Add import and logger setup at the top.
- Update exception handlers in the three mentioned endpoints.
| @@ -7,10 +7,10 @@ | ||
| import threading | ||
| import time | ||
| import os | ||
| import logging | ||
|
|
||
| app = Flask(__name__) | ||
| CORS(app) | ||
|
|
||
| # Initialize game data | ||
| tomato = KitchenObjectSO("Tomato", 1) | ||
| lettuce = KitchenObjectSO("Lettuce", 2) | ||
| @@ -138,9 +135,10 @@ | ||
| } | ||
| }), 200 | ||
| except Exception as e: | ||
| logger.exception("Exception during /api/deliver") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': 'An internal error has occurred.' | ||
| }), 500 | ||
|
|
||
|
|
||
| @@ -154,9 +150,10 @@ | ||
| 'message': 'Game started' | ||
| }), 200 | ||
| except Exception as e: | ||
| logger.exception("Exception during /api/start") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': 'An internal error has occurred.' | ||
| }), 500 | ||
|
|
||
|
|
||
| @@ -170,9 +165,10 @@ | ||
| 'message': 'Game stopped' | ||
| }), 200 | ||
| except Exception as e: | ||
| logger.exception("Exception during /api/stop") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': 'An internal error has occurred.' | ||
| }), 500 | ||
|
|
||
|
|
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| }), 500 |
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 9 months ago
To fix this issue, generic error messages should be sent to the user, while the detailed exception (including stack trace) should only be logged server-side for debugging. The ideal approach is to:
- Replace all return statements that send
str(e)(or raw exception info) to the client with a static, generic message (e.g.,"An internal error has occurred."). - Add code to log exception details securely on the server for later inspection. Since the code does not show an existing logger, but Python provides a built-in
loggingmodule, it's reasonable to use it. - The code requiring changes are the
except Exception as eblocks and their corresponding return statements in thedeliver_recipe(),start_game(), andstop_game()functions inmain.py. - Also, an import of the
loggingmodule should be added, and it’s best to initialize logging at the top of the file, close to other imports.
| @@ -7,6 +7,7 @@ | ||
| import threading | ||
| import time | ||
| import os | ||
| import logging | ||
|
|
||
| app = Flask(__name__) | ||
| CORS(app) | ||
| @@ -138,9 +139,10 @@ | ||
| } | ||
| }), 200 | ||
| except Exception as e: | ||
| logging.exception("Error in deliver_recipe") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': "An internal server error has occurred." | ||
| }), 500 | ||
|
|
||
|
|
||
| @@ -154,9 +154,10 @@ | ||
| 'message': 'Game started' | ||
| }), 200 | ||
| except Exception as e: | ||
| logging.exception("Error in start_game") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': "An internal server error has occurred." | ||
| }), 500 | ||
|
|
||
|
|
||
| @@ -170,9 +169,10 @@ | ||
| 'message': 'Game stopped' | ||
| }), 200 | ||
| except Exception as e: | ||
| logging.exception("Error in stop_game") | ||
| return jsonify({ | ||
| 'success': False, | ||
| 'error': str(e) | ||
| 'error': "An internal server error has occurred." | ||
| }), 500 | ||
|
|
||
|
|
Co-authored-by: moulongzhang <39043782+moulongzhang@users.noreply.github.com>
Co-authored-by: moulongzhang <39043782+moulongzhang@users.noreply.github.com>
|
@copilot テストも実装してください |
テストは既に実装済みです。 ✅ test_get_progress - 進捗データ取得 実行方法: |
Adds REST API backend and web frontend to enable real-time progress tracking, recipe management, and work completion updates with error handling.
Backend (
main.py)threading.Eventfor continuous game state updates{success: bool, data: any, error: str}Frontend (
index.html)API Examples
Testing
UI Preview
Initial state with waiting recipes:

After successful delivery:

Dependencies
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.