diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-10-31 22:11:48 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-31 22:11:48 +0800 |
commit | a4b242ae7a3edc6302a2730a01cda5a2e76bd6b5 (patch) | |
tree | 525ac99acfa676038e6d0af57bec90c4e0517560 /routers/common | |
parent | 16d15ce087cd4b9ddf91f6acff7eacac24e6aac5 (diff) | |
download | gitea-a4b242ae7a3edc6302a2730a01cda5a2e76bd6b5.tar.gz gitea-a4b242ae7a3edc6302a2730a01cda5a2e76bd6b5.zip |
Clean up template locale usage (#27856)
After many refactoring PRs for the "locale" and "template context
function", now the ".locale" is not needed for web templates any more.
This PR does a clean up for:
1. Remove `ctx.Data["locale"]` for web context.
2. Use `ctx.Locale` in `500.tmpl`, for consistency.
3. Add a test check for `500 page` locale usage.
4. Remove the `Str2html` and `DotEscape` from mail template context
data, they are copy&paste errors introduced by #19169 and #16200 . These
functions are template functions (provided by the common renderer), but
not template data variables.
5. Make email `SendAsync` function mockable (I was planning to add more
tests but it would make this PR much too complex, so the tests could be
done in another PR)
Diffstat (limited to 'routers/common')
-rw-r--r-- | routers/common/errpage.go | 15 | ||||
-rw-r--r-- | routers/common/errpage_test.go | 1 |
2 files changed, 8 insertions, 8 deletions
diff --git a/routers/common/errpage.go b/routers/common/errpage.go index 9c8ccc3388..923421a29c 100644 --- a/routers/common/errpage.go +++ b/routers/common/errpage.go @@ -9,6 +9,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -35,20 +36,18 @@ func RenderPanicErrorPage(w http.ResponseWriter, req *http.Request, err any) { httpcache.SetCacheControlInHeader(w.Header(), 0, "no-transform") w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions) - data := middleware.GetContextData(req.Context()) - if data["locale"] == nil { - data = middleware.CommonTemplateContextData() - data["locale"] = middleware.Locale(w, req) - } + tmplCtx := context.TemplateContext{} + tmplCtx["Locale"] = middleware.Locale(w, req) + ctxData := middleware.GetContextData(req.Context()) // This recovery handler could be called without Gitea's web context, so we shouldn't touch that context too much. // Otherwise, the 500-page may cause new panics, eg: cache.GetContextWithData, it makes the developer&users couldn't find the original panic. - user, _ := data[middleware.ContextDataKeySignedUser].(*user_model.User) + user, _ := ctxData[middleware.ContextDataKeySignedUser].(*user_model.User) if !setting.IsProd || (user != nil && user.IsAdmin) { - data["ErrorMsg"] = "PANIC: " + combinedErr + ctxData["ErrorMsg"] = "PANIC: " + combinedErr } - err = templates.HTMLRenderer().HTML(w, http.StatusInternalServerError, string(tplStatus500), data, nil) + err = templates.HTMLRenderer().HTML(w, http.StatusInternalServerError, string(tplStatus500), ctxData, tmplCtx) if err != nil { log.Error("Error occurs again when rendering error page: %v", err) w.WriteHeader(http.StatusInternalServerError) diff --git a/routers/common/errpage_test.go b/routers/common/errpage_test.go index 58a633b2a4..4fd63ba49e 100644 --- a/routers/common/errpage_test.go +++ b/routers/common/errpage_test.go @@ -26,6 +26,7 @@ func TestRenderPanicErrorPage(t *testing.T) { respContent := w.Body.String() assert.Contains(t, respContent, `class="page-content status-page-500"`) assert.Contains(t, respContent, `</html>`) + assert.Contains(t, respContent, `lang="en-US"`) // make sure the locale work // the 500 page doesn't have normal pages footer, it makes it easier to distinguish a normal page and a failed page. // especially when a sub-template causes page error, the HTTP response code is still 200, |