diff --git a/src/howitz/endpoints.py b/src/howitz/endpoints.py index 3d7519ef..cae97cc3 100644 --- a/src/howitz/endpoints.py +++ b/src/howitz/endpoints.py @@ -1,5 +1,4 @@ import os -import uuid from flask import ( Blueprint, @@ -24,7 +23,7 @@ from zinolib.ritz import NotConnectedError from howitz.users.utils import authenticate_user -from .utils import login_check, serialize_exception +from .utils import login_check main = Blueprint('main', __name__) @@ -88,7 +87,6 @@ def get_current_events(): if session["not_connected_counter"] > 1: # This error is not intermittent - increase counter and handle current_app.logger.exception('Recurrent NotConnectedError %s', notConnErr) session["not_connected_counter"] += 1 - show_error_popup(notConnErr, 'An error occurred, please check your connection status to Zino') raise else: # This error is intermittent - increase counter and retry current_app.logger.exception('Intermittent NotConnectedError %s', notConnErr) @@ -120,7 +118,6 @@ def poll_current_events(): if session["not_connected_counter"] > 1: # This error is not intermittent - increase counter and handle current_app.logger.exception('Recurrent NotConnectedError %s', notConnErr) session["not_connected_counter"] += 1 - show_error_popup(notConnErr, 'An error occurred, please check your connection status to Zino') raise else: # This error is intermittent - increase counter and retry current_app.logger.exception('Intermittent NotConnectedError %s', notConnErr) @@ -209,7 +206,6 @@ def get_event_attributes(id, res_format=dict): event = current_app.event_manager.create_event_from_id(int(id)) except RetryError as retryErr: # Intermittent error in Zino current_app.logger.exception('RetryError when fetching event attributes %s', retryErr) - show_error_popup(retryErr, 'Could not fetch event attributes, please retry') raise event_dict = vars(event) @@ -227,7 +223,6 @@ def get_event_details(id): event_attr = vars(current_app.event_manager.create_event_from_id(int(id))) except RetryError as retryErr: # Intermittent error in Zino current_app.logger.exception('RetryError when fetching event details %s', retryErr) - show_error_popup(retryErr, 'Could not fetch event details, please retry') raise event_logs = current_app.event_manager.get_log_for_id(int(id)) @@ -238,16 +233,6 @@ def get_event_details(id): return event_attr, event_logs, event_history, event_msgs -def show_error_popup(error, short_description): - alert_random_id = str(uuid.uuid4()) - - session["errors"][alert_random_id] = serialize_exception(error) - session.modified = True - - return render_template('/components/popups/alerts/error/error-alert.html', - alert_id=alert_random_id, short_err_msg=short_description) - - @main.route('/') @main.route('/events') @login_check() @@ -342,7 +327,6 @@ def expand_event_row(event_id): eventobj = current_app.event_manager.create_event_from_id(event_id) except RetryError as retryErr: # Intermittent error in Zino current_app.logger.exception('RetryError on row expand after retry, %s', retryErr) - show_error_popup(retryErr, 'Could not expand event, please retry') raise event = create_table_event(eventobj) @@ -372,7 +356,6 @@ def collapse_event_row(event_id): eventobj = current_app.event_manager.create_event_from_id(event_id) except RetryError as retryErr: # Intermittent error in Zino current_app.logger.exception('RetryError on row collapse %s', retryErr) - show_error_popup(retryErr, 'Could not collapse event, please retry') raise event = create_table_event(eventobj) diff --git a/src/howitz/error_handlers.py b/src/howitz/error_handlers.py index 7f44b4c2..bdadc609 100644 --- a/src/howitz/error_handlers.py +++ b/src/howitz/error_handlers.py @@ -1,26 +1,29 @@ import uuid -from flask import json, render_template, session, current_app +from flask import render_template, session, current_app, make_response from werkzeug.exceptions import HTTPException from howitz.utils import serialize_exception -# Fixme add non-generic error handling as well def handle_generic_http_exception(e): current_app.logger.exception("An unexpected HTTP exception has occurred %s", e) - """Return JSON instead of HTML for HTTP errors.""" - # start with the correct headers and status code from the error - response = e.get_response() - # replace the body with JSON - response.data = json.dumps({ - "code": e.code, - "name": e.name, - "description": e.description, - }) - response.content_type = "application/json" - return response + alert_random_id = str(uuid.uuid4()) + short_err_msg = f"{e.code} {e.name}: {e.description}" + + if not "errors" in session: + session["errors"] = dict() + session["errors"][str(alert_random_id)] = serialize_exception(e) + session.modified = True + current_app.logger.debug('ERRORS %s', session["errors"]) + + response = make_response(render_template('/components/popups/alerts/error/error-alert.html', + alert_id=alert_random_id, short_err_msg=short_err_msg)) + + response.headers['HX-Reswap'] = 'beforeend' + + return response, e.code def handle_generic_exception(e): @@ -30,7 +33,10 @@ def handle_generic_exception(e): # now you're handling non-HTTP exceptions only alert_random_id = str(uuid.uuid4()) - short_err_msg = 'An unexpected error has occurred' + try: + short_err_msg = e.args[0] + except IndexError: + short_err_msg = 'An unexpected error has occurred' if not "errors" in session: session["errors"] = dict() @@ -39,8 +45,12 @@ def handle_generic_exception(e): current_app.logger.debug('ERRORS %s', session["errors"]) current_app.logger.exception("An unexpected exception has occurred %s", e) - return render_template('/components/popups/alerts/error/error-alert.html', - alert_id=alert_random_id, short_err_msg=short_err_msg) + response = make_response(render_template('/components/popups/alerts/error/error-alert.html', + alert_id=alert_random_id, short_err_msg=short_err_msg)) + + response.headers['HX-Reswap'] = 'beforeend' + + return response, 500 def handle_400(e): diff --git a/src/howitz/templates/components/popups/alerts/error/error-alert.html b/src/howitz/templates/components/popups/alerts/error/error-alert.html index 02754f9e..8fae40ee 100644 --- a/src/howitz/templates/components/popups/alerts/error/error-alert.html +++ b/src/howitz/templates/components/popups/alerts/error/error-alert.html @@ -1,19 +1,19 @@