diff --git a/routers/common/errpage.go b/routers/common/errpage.go index 6152a0c..4914436 100644 --- a/routers/common/errpage.go +++ b/routers/common/errpage.go @@ -24,6 +24,11 @@ import ( const tplStatus500 templates.TplName = "status/500" func renderServerErrorPage(w http.ResponseWriter, req *http.Request, respCode int, tmpl templates.TplName, ctxData map[string]any, plainMsg string) { + if ctxData == nil { + ctxData = map[string]any{} + } + ctxData["ShowNavbarBrand"] = true + acceptsHTML := false for _, part := range req.Header["Accept"] { if strings.Contains(part, "text/html") { diff --git a/routers/common/errpage_test.go b/routers/common/errpage_test.go index 319ae7c..986362c 100644 --- a/routers/common/errpage_test.go +++ b/routers/common/errpage_test.go @@ -27,12 +27,21 @@ func TestRenderPanicErrorPage(t *testing.T) { assert.Contains(t, respContent, `class="page-content status-page-500"`) assert.Contains(t, respContent, ``) assert.Contains(t, respContent, `lang="en-US"`) // make sure the locale work + assertNavbarBrand(t, respContent) // 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, // the different "footer" is the only way to know whether a page is fully rendered without error. assert.False(t, test.IsNormalPageCompleted(respContent)) }) + t.Run("ServiceUnavailableHTML", func(t *testing.T) { + w := httptest.NewRecorder() + req := &http.Request{URL: &url.URL{}, Header: http.Header{"Accept": []string{"text/html"}}} + req = req.WithContext(reqctx.NewRequestContextForTest(t.Context())) + renderServiceUnavailable(w, req) + assert.Equal(t, http.StatusServiceUnavailable, w.Code) + assertNavbarBrand(t, w.Body.String()) + }) t.Run("Plain", func(t *testing.T) { w := httptest.NewRecorder() req := &http.Request{URL: &url.URL{}} @@ -42,6 +51,13 @@ func TestRenderPanicErrorPage(t *testing.T) { }) } +func assertNavbarBrand(t *testing.T, content string) { + t.Helper() + assert.Contains(t, content, `class="navbar-brand-copy"`) + assert.Contains(t, content, "荣刻科技") + assert.Contains(t, content, "RONCARVE TECHNOLOGY") +} + func TestMain(m *testing.M) { unittest.MainTest(m) } diff --git a/services/context/context_response.go b/services/context/context_response.go index d869c77..c3fc48c 100644 --- a/services/context/context_response.go +++ b/services/context/context_response.go @@ -166,6 +166,7 @@ func (ctx *Context) notFoundInternal(logMsg string, logErr error) { ctx.Data["IsRepo"] = ctx.Repo.Repository != nil ctx.Data["Title"] = "Page Not Found" ctx.Data["ErrorMsg"] = "" // FIXME: the template never renders this message, need to fix in the future (and show safe messages to end users) + ctx.Data["ShowNavbarBrand"] = true ctx.HTML(http.StatusNotFound, "status/404") } diff --git a/templates/base/head_navbar.tmpl b/templates/base/head_navbar.tmpl index 9d4c44b..47a270a 100644 --- a/templates/base/head_navbar.tmpl +++ b/templates/base/head_navbar.tmpl @@ -1,13 +1,10 @@