From aefb9e7ea1af5c3d74f05a36ee2c52e033fcedd9 Mon Sep 17 00:00:00 2001 From: Adam Daley Date: Mon, 26 Jan 2026 01:03:54 +0000 Subject: [PATCH] Improve API/JS error handling (#3206) * Remove tab state management via URL hash * Add global error handling to `admin_defaul`t and `huraga` themes Introduced global handlers for unhandled Promise rejections and synchronous errors in both `admin_default` and `huraga` themes to improve error reporting. Updated `API.js` to handle errors more gracefully during API requests and when parsing API configuration. * Remove prevention of default unhandledrejection handling * Copilot suggested fix Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Refactor error handling in API.js * Update src/themes/admin_default/assets/js/fossbilling.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/library/Api/API.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/library/Api/API.js | 8 +++--- .../html_admin/mod_product_manage.html.twig | 20 --------------- .../admin_default/assets/js/fossbilling.js | 23 ++++++++++++++++- src/themes/huraga/assets/huraga.js | 25 +++++++++++++++++++ 4 files changed, 52 insertions(+), 24 deletions(-) diff --git a/src/library/Api/API.js b/src/library/Api/API.js index 4550b1b1d..a370bb637 100644 --- a/src/library/Api/API.js +++ b/src/library/Api/API.js @@ -452,9 +452,10 @@ const API = { errorHandler(errorObj); } else { console.warn('No error handler was specified for API error.'); + const normalizedError = new Error(errorObj.message); + normalizedError.code = errorObj.code; + throw normalizedError; } - - throw error; }); }, @@ -608,7 +609,8 @@ const API = { try { apiData = Tools.parseDataAttr(linkElement.dataset.fbApi || '{}'); } catch (error) { - console.warn('Invalid JSON in data-fb-api attribute:', error); + console.error('Failed to parse data-fb-api attribute:', error); + FOSSBilling.message('Invalid API configuration', 'error'); return; } diff --git a/src/modules/Product/html_admin/mod_product_manage.html.twig b/src/modules/Product/html_admin/mod_product_manage.html.twig index b81a9b751..efcc7e0e5 100644 --- a/src/modules/Product/html_admin/mod_product_manage.html.twig +++ b/src/modules/Product/html_admin/mod_product_manage.html.twig @@ -345,26 +345,6 @@ btn.setAttribute('href', "{{ 'extension/settings/formbuilder'|alink }}"); } } - - const urlHash = window.location.hash; - if (urlHash && urlHash.startsWith('#tab-')) { - const tabId = urlHash.substring(5); - const tabEl = document.querySelector(`[data-bs-target="#tab-${tabId}"]`); - if (tabEl) { - const tab = new bootstrap.Tab(tabEl); - tab.show(); - } - } - - document.querySelectorAll('.nav-tabs .nav-link').forEach(tabEl => { - tabEl.addEventListener('shown.bs.tab', function(e) { - const targetId = e.target.getAttribute('data-bs-target'); - if (targetId && targetId.startsWith('#tab-')) { - const tabName = targetId.substring(5); - history.replaceState(null, null, '#' + tabName); - } - }); - }); }); {% endblock %} diff --git a/src/themes/admin_default/assets/js/fossbilling.js b/src/themes/admin_default/assets/js/fossbilling.js index 8405e899d..b3635efab 100644 --- a/src/themes/admin_default/assets/js/fossbilling.js +++ b/src/themes/admin_default/assets/js/fossbilling.js @@ -82,7 +82,28 @@ globalThis.FOSSBilling = { }; //===== Global ajax methods =====// - document.addEventListener("DOMContentLoaded", function() { + document.addEventListener('DOMContentLoaded', function() { + // Global error handler for unhandled Promise rejections + window.addEventListener('unhandledrejection', function(event) { + const error = event.reason; + let message = 'An unexpected error occurred'; + if (error && typeof error === 'object') { + message = error.message || error.code || message; + } else if (typeof error === 'string') { + message = error; + } + FOSSBilling.message(message, 'error'); + }); + + // Global error handler for synchronous errors + window.addEventListener('error', function(event) { + let displayMessage = event && event.message ? event.message : 'An unexpected error occurred'; + if (event && event.error && event.error.message) { + displayMessage = event.error.message; + } + FOSSBilling.message(displayMessage, 'error'); + }); + // Attach event listeners to all forms and links with data-fb-api attribute. if (document.querySelector("form[data-fb-api]")) { API._apiForm(); diff --git a/src/themes/huraga/assets/huraga.js b/src/themes/huraga/assets/huraga.js index 0940a44f9..4554c4ba0 100644 --- a/src/themes/huraga/assets/huraga.js +++ b/src/themes/huraga/assets/huraga.js @@ -5,6 +5,31 @@ import './js/utils'; globalThis.bootstrap = { Tooltip, Toast }; document.addEventListener('DOMContentLoaded', () => { + /** + * Global error handler for unhandled Promise rejections + */ + window.addEventListener('unhandledrejection', function(event) { + const error = event.reason; + let message = 'An unexpected error occurred'; + if (error && typeof error === 'object') { + message = error.message || error.code || message; + } else if (typeof error === 'string') { + message = error; + } + FOSSBilling.message(message, 'error'); + }); + + /** + * Global error handler for synchronous errors + */ + window.onerror = function(message, source, lineno, colno, error) { + let displayMessage = message; + if (error && error.message) { + displayMessage = error.message; + } + FOSSBilling.message(displayMessage, 'error'); + }; + /** * Enable Bootstrap Tooltip */