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 4.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // DO NOT IMPORT window.config HERE!
  2. // to make sure the error handler always works, we should never import `window.config`, because
  3. // some user's custom template breaks it.
  4. // This sets up the URL prefix used in webpack's chunk loading.
  5. // This file must be imported before any lazy-loading is being attempted.
  6. __webpack_public_path__ = `${window.config?.assetUrlPrefix ?? '/assets'}/`;
  7. const filteredErrors = new Set([
  8. 'getModifierState is not a function', // https://github.com/microsoft/monaco-editor/issues/4325
  9. ]);
  10. export function showGlobalErrorMessage(msg) {
  11. const pageContent = document.querySelector('.page-content');
  12. if (!pageContent) return;
  13. for (const filteredError of filteredErrors) {
  14. if (msg.includes(filteredError)) return;
  15. }
  16. // compact the message to a data attribute to avoid too many duplicated messages
  17. const msgCompact = msg.replace(/\W/g, '').trim();
  18. let msgDiv = pageContent.querySelector(`.js-global-error[data-global-error-msg-compact="${msgCompact}"]`);
  19. if (!msgDiv) {
  20. const el = document.createElement('div');
  21. el.innerHTML = `<div class="ui container negative message center aligned js-global-error tw-mt-[15px] tw-whitespace-pre-line"></div>`;
  22. msgDiv = el.childNodes[0];
  23. }
  24. // merge duplicated messages into "the message (count)" format
  25. const msgCount = Number(msgDiv.getAttribute(`data-global-error-msg-count`)) + 1;
  26. msgDiv.setAttribute(`data-global-error-msg-compact`, msgCompact);
  27. msgDiv.setAttribute(`data-global-error-msg-count`, msgCount.toString());
  28. msgDiv.textContent = msg + (msgCount > 1 ? ` (${msgCount})` : '');
  29. pageContent.prepend(msgDiv);
  30. }
  31. /**
  32. * @param {ErrorEvent|PromiseRejectionEvent} event - Event
  33. * @param {string} event.message - Only present on ErrorEvent
  34. * @param {string} event.error - Only present on ErrorEvent
  35. * @param {string} event.type - Only present on ErrorEvent
  36. * @param {string} event.filename - Only present on ErrorEvent
  37. * @param {number} event.lineno - Only present on ErrorEvent
  38. * @param {number} event.colno - Only present on ErrorEvent
  39. * @param {string} event.reason - Only present on PromiseRejectionEvent
  40. * @param {number} event.promise - Only present on PromiseRejectionEvent
  41. */
  42. function processWindowErrorEvent({error, reason, message, type, filename, lineno, colno}) {
  43. const err = error ?? reason;
  44. const assetBaseUrl = String(new URL(__webpack_public_path__, window.location.origin));
  45. const {runModeIsProd} = window.config ?? {};
  46. // `error` and `reason` are not guaranteed to be errors. If the value is falsy, it is likly a
  47. // non-critical event from the browser. We log them but don't show them to users. Examples:
  48. // - https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver#observation_errors
  49. // - https://github.com/mozilla-mobile/firefox-ios/issues/10817
  50. // - https://github.com/go-gitea/gitea/issues/20240
  51. if (!err) {
  52. if (message) console.error(new Error(message));
  53. if (runModeIsProd) return;
  54. }
  55. // If the error stack trace does not include the base URL of our script assets, it likely came
  56. // from a browser extension or inline script. Do not show such errors in production.
  57. if (err instanceof Error && !err.stack?.includes(assetBaseUrl) && runModeIsProd) {
  58. return;
  59. }
  60. let msg = err?.message ?? message;
  61. if (lineno) msg += ` (${filename} @ ${lineno}:${colno})`;
  62. const dot = msg.endsWith('.') ? '' : '.';
  63. const renderedType = type === 'unhandledrejection' ? 'promise rejection' : type;
  64. showGlobalErrorMessage(`JavaScript ${renderedType}: ${msg}${dot} Open browser console to see more details.`);
  65. }
  66. function initGlobalErrorHandler() {
  67. if (window._globalHandlerErrors?._inited) {
  68. showGlobalErrorMessage(`The global error handler has been initialized, do not initialize it again`);
  69. return;
  70. }
  71. if (!window.config) {
  72. showGlobalErrorMessage(`Gitea JavaScript code couldn't run correctly, please check your custom templates`);
  73. }
  74. // we added an event handler for window error at the very beginning of <script> of page head the
  75. // handler calls `_globalHandlerErrors.push` (array method) to record all errors occur before
  76. // this init then in this init, we can collect all error events and show them.
  77. for (const e of window._globalHandlerErrors || []) {
  78. processWindowErrorEvent(e);
  79. }
  80. // then, change _globalHandlerErrors to an object with push method, to process further error
  81. // events directly
  82. window._globalHandlerErrors = {_inited: true, push: (e) => processWindowErrorEvent(e)};
  83. }
  84. initGlobalErrorHandler();