You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bootstrap.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // DO NOT IMPORT window.config HERE!
  2. // to make sure the error handler always works, we should never import `window.config`, because some user's custom template breaks it.
  3. // This sets up the URL prefix used in webpack's chunk loading.
  4. // This file must be imported before any lazy-loading is being attempted.
  5. __webpack_public_path__ = `${window.config?.assetUrlPrefix ?? '/assets'}/`;
  6. export function showGlobalErrorMessage(msg) {
  7. const pageContent = document.querySelector('.page-content');
  8. if (!pageContent) return;
  9. // compact the message to a data attribute to avoid too many duplicated messages
  10. const msgCompact = msg.replace(/\W/g, '').trim();
  11. let msgDiv = pageContent.querySelector(`.js-global-error[data-global-error-msg-compact="${msgCompact}"]`);
  12. if (!msgDiv) {
  13. const el = document.createElement('div');
  14. el.innerHTML = `<div class="ui container negative message center aligned js-global-error" style="white-space: pre-line;"></div>`;
  15. msgDiv = el.childNodes[0];
  16. }
  17. // merge duplicated messages into "the message (count)" format
  18. const msgCount = Number(msgDiv.getAttribute(`data-global-error-msg-count`)) + 1;
  19. msgDiv.setAttribute(`data-global-error-msg-compact`, msgCompact);
  20. msgDiv.setAttribute(`data-global-error-msg-count`, msgCount.toString());
  21. msgDiv.textContent = msg + (msgCount > 1 ? ` (${msgCount})` : '');
  22. pageContent.prepend(msgDiv);
  23. }
  24. /**
  25. * @param {ErrorEvent} e
  26. */
  27. function processWindowErrorEvent(e) {
  28. const err = e.error ?? e.reason;
  29. const assetBaseUrl = String(new URL(__webpack_public_path__, window.location.origin));
  30. // error is likely from browser extension or inline script. Do not show these in production builds.
  31. if (!err.stack?.includes(assetBaseUrl) && window.config?.runModeIsProd) return;
  32. let message;
  33. if (e.type === 'unhandledrejection') {
  34. message = `JavaScript promise rejection: ${err.message}.`;
  35. } else {
  36. message = `JavaScript error: ${e.message} (${e.filename} @ ${e.lineno}:${e.colno}).`;
  37. }
  38. if (!e.error && e.lineno === 0 && e.colno === 0 && e.filename === '' && window.navigator.userAgent.includes('FxiOS/')) {
  39. // At the moment, Firefox (iOS) (10x) has an engine bug. See https://github.com/go-gitea/gitea/issues/20240
  40. // If a script inserts a newly created (and content changed) element into DOM, there will be a nonsense error event reporting: Script error: line 0, col 0.
  41. return; // ignore such nonsense error event
  42. }
  43. showGlobalErrorMessage(`${message} Open browser console to see more details.`);
  44. }
  45. function initGlobalErrorHandler() {
  46. if (window._globalHandlerErrors?._inited) {
  47. showGlobalErrorMessage(`The global error handler has been initialized, do not initialize it again`);
  48. return;
  49. }
  50. if (!window.config) {
  51. showGlobalErrorMessage(`Gitea JavaScript code couldn't run correctly, please check your custom templates`);
  52. }
  53. // we added an event handler for window error at the very beginning of <script> of page head
  54. // the handler calls `_globalHandlerErrors.push` (array method) to record all errors occur before this init
  55. // then in this init, we can collect all error events and show them
  56. for (const e of window._globalHandlerErrors || []) {
  57. processWindowErrorEvent(e);
  58. }
  59. // then, change _globalHandlerErrors to an object with push method, to process further error events directly
  60. window._globalHandlerErrors = {_inited: true, push: (e) => processWindowErrorEvent(e)};
  61. }
  62. initGlobalErrorHandler();