1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/static/js/error_focus.js
catborise 6abea495f1 fix(logs): failures name their job, VM and user; the new VM form shows a list it could not load
- A failed cancel hook logged "worker task cancel failed": it now names
  the job, its kind, resource and user, with the traceback for a fault
  of the panel.
- novncd logged "Console connection failed" without the VM or the user.
- django-axes wrote four INFO lines a login ("Reset 0 access
  attempts"...); logins are on the Logs page, so it logs warnings only.
- The new VM form's machine types and disk buses answered 200 with an
  empty object when libvirt failed, and nothing was logged: the list
  stayed empty, the form could not go on and said nothing. They answer
  400 with the error, as the pool's volume list does, the warning is
  logged with the compute, and the form's lists show an error they get
  with the page's messages.
2026-11-06 23:48:44 +03:00

33 lines
1.4 KiB
JavaScript

// After a refused submission, start at the error. The places an error
// can be, most specific first: a refused field, its form's summary, an
// open dialog's copy of the messages (the page's are behind it), the
// page's messages. A page that sets itself up first has
// data-error-focus="manual" and calls this when it is ready.
const ERROR_PLACES = [".is-invalid", ".form-errors", ".modal.show .modal-messages .alert-danger",
"#messages .alert-danger"];
function focusFirstError() {
for (const selector of ERROR_PLACES) {
const target = [...document.querySelectorAll(selector)].find(e => e.offsetParent !== null);
if (target) {
if (!target.matches("input, select, textarea")) target.tabIndex = -1;
target.focus();
return;
}
}
}
window.focusFirstError = focusFirstError;
if (document.body.dataset.errorFocus !== "manual") focusFirstError();
// A list a page loads (jQuery) that the server could not give: its error goes
// with the page's messages. A load a newer one aborted has none.
function showLoadError(xhr) {
const text = (xhr.responseJSON || {}).error;
if (!text) return;
const alert = document.createElement("div");
alert.className = "alert alert-danger";
alert.setAttribute("role", "alert");
alert.textContent = text;
document.querySelector("#messages").append(alert);
}
window.showLoadError = showLoadError;