-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show messages for users if the ROOT_URL is wrong, show JavaScript err…
…ors (#18971) * ROOT_URL issues: some users did wrong to there app.ini config, then: * The assets can not be loaded (AppSubUrl != "" and users try to access http://host:3000/) *The ROOT_URL is wrong, then many URLs in Gitea are broken. Now Gitea show enough information to users. * JavaScript error issues, there are many users affected by JavaScript errors, some are caused by frontend bugs, some are caused by broken customized templates. If these JS errors can be found at first time, then maintainers do not need to ask about how bug occurs again and again. * Some people like to modify the `head.tmpl`, so we separate the script part to `head_script.tmpl`, then it's much safer. * use specialized CSS class "js-global-error", end users still have a chance to hide error messages by customized CSS styles.
- Loading branch information
1 parent
ea8622d
commit 2bce1ea
Showing
7 changed files
with
118 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{{/* | ||
==== DO NOT EDIT ==== | ||
If you are customizing Gitea, please do not change this file. | ||
If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly. | ||
*/}} | ||
<script> | ||
<!-- /* eslint-disable */ --> | ||
window.addEventListener('error', function(e) {window._globalHandlerErrors=window._globalHandlerErrors||[]; window._globalHandlerErrors.push(e);}); | ||
window.config = { | ||
appVer: '{{AppVer}}', | ||
appUrl: '{{AppUrl}}', | ||
appSubUrl: '{{AppSubUrl}}', | ||
assetUrlPrefix: '{{AssetUrlPrefix}}', | ||
runModeIsProd: {{.RunModeIsProd}}, | ||
customEmojis: {{CustomEmojis}}, | ||
useServiceWorker: {{UseServiceWorker}}, | ||
csrfToken: '{{.CsrfToken}}', | ||
pageData: {{.PageData}}, | ||
requireTribute: {{.RequireTribute}}, | ||
notificationSettings: {{NotificationSettings}}, {{/*a map provided by NewFuncMap in helper.go*/}} | ||
enableTimeTracking: {{EnableTimetracking}}, | ||
{{if .RequireTribute}} | ||
tributeValues: Array.from(new Map([ | ||
{{ range .Participants }} | ||
['{{.Name}}', {key: '{{.Name}} {{.FullName}}', value: '{{.Name}}', | ||
name: '{{.Name}}', fullname: '{{.FullName}}', avatar: '{{.AvatarLink}}'}], | ||
{{ end }} | ||
{{ range .Assignees }} | ||
['{{.Name}}', {key: '{{.Name}} {{.FullName}}', value: '{{.Name}}', | ||
name: '{{.Name}}', fullname: '{{.FullName}}', avatar: '{{.AvatarLink}}'}], | ||
{{ end }} | ||
{{ range .MentionableTeams }} | ||
['{{$.MentionableTeamsOrg}}/{{.Name}}', {key: '{{$.MentionableTeamsOrg}}/{{.Name}}', value: '{{$.MentionableTeamsOrg}}/{{.Name}}', | ||
name: '{{$.MentionableTeamsOrg}}/{{.Name}}', avatar: '{{$.MentionableTeamsOrgAvatar}}'}], | ||
{{ end }} | ||
]).values()), | ||
{{end}} | ||
mermaidMaxSourceCharacters: {{MermaidMaxSourceCharacters}}, | ||
{{/* this global i18n object should only contain general texts. for specialized texts, it should be provided inside the related modules by: (1) API response (2) HTML data-attribute (3) PageData */}} | ||
i18n: { | ||
copy_success: '{{.i18n.Tr "copy_success"}}', | ||
copy_error: '{{.i18n.Tr "copy_error"}}', | ||
error_occurred: '{{.i18n.Tr "error.occurred"}}', | ||
network_error: '{{.i18n.Tr "error.network_error"}}', | ||
}, | ||
}; | ||
{{/* in case some pages don't render the pageData, we make sure it is an object to prevent null access */}} | ||
window.config.pageData = window.config.pageData || {}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {joinPaths} from './utils.js'; | ||
|
||
// DO NOT IMPORT window.config HERE! | ||
// to make sure the error handler always works, we should never import `window.config`, because some user's custom template breaks it. | ||
|
||
// This sets up the URL prefix used in webpack's chunk loading. | ||
// This file must be imported before any lazy-loading is being attempted. | ||
__webpack_public_path__ = joinPaths(window?.config?.assetUrlPrefix ?? '/', '/'); | ||
|
||
export function showGlobalErrorMessage(msg) { | ||
const pageContent = document.querySelector('.page-content'); | ||
if (!pageContent) return; | ||
const el = document.createElement('div'); | ||
el.innerHTML = `<div class="ui container negative message center aligned js-global-error" style="white-space: pre-line;"></div>`; | ||
el.childNodes[0].textContent = msg; | ||
pageContent.prepend(el.childNodes[0]); | ||
} | ||
|
||
/** | ||
* @param {ErrorEvent} e | ||
*/ | ||
function processWindowErrorEvent(e) { | ||
showGlobalErrorMessage(`JavaScript error: ${e.message} (${e.filename} @ ${e.lineno}:${e.colno}). Open browser console to see more details.`); | ||
} | ||
|
||
function initGlobalErrorHandler() { | ||
if (!window.config) { | ||
showGlobalErrorMessage(`Gitea JavaScript code couldn't run correctly, please check your custom templates`); | ||
} | ||
|
||
// we added an event handler for window error at the very beginning of <script> of page head | ||
// the handler calls `_globalHandlerErrors.push` (array method) to record all errors occur before this init | ||
// then in this init, we can collect all error events and show them | ||
for (const e of window._globalHandlerErrors || []) { | ||
processWindowErrorEvent(e); | ||
} | ||
// then, change _globalHandlerErrors to an object with push method, to process further error events directly | ||
window._globalHandlerErrors = {'push': (e) => processWindowErrorEvent(e)}; | ||
} | ||
|
||
initGlobalErrorHandler(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.