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.6KB

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