-
+ {{range .SubNames}}
+
- {{.}} + {{end}} +
Error pages
+-
+
- Not found +
- Quota exhaustion +
- Server error +
diff --git a/.deadcode-out b/.deadcode-out index e366abee94..31184826ae 100644 --- a/.deadcode-out +++ b/.deadcode-out @@ -210,9 +210,6 @@ forgejo.org/modules/zstd Writer.Write Writer.Close -forgejo.org/routers/web - NotFound - forgejo.org/routers/web/org MustEnableProjects diff --git a/modules/testlogger/testlogger.go b/modules/testlogger/testlogger.go index b5f196ad4b..5567ea433e 100644 --- a/modules/testlogger/testlogger.go +++ b/modules/testlogger/testlogger.go @@ -363,6 +363,9 @@ var ignoredErrorMessage = []string{ // TestDatabaseCollation `[E] [Error SQL Query] INSERT INTO test_collation_tbl (txt) VALUES ('main') []`, + + // TestDevtestErrorpages + `ErrorPage() [E] Example error: Example error`, } func (w *testLoggerWriterCloser) recordError(msg string) { diff --git a/options/locale_next/locale_en-US.json b/options/locale_next/locale_en-US.json index 4f5d102392..e525598ed5 100644 --- a/options/locale_next/locale_en-US.json +++ b/options/locale_next/locale_en-US.json @@ -16,5 +16,6 @@ "incorrect_root_url": "This Forgejo instance is configured to be served on \"%s\". You are currently viewing Forgejo through a different URL, which may cause parts of the application to break. The canonical URL is controlled by Forgejo admins via the ROOT_URL setting in the app.ini.", "themes.names.forgejo-auto": "Forgejo (follow system theme)", "themes.names.forgejo-light": "Forgejo light", - "themes.names.forgejo-dark": "Forgejo dark" -} + "themes.names.forgejo-dark": "Forgejo dark", + "error.not_found.title": "Page not found" +} \ No newline at end of file diff --git a/routers/web/devtest/devtest.go b/routers/web/devtest/devtest.go index 37496ca117..2ef030cb92 100644 --- a/routers/web/devtest/devtest.go +++ b/routers/web/devtest/devtest.go @@ -1,9 +1,11 @@ // Copyright 2023 The Gitea Authors. All rights reserved. +// Copyright 2025 The Forgejo Authors. All rights reserved. // SPDX-License-Identifier: MIT package devtest import ( + "errors" "net/http" "path" "strings" @@ -42,6 +44,17 @@ func FetchActionTest(ctx *context.Context) { ctx.JSONRedirect("") } +func ErrorPage(ctx *context.Context) { + if ctx.Params("errcode") == "404" { + ctx.NotFound("Example error", errors.New("Example error")) + return + } else if ctx.Params("errcode") == "413" { + ctx.HTML(http.StatusRequestEntityTooLarge, base.TplName("status/413")) + return + } + ctx.ServerError("Example error", errors.New("Example error")) +} + func Tmpl(ctx *context.Context) { now := time.Now() ctx.Data["TimeNow"] = now diff --git a/routers/web/home.go b/routers/web/home.go index e0a466a81c..bd9942748a 100644 --- a/routers/web/home.go +++ b/routers/web/home.go @@ -1,5 +1,6 @@ // Copyright 2014 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. +// Copyright 2025 The Forgejo Authors. All rights reserved. // SPDX-License-Identifier: MIT package web @@ -112,9 +113,3 @@ func HomeSitemap(ctx *context.Context) { log.Error("Failed writing sitemap: %v", err) } } - -// NotFound render 404 page -func NotFound(ctx *context.Context) { - ctx.Data["Title"] = "Page Not Found" - ctx.NotFound("home.NotFound", nil) -} diff --git a/routers/web/web.go b/routers/web/web.go index 303167a6b9..3643943b5b 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1,4 +1,5 @@ // Copyright 2017 The Gitea Authors. All rights reserved. +// Copyright 2023 The Forgejo Authors. All rights reserved. // SPDX-License-Identifier: MIT package web @@ -1661,6 +1662,7 @@ func registerRoutes(m *web.Route) { m.Any("/devtest", devtest.List) m.Any("/devtest/fetch-action-test", devtest.FetchActionTest) m.Any("/devtest/{sub}", devtest.Tmpl) + m.Get("/devtest/error/{errcode}", devtest.ErrorPage) } m.NotFound(func(w http.ResponseWriter, req *http.Request) { diff --git a/services/context/context_response.go b/services/context/context_response.go index e20e7dd852..e64f478420 100644 --- a/services/context/context_response.go +++ b/services/context/context_response.go @@ -1,4 +1,5 @@ // Copyright 2023 The Gitea Authors. All rights reserved. +// Copyright 2024 The Forgejo Authors. All rights reserved. // SPDX-License-Identifier: MIT package context @@ -66,7 +67,10 @@ func (ctx *Context) RedirectToFirst(location ...string) string { return setting.AppSubURL + "/" } -const tplStatus500 base.TplName = "status/500" +const ( + tplStatus404 base.TplName = "status/404" + tplStatus500 base.TplName = "status/500" +) // HTML calls Context.HTML and renders the template to HTTP response func (ctx *Context) HTML(status int, name base.TplName) { @@ -153,8 +157,8 @@ func (ctx *Context) notFoundInternal(logMsg string, logErr error) { } ctx.Data["IsRepo"] = ctx.Repo.Repository != nil - ctx.Data["Title"] = "Page Not Found" - ctx.HTML(http.StatusNotFound, base.TplName("status/404")) + ctx.Data["Title"] = ctx.Locale.TrString("error.not_found.title") + ctx.HTML(http.StatusNotFound, tplStatus404) } // ServerError displays a 500 (Internal Server Error) page and prints the given error, if any. @@ -177,7 +181,6 @@ func (ctx *Context) serverErrorInternal(logMsg string, logErr error) { } } - ctx.Data["Title"] = "Internal Server Error" ctx.HTML(http.StatusInternalServerError, tplStatus500) } diff --git a/templates/devtest/list.tmpl b/templates/devtest/list.tmpl index 90b1fcc9d0..e169311222 100644 --- a/templates/devtest/list.tmpl +++ b/templates/devtest/list.tmpl @@ -1,10 +1,21 @@ {{template "base/head" .}} -