Improve error handling.

This commit is contained in:
rogier committed 2026-01-29 17:14:50 +01:00
1 parent f1993294c9
commit 6b68c55f02
4 files changed
+36 -6

No files matched your search

+10 -2
View File
@@ -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)
+2
View File
@@ -3,4 +3,6 @@ services:
build: .
ports:
- "5000:5000"
environment:
- FLASK_DEBUG=0
restart: unless-stopped
+8 -3
View File
@@ -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')
+16 -1
View File
@@ -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);