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>
This commit is contained in:
Adam DaleyandCopilot authored and GitHub committed 2026-01-26 01:03:54 +00:00
1 parent 217b32e440
commit aefb9e7ea1
4 files changed
+52 -24

No files matched your search

+5 -3
View File
@@ -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;
}
@@ -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);
}
});
});
});
</script>
{% endblock %}
@@ -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();
+25
View File
@@ -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
*/