Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show custom error page on 502 BadGateway #147

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/howitz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from howitz.config.utils import load_config
from howitz.config.zino1 import make_zino1_config
from howitz.config.howitz import make_howitz_config
from howitz.error_handlers import handle_generic_exception, handle_generic_http_exception, handle_400, handle_404, handle_403, handle_lost_connection
from howitz.error_handlers import handle_generic_exception, handle_generic_http_exception, handle_400, handle_404, handle_403, handle_lost_connection, handle_bad_gateway
from howitz.users.db import UserDB
from howitz.users.commands import user_cli
from zinolib.controllers.zino1 import Zino1EventManager, LostConnectionError, NotConnectedError
Expand All @@ -33,6 +33,7 @@ def create_app(test_config=None):
app.register_error_handler(LostConnectionError, handle_lost_connection)
app.register_error_handler(BrokenPipeError, handle_lost_connection)
app.register_error_handler(NotConnectedError, handle_lost_connection)
app.register_error_handler(502, handle_bad_gateway)

# load config
app = load_config(app, test_config)
Expand Down
16 changes: 15 additions & 1 deletion src/howitz/error_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from flask import render_template, session, current_app, make_response, request
from flask_login import current_user
from werkzeug.exceptions import HTTPException
from werkzeug.exceptions import HTTPException, BadGateway

from howitz.endpoints import connect_to_zino
from howitz.utils import serialize_exception
Expand Down Expand Up @@ -79,6 +79,20 @@ def handle_403(e):
return response, 403


def handle_bad_gateway(e):
current_app.logger.exception("502 Bad Gateway has occurred %s", e)
description = BadGateway.description
try:
description = e.description
except AttributeError:
pass

response = make_response(render_template('responses/502.html', err_msg=description))
response.headers['HX-Retarget'] = 'body'
response.headers['HX-Reswap'] = 'innerHTML'
return response, 502


def handle_lost_connection(e):
if isinstance(e, BrokenPipeError):
current_app.logger.exception("Lost connection to Zino server: %s", e)
Expand Down
11 changes: 11 additions & 0 deletions src/howitz/templates/responses/502.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% block content %}
<main
class="py-8 px-4 mx-auto max-w-screen-xl lg:py-16 lg:px-6 bg-slate-900 text-sky-200/80"
>
<section class="mx-auto max-w-screen-sm text-center">
<h1 class="mb-4 text-7xl tracking-tight font-extrabold lg:text-9xl">502</h1>
<p class="mb-4 text-3xl tracking-tight font-bold md:text-4xl">Bad Gateway</p>
<p class="mb-4 text-lg font-light text-teal-300">{{ err_msg }}</p>
</section>
</main>
{% endblock %}
Comment on lines +1 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Polish: mention that this is a problem on the server, not necessarily a problem with howitz code.