From b6124f0da134294fb1ec2255a3a15497ceb80e76 Mon Sep 17 00:00:00 2001 From: Michiel Kodde Date: Wed, 10 Apr 2024 15:56:28 +0200 Subject: [PATCH] Implement error handling in JS --- assets/app.js | 9 +---- assets/error_handling.js | 35 +++++++++++++++++ templates/Exception/error.html.twig | 45 ++++++---------------- templates/default/authentication.html.twig | 33 ++++++++++++---- templates/default/registration.html.twig | 17 +++++++- templates/default/variables.twig | 43 +++------------------ templates/partial/error_table.html.twig | 39 +++++++++++++++++++ 7 files changed, 132 insertions(+), 89 deletions(-) create mode 100755 assets/error_handling.js create mode 100644 templates/partial/error_table.html.twig diff --git a/assets/app.js b/assets/app.js index e970f045..7b4b1baf 100755 --- a/assets/app.js +++ b/assets/app.js @@ -1,9 +1,2 @@ import './bootstrap.js'; -/* - * Welcome to your app's main JavaScript file! - * - * We recommend including the built version of this JavaScript file - * (and its CSS file) in your base layout (base.html.twig). - */ - -// any CSS you import will output into a single css file (app.css in this case) \ No newline at end of file +import './error_handling' diff --git a/assets/error_handling.js b/assets/error_handling.js new file mode 100755 index 00000000..59adc106 --- /dev/null +++ b/assets/error_handling.js @@ -0,0 +1,35 @@ +window.addEventListener("unhandledrejection", (event) => { + const requestInfo = event.target.requestInformation; + const timestamp = new Date(); + + // Hide the form + document.getElementById("form").classList.add('hidden'); + + // Show the error report + document.getElementById("errorData").classList.remove('hidden'); + let errorFields = document.querySelectorAll('[data-content-for]'); + let written = false; + errorFields.forEach((element) => { + fieldName = element.dataset.contentFor; + written = false; + // 1. The timestamp is filled with the current time + if (fieldName === 'timestamp') { + element.innerHTML = timestamp.toISOString(); + written = true; + } + // Other fields are displayed from the requestInformation that is present in the event + if (requestInfo.hasOwnProperty(fieldName)) { + element.innerHTML = requestInfo[fieldName]; + written = true; + } + // Show the error message from the uncaught exception + if (fieldName === 'errorMessage' && requestInfo['errorMessage'] === undefined ) { + element.innerHTML = event.reason.message; + written = true; + } + // If no sensible data was available to write error field, remove the tr + if (!written) { + element.parentElement.remove(); + } + }); +}); diff --git a/templates/Exception/error.html.twig b/templates/Exception/error.html.twig index 386e630e..11eb8928 100644 --- a/templates/Exception/error.html.twig +++ b/templates/Exception/error.html.twig @@ -6,40 +6,17 @@ {% block body %}

{{ description }}

- - - - - - - - - - - - - - - - - - {% if sari %} - - - - - {% endif %} - {% if user_agent %} - - - - - {% endif %} - - - - -
{{ 'stepup.error.timestamp'|trans }}{{ timestamp }}
{{ 'stepup.error.hostname'|trans }}{{ hostname }}
{{ 'stepup.error.request_id'|trans }}{{ request_id }}
{{ 'stepup.error.error_code'|trans }}{{ errorMessage }}
{{ 'stepup.error.sari'|trans }}{{ sari }}
{{ 'stepup.error.user_agent'|trans }}{{ user_agent }}
{{ 'stepup.error.ip_address'|trans }}{{ ip_address }}
+ {% include 'partial/error_table.html.twig' with + { + 'timestamp': timestamp, + 'hostname': hostname, + 'requestId': request_id, + 'errorMessage': errorMessage, + 'sari': sari, + 'userAgent': user_agent, + 'ipAddress': ip_address + } + %}

{{ 'stepup.error.support_page.text'|trans({'%support_url%': global_view_parameters.supportUrl })|raw }}

diff --git a/templates/default/authentication.html.twig b/templates/default/authentication.html.twig index 6b23e664..7c62334f 100644 --- a/templates/default/authentication.html.twig +++ b/templates/default/authentication.html.twig @@ -3,8 +3,8 @@ {% block body %}

{{ 'authentication.sub_title'|trans }}

-
+ + {{ 'status.authentication_initial'|trans }} + - -
+ + {% include 'partial/error_table.html.twig' with + { + 'timestamp': 'timestamp', + 'hostname': 'hostname', + 'requestId': 'requestId', + 'errorMessage': 'errorMessage', + 'sari': 'sari', + 'userAgent': 'userAgent', + 'ipAddress': 'ipAddress', + } %} + + + + {% include 'default/variables.twig' %} + {% endblock %} diff --git a/templates/default/registration.html.twig b/templates/default/registration.html.twig index 315f96f7..8ba838f5 100755 --- a/templates/default/registration.html.twig +++ b/templates/default/registration.html.twig @@ -1,9 +1,10 @@ + {% extends 'base.html.twig' %} {% block body %}

{{ 'registration.sub_title'|trans }}

-
+ {{ 'status.registration_initial'|trans }} +
+ {% include 'partial/error_table.html.twig' with + { + 'timestamp': 'timestamp', + 'hostname': 'hostname', + 'requestId': 'requestId', + 'errorMessage': 'errorMessage', + 'sari': 'sari', + 'userAgent': 'userAgent', + 'ipAddress': 'ipAddress', + } + %} + - {% endblock %} +{% block javascripts %} + +{% endblock %} diff --git a/templates/partial/error_table.html.twig b/templates/partial/error_table.html.twig new file mode 100644 index 00000000..a130c23d --- /dev/null +++ b/templates/partial/error_table.html.twig @@ -0,0 +1,39 @@ +