Skip to content

Commit 9ecde7f

Browse files
committed
Merge remote-tracking branch 'upstream/main'
* upstream/main: Show friendly 500 error page to users and developers (go-gitea#24110)
2 parents 60fcb2f + 1c8bc40 commit 9ecde7f

File tree

10 files changed

+305
-172
lines changed

10 files changed

+305
-172
lines changed

modules/context/context.go

+10-34
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ import (
1616
"net/http"
1717
"net/url"
1818
"path"
19-
"regexp"
2019
"strconv"
2120
"strings"
22-
texttemplate "text/template"
2321
"time"
2422

2523
"code.gitea.io/gitea/models/db"
@@ -216,7 +214,7 @@ func (ctx *Context) RedirectToFirst(location ...string) {
216214
ctx.Redirect(setting.AppSubURL + "/")
217215
}
218216

219-
var templateExecutingErr = regexp.MustCompile(`^template: (.*):([1-9][0-9]*):([1-9][0-9]*): executing (?:"(.*)" at <(.*)>: )?`)
217+
const tplStatus500 base.TplName = "status/500"
220218

221219
// HTML calls Context.HTML and renders the template to HTTP response
222220
func (ctx *Context) HTML(status int, name base.TplName) {
@@ -229,34 +227,11 @@ func (ctx *Context) HTML(status int, name base.TplName) {
229227
return strconv.FormatInt(time.Since(tmplStartTime).Nanoseconds()/1e6, 10) + "ms"
230228
}
231229
if err := ctx.Render.HTML(ctx.Resp, status, string(name), templates.BaseVars().Merge(ctx.Data)); err != nil {
232-
if status == http.StatusInternalServerError && name == base.TplName("status/500") {
230+
if status == http.StatusInternalServerError && name == tplStatus500 {
233231
ctx.PlainText(http.StatusInternalServerError, "Unable to find HTML templates, the template system is not initialized, or Gitea can't find your template files.")
234232
return
235233
}
236-
if execErr, ok := err.(texttemplate.ExecError); ok {
237-
if groups := templateExecutingErr.FindStringSubmatch(err.Error()); len(groups) > 0 {
238-
errorTemplateName, lineStr, posStr := groups[1], groups[2], groups[3]
239-
target := ""
240-
if len(groups) == 6 {
241-
target = groups[5]
242-
}
243-
line, _ := strconv.Atoi(lineStr) // Cannot error out as groups[2] is [1-9][0-9]*
244-
pos, _ := strconv.Atoi(posStr) // Cannot error out as groups[3] is [1-9][0-9]*
245-
assetLayerName := templates.AssetFS().GetFileLayerName(errorTemplateName + ".tmpl")
246-
filename := fmt.Sprintf("(%s) %s", assetLayerName, errorTemplateName)
247-
if errorTemplateName != string(name) {
248-
filename += " (subtemplate of " + string(name) + ")"
249-
}
250-
err = fmt.Errorf("failed to render %s, error: %w:\n%s", filename, err, templates.GetLineFromTemplate(errorTemplateName, line, target, pos))
251-
} else {
252-
assetLayerName := templates.AssetFS().GetFileLayerName(execErr.Name + ".tmpl")
253-
filename := fmt.Sprintf("(%s) %s", assetLayerName, execErr.Name)
254-
if execErr.Name != string(name) {
255-
filename += " (subtemplate of " + string(name) + ")"
256-
}
257-
err = fmt.Errorf("failed to render %s, error: %w", filename, err)
258-
}
259-
}
234+
err = fmt.Errorf("failed to render template: %s, error: %s", name, templates.HandleTemplateRenderingError(err))
260235
ctx.ServerError("Render failed", err)
261236
}
262237
}
@@ -324,24 +299,25 @@ func (ctx *Context) serverErrorInternal(logMsg string, logErr error) {
324299
return
325300
}
326301

327-
if !setting.IsProd {
302+
// it's safe to show internal error to admin users, and it helps
303+
if !setting.IsProd || (ctx.Doer != nil && ctx.Doer.IsAdmin) {
328304
ctx.Data["ErrorMsg"] = logErr
329305
}
330306
}
331307

332308
ctx.Data["Title"] = "Internal Server Error"
333-
ctx.HTML(http.StatusInternalServerError, base.TplName("status/500"))
309+
ctx.HTML(http.StatusInternalServerError, tplStatus500)
334310
}
335311

336312
// NotFoundOrServerError use error check function to determine if the error
337313
// is about not found. It responds with 404 status code for not found error,
338314
// or error context description for logging purpose of 500 server error.
339-
func (ctx *Context) NotFoundOrServerError(logMsg string, errCheck func(error) bool, err error) {
340-
if errCheck(err) {
341-
ctx.notFoundInternal(logMsg, err)
315+
func (ctx *Context) NotFoundOrServerError(logMsg string, errCheck func(error) bool, logErr error) {
316+
if errCheck(logErr) {
317+
ctx.notFoundInternal(logMsg, logErr)
342318
return
343319
}
344-
ctx.serverErrorInternal(logMsg, err)
320+
ctx.serverErrorInternal(logMsg, logErr)
345321
}
346322

347323
// PlainTextBytes renders bytes as plain text

0 commit comments

Comments
 (0)