diff --git a/app.py b/app.py index c4be984..8470d64 100644 --- a/app.py +++ b/app.py @@ -197,6 +197,11 @@ def update_servers_background(): update_thread = threading.Thread(target=update_servers_background, daemon=True) update_thread.start() +@app.errorhandler(Exception) +def handle_unexpected_error(error): + print(f"Unhandled error: {error}", flush=True) + return jsonify({"error": "Internal server error"}), 500 + @app.route('/') def index(): return send_from_directory('static', 'index.html') @@ -209,7 +214,9 @@ def get_servers(): @app.route('/api/update-locations', methods=['POST']) def update_locations_endpoint(): try: - generate_locations() + ok = generate_locations() + if not ok: + raise RuntimeError("Location generation failed") load_cache() @@ -255,4 +262,5 @@ def run_test(): if __name__ == '__main__': load_cache() - app.run(host='0.0.0.0', port=5000, debug=True) + debug_enabled = os.getenv('FLASK_DEBUG', '0') == '1' + app.run(host='0.0.0.0', port=5000, debug=debug_enabled) diff --git a/docker-compose.yml b/docker-compose.yml index 6aaa7ea..3d2605a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,4 +3,6 @@ services: build: . ports: - "5000:5000" + environment: + - FLASK_DEBUG=0 restart: unless-stopped \ No newline at end of file diff --git a/generate_locations.py b/generate_locations.py index 1bd8a62..c4d467e 100644 --- a/generate_locations.py +++ b/generate_locations.py @@ -31,7 +31,7 @@ def generate_locations(): servers = response.json() except Exception as e: print(f"Failed to fetch server list: {e}") - return + raise RuntimeError(f"Failed to fetch server list: {e}") from e print(f"Found {len(servers)} servers.") @@ -95,10 +95,15 @@ def generate_locations(): sorted_results = dict(sorted(results.items())) - with open('prefilled_locations.json', 'w', encoding='utf-8') as f: - json.dump(sorted_results, f, indent=2) + try: + with open('prefilled_locations.json', 'w', encoding='utf-8') as f: + json.dump(sorted_results, f, indent=2) + except Exception as e: + print(f"Failed to write prefilled_locations.json: {e}") + raise RuntimeError(f"Failed to write prefilled_locations.json: {e}") from e print("Saved to prefilled_locations.json") + return True if __name__ == "__main__": sys.stdout = open('gen_log.txt', 'w', encoding='utf-8') diff --git a/static/script.js b/static/script.js index 42e634c..d77b3ba 100644 --- a/static/script.js +++ b/static/script.js @@ -144,7 +144,22 @@ document.addEventListener('DOMContentLoaded', () => { updateBtn.disabled = true; fetch('/api/update-locations', { method: 'POST' }) - .then(res => res.json()) + .then(async res => { + const contentType = res.headers.get('content-type') || ''; + if (contentType.includes('application/json')) { + const data = await res.json(); + if (!res.ok) { + throw new Error(data.error || `HTTP ${res.status}`); + } + return data; + } + + const text = await res.text(); + if (!res.ok) { + throw new Error(text || `HTTP ${res.status}`); + } + throw new Error(text || 'Unexpected non-JSON response'); + }) .then(data => { if (data.error) { alert("Error: " + data.error);