mirror of
https://github.com/R0GGER/iperf3-map.git
synced 2026-11-11 01:20:06 +01:00
Improve error handling.
This commit is contained in:
1 parent
f1993294c9
commit
6b68c55f02
4 files changed
+36
-6
No files matched your search
@@ -197,6 +197,11 @@ def update_servers_background():
|
|||||||
update_thread = threading.Thread(target=update_servers_background, daemon=True)
|
update_thread = threading.Thread(target=update_servers_background, daemon=True)
|
||||||
update_thread.start()
|
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('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
return send_from_directory('static', 'index.html')
|
return send_from_directory('static', 'index.html')
|
||||||
@@ -209,7 +214,9 @@ def get_servers():
|
|||||||
@app.route('/api/update-locations', methods=['POST'])
|
@app.route('/api/update-locations', methods=['POST'])
|
||||||
def update_locations_endpoint():
|
def update_locations_endpoint():
|
||||||
try:
|
try:
|
||||||
generate_locations()
|
ok = generate_locations()
|
||||||
|
if not ok:
|
||||||
|
raise RuntimeError("Location generation failed")
|
||||||
|
|
||||||
load_cache()
|
load_cache()
|
||||||
|
|
||||||
@@ -255,4 +262,5 @@ def run_test():
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
load_cache()
|
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)
|
||||||
@@ -3,4 +3,6 @@ services:
|
|||||||
build: .
|
build: .
|
||||||
ports:
|
ports:
|
||||||
- "5000:5000"
|
- "5000:5000"
|
||||||
|
environment:
|
||||||
|
- FLASK_DEBUG=0
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
@@ -31,7 +31,7 @@ def generate_locations():
|
|||||||
servers = response.json()
|
servers = response.json()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to fetch server list: {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.")
|
print(f"Found {len(servers)} servers.")
|
||||||
|
|
||||||
@@ -95,10 +95,15 @@ def generate_locations():
|
|||||||
|
|
||||||
sorted_results = dict(sorted(results.items()))
|
sorted_results = dict(sorted(results.items()))
|
||||||
|
|
||||||
with open('prefilled_locations.json', 'w', encoding='utf-8') as f:
|
try:
|
||||||
json.dump(sorted_results, f, indent=2)
|
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")
|
print("Saved to prefilled_locations.json")
|
||||||
|
return True
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.stdout = open('gen_log.txt', 'w', encoding='utf-8')
|
sys.stdout = open('gen_log.txt', 'w', encoding='utf-8')
|
||||||
|
|||||||
+16
-1
@@ -144,7 +144,22 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
updateBtn.disabled = true;
|
updateBtn.disabled = true;
|
||||||
|
|
||||||
fetch('/api/update-locations', { method: 'POST' })
|
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 => {
|
.then(data => {
|
||||||
if (data.error) {
|
if (data.error) {
|
||||||
alert("Error: " + data.error);
|
alert("Error: " + data.error);
|
||||||
|
|||||||
Reference in new issue
Block a user