python: send html error page on Packages errors

Like the C bridge send a proper formatted `fail.html` back to the
client. This does not handle anything any other then 404's.

Co-authored-by: Jelle van der Waa <jvanderwaa@redhat.com>
This commit is contained in:
Allison Karlitskaya 2023-01-26 16:04:03 +01:00 committed by Martin Pitt
parent 340b78dbc8
commit 04a13a1395
3 changed files with 10 additions and 2 deletions

View File

@ -14,9 +14,13 @@ where = ["src"]
include = [
"cockpit",
"cockpit.channels",
"cockpit.data",
"systemd_ctypes",
]
[tool.setuptools.package-data]
"cockpit.data" = ["*.html"]
[tool.mypy]
mypy_path = 'src'

View File

@ -48,8 +48,11 @@ class PackagesChannel(Channel):
self.send_message(status=200, reason='OK', headers={k: v for k, v in headers.items() if v is not None})
def http_error(self, status, message):
self.send_message(status=status, reason='ERROR')
self.send_data(message.encode('utf-8'))
# with (importlib.resources.file('cockpit.data') / 'data' / 'fail.html').open() ... (from py3.7)
fail_path = __file__.removesuffix('/channels/packages.py') + '/data/fail.html'
template = __loader__.get_data(fail_path)
self.send_message(status=status, reason='ERROR', headers={'Content-Type': 'text/html; charset=utf-8'})
self.send_data(template.replace(b'@@message@@', message.encode('utf-8')))
def do_done(self):
assert not self.post

1
src/cockpit/data/fail.html Symbolic link
View File

@ -0,0 +1 @@
../../../src/common/fail.html