diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2022-01-23 17:13:49 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2022-01-23 17:13:49 +0300 |
commit | d4ccff8141cc4325d5ffc8890a7be96c0a7f12e7 (patch) | |
tree | c426dceeaf31822530032382d0b147e2ba0e378b | |
parent | app.py: fix loginctl output parsing (diff) | |
download | linux-status-d4ccff8141cc4325d5ffc8890a7be96c0a7f12e7.tar.gz linux-status-d4ccff8141cc4325d5ffc8890a7be96c0a7f12e7.zip |
dump exceptions to browser's console
-rwxr-xr-x | app.py | 43 | ||||
-rw-r--r-- | index.html | 17 | ||||
-rwxr-xr-x | server.py | 10 |
3 files changed, 43 insertions, 27 deletions
@@ -70,18 +70,23 @@ def hostname(): class Response: - def __init__(self, data): - self.data = data + DEFAULT_STATUS = http.server.HTTPStatus.OK + + @staticmethod + def body_from_json(body): + return json.dumps(body, ensure_ascii=False, indent=4) + + def __init__(self, body, status=None): + if status is None: + status = Response.DEFAULT_STATUS + self.status = status + self.body = body def headers(self): yield 'Content-Type', 'text/html; charset=utf-8' - @staticmethod - def dump_json(data): - return json.dumps(data, ensure_ascii=False, indent=4) - - def body(self): - return self.dump_json(self.data) + def encode_body(self): + return self.body.encode(errors='replace') def write_as_cgi_script(self): self.write_headers_as_cgi_script() @@ -93,22 +98,22 @@ class Response: print() def write_body_as_cgi_script(self): - if self.data is not None: - print(self.body()) + if self.body is not None: + print(self.body) - def write_as_request_handler(self, handler): - handler.send_response(http.server.HTTPStatus.OK) - self.write_headers_as_request_handler(handler) - self.write_body_as_request_handler(handler) + def write_to_request_handler(self, handler): + handler.send_response(self.status) + self.write_headers_to_request_handler(handler) + self.write_body_to_request_handler(handler) - def write_headers_as_request_handler(self, handler): + def write_headers_to_request_handler(self, handler): for name, val in self.headers(): handler.send_header(name, val) handler.end_headers() - def write_body_as_request_handler(self, handler): - if self.data is not None: - handler.wfile.write(self.body().encode(errors='replace')) + def write_body_to_request_handler(self, handler): + if self.body is not None: + handler.wfile.write(self.encode_body()) def run_do(*args, **kwargs): @@ -195,7 +200,7 @@ class Loginctl(Systemd): class Task(abc.ABC): def complete(self): self.run() - return Response(self.result()) + return Response(Response.body_from_json(self.result())) @abc.abstractmethod def run(self): @@ -64,12 +64,21 @@ h1, .h1 { <script src="js/jquery-3.3.1.min.js"></script> <script src="js/bootstrap.bundle.min.js"></script> <script> +function dump_fail(data) { + console.log('Response code was: ' + data.status + ' ' + data.statusText); + console.log('Response was:\n' + data.responseText); +} + +function get(url, success_callback) { + $.get(url, success_callback).fail(dump_fail); +} + function reboot() { - $.get('reboot'); + get('reboot'); } function shutdown() { - $.get('poweroff'); + get('poweroff'); } function set_hostname(data) { @@ -82,7 +91,7 @@ function set_top(data) { } function refresh_top() { - $.get('top', function(data) { + get('top', function(data) { set_top(JSON.parse(data)); }); } @@ -183,7 +192,7 @@ function set_users(data) { } function refresh_status() { - $.get('status', function(data) { + get('status', function(data) { data = JSON.parse(data); set_hostname(data['hostname']); set_system(data['system']); @@ -12,8 +12,9 @@ import argparse import http.server import os import sys +import traceback -from app import Request +from app import Request, Response DEFAULT_PORT = 18101 @@ -31,11 +32,12 @@ class RequestHandler(http.server.SimpleHTTPRequestHandler): return super().do_GET() try: response = request.process() + response.write_to_request_handler(self) except: - self.send_response(http.server.HTTPStatus.INTERNAL_SERVER_ERROR) - self.end_headers() + status = http.server.HTTPStatus.INTERNAL_SERVER_ERROR + response = Response(traceback.format_exc(), status) + response.write_to_request_handler(self) return - response.write_as_request_handler(self) def parse_args(args=None): |