Add global HTMX error handler to prevent JS errors when error container is missing

This commit is contained in:
Ross
2026-03-02 10:39:46 +00:00
parent 166b0a0282
commit b3f5684e69
2 changed files with 19 additions and 0 deletions
+5
View File
@@ -20,6 +20,11 @@ $(document).ready(function () {
document.body.addEventListener("htmx:responseError", function(e) {
let el = document.getElementById("htmx-error");
if (!el) {
// No global error container available; log and return to avoid JS errors
console.error('htmx:responseError but no #htmx-error element present', e.detail);
return;
}
var error;
if (e.detail.pathInfo.requestPath.includes("cimar") && e.detail.xhr.status == 403) {
error = "Cimar permission error, please login and refresh the page.";
+14
View File
@@ -219,6 +219,20 @@
});
</script>
<script>
// Page-level safe HTMX error handler: avoid throwing if global container missing
document.body.addEventListener('htmx:responseError', function (e) {
const el = document.getElementById('htmx-error');
if (!el) {
console.error('htmx response error (no #htmx-error):', e.detail);
return;
}
let error = e.detail && e.detail.xhr ? e.detail.xhr.response : 'Unknown error';
el.innerHTML = error;
el.classList.remove('hidden');
});
</script>
{% endblock %}