Add a nicer response for TimeoutError

This commit is contained in:
Timotej Lazar 2023-07-07 10:15:02 +02:00
parent 6b72316076
commit dd607dbddd
6 changed files with 28 additions and 3 deletions

View file

@ -96,6 +96,8 @@ def create_app(test_config=None):
version = db.load('settings').get('version') version = db.load('settings').get('version')
nodes = db.read('nodes') nodes = db.read('nodes')
return flask.render_template('nodes.html', version=version, nodes=nodes) return flask.render_template('nodes.html', version=version, nodes=nodes)
except TimeoutError:
return flask.render_template('busy.html')
except Exception as e: except Exception as e:
return flask.Response(f'something went catastrophically wrong: {e}', return flask.Response(f'something went catastrophically wrong: {e}',
status=400, mimetype='text/plain') status=400, mimetype='text/plain')

View file

@ -21,6 +21,8 @@ def index():
system.run(system.save_config) system.run(system.save_config)
settings = db.read('settings') settings = db.read('settings')
return flask.render_template('config/index.html', **locals()) return flask.render_template('config/index.html', **locals())
except TimeoutError:
return flask.render_template('busy.html')
except Exception as e: except Exception as e:
return flask.Response(f'something went catastrophically wrong: {e}', return flask.Response(f'something went catastrophically wrong: {e}',
status=400, mimetype='text/plain') status=400, mimetype='text/plain')
@ -37,6 +39,8 @@ def edit(name):
system.run(system.save_config) system.run(system.save_config)
content = json.dumps(db.load(name), indent=2) content = json.dumps(db.load(name), indent=2)
return flask.render_template('config/edit.html', **locals()) return flask.render_template('config/edit.html', **locals())
except TimeoutError:
return flask.render_template('busy.html')
except Exception as e: except Exception as e:
return flask.Response(f'something went catastrophically wrong: {e}', return flask.Response(f'something went catastrophically wrong: {e}',
status=400, mimetype='text/plain') status=400, mimetype='text/plain')

View file

@ -27,7 +27,8 @@ def index():
return flask.redirect(flask.url_for('nat.index')) return flask.redirect(flask.url_for('nat.index'))
return flask.render_template('nat/index.html', nat=nat) return flask.render_template('nat/index.html', nat=nat)
except TimeoutError:
return flask.render_template('busy.html')
except Exception as e: except Exception as e:
return flask.Response(f'something went catastrophically wrong: {e}', return flask.Response(f'something went catastrophically wrong: {e}',
status=400, mimetype='text/plain') status=400, mimetype='text/plain')

View file

@ -27,6 +27,8 @@ def index():
system.run(system.save_config) system.run(system.save_config)
return flask.render_template('rules/index.html', rules=db.load('rules')) return flask.render_template('rules/index.html', rules=db.load('rules'))
except TimeoutError:
return flask.render_template('busy.html')
except Exception as e: except Exception as e:
return flask.Response(f'something went catastrophically wrong: {e}', return flask.Response(f'something went catastrophically wrong: {e}',
status=400, mimetype='text/plain') status=400, mimetype='text/plain')
@ -51,6 +53,8 @@ def edit(index):
return flask.render_template('rules/edit.html', index=index, rule=db.load('rules')[index]) return flask.render_template('rules/edit.html', index=index, rule=db.load('rules')[index])
except IndexError as e: except IndexError as e:
return flask.Response(f'invalid rule: {index}', status=400, mimetype='text/plain') return flask.Response(f'invalid rule: {index}', status=400, mimetype='text/plain')
except TimeoutError:
return flask.render_template('busy.html')
except Exception as e: except Exception as e:
return flask.Response(f'something went catastrophically wrong: {e}', return flask.Response(f'something went catastrophically wrong: {e}',
status=400, mimetype='text/plain') status=400, mimetype='text/plain')
@ -65,6 +69,8 @@ def manage():
rules = [rule|{'index': index} for index, rule in enumerate(db.load('rules')) rules = [rule|{'index': index} for index, rule in enumerate(db.load('rules'))
if can_toggle(flask_login.current_user, rule)] if can_toggle(flask_login.current_user, rule)]
return flask.render_template('rules/manage.html', rules=rules) return flask.render_template('rules/manage.html', rules=rules)
except TimeoutError:
return flask.render_template('busy.html')
except Exception as e: except Exception as e:
return flask.Response(f'something went catastrophically wrong: {e}', return flask.Response(f'something went catastrophically wrong: {e}',
status=400, mimetype='text/plain') status=400, mimetype='text/plain')
@ -83,6 +89,8 @@ def toggle(index, enable):
return flask.redirect(flask.url_for('rules.manage')) return flask.redirect(flask.url_for('rules.manage'))
except IndexError as e: except IndexError as e:
return flask.Response(f'invalid rule: {index}', status=400, mimetype='text/plain') return flask.Response(f'invalid rule: {index}', status=400, mimetype='text/plain')
except TimeoutError:
return flask.render_template('busy.html')
except Exception as e: except Exception as e:
return flask.Response(f'something went catastrophically wrong: {e}', return flask.Response(f'something went catastrophically wrong: {e}',
status=400, mimetype='text/plain') status=400, mimetype='text/plain')

6
web/templates/busy.html Normal file
View file

@ -0,0 +1,6 @@
{% extends 'base.html' %}
{% block content %}
<p>
Strežnik je zaseden, poskusite znova pozneje.
{% endblock %}

View file

@ -24,6 +24,8 @@ def list():
try: try:
user = flask_login.current_user.get_id() user = flask_login.current_user.get_id()
return flask.jsonify({k: v for k, v in db.load('wireguard').items() if v.get('user') == user}) return flask.jsonify({k: v for k, v in db.load('wireguard').items() if v.get('user') == user})
except TimeoutError:
return flask.render_template('busy.html')
except Exception as e: except Exception as e:
return flask.Response(f'failed: {e}', status=500, mimetype='text/plain') return flask.Response(f'failed: {e}', status=500, mimetype='text/plain')
@ -75,7 +77,8 @@ def new():
'use_dns': flask.request.json.get('use_dns', True), 'use_dns': flask.request.json.get('use_dns', True),
} }
return flask.render_template('vpn/wg-fri.conf', **args) return flask.render_template('vpn/wg-fri.conf', **args)
except TimeoutError:
return flask.render_template('busy.html')
except Exception as e: except Exception as e:
return flask.Response(f'something went catastrophically wrong: {e}', return flask.Response(f'something went catastrophically wrong: {e}',
status=400, mimetype='text/plain') status=400, mimetype='text/plain')
@ -96,7 +99,8 @@ def delete():
system.run(system.save_config) system.run(system.save_config)
return flask.Response(f'deleted key {pubkey}', status=200, mimetype='text/plain') return flask.Response(f'deleted key {pubkey}', status=200, mimetype='text/plain')
except TimeoutError:
return flask.render_template('busy.html')
except Exception as e: except Exception as e:
return flask.Response(f'something went catastrophically wrong: {e}', return flask.Response(f'something went catastrophically wrong: {e}',
status=400, mimetype='text/plain') status=400, mimetype='text/plain')