@@ -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"
@@ -214,7 +212,7 @@ func (ctx *Context) RedirectToFirst(location ...string) {
214212 ctx .Redirect (setting .AppSubURL + "/" )
215213}
216214
217- var templateExecutingErr = regexp . MustCompile ( `^template: (.*):([1-9][0-9]*):([1-9][0-9]*): executing (?:"(.*)" at <(.*)>: )?` )
215+ const tplStatus500 base. TplName = "status/500"
218216
219217// HTML calls Context.HTML and renders the template to HTTP response
220218func (ctx * Context ) HTML (status int , name base.TplName ) {
@@ -227,34 +225,11 @@ func (ctx *Context) HTML(status int, name base.TplName) {
227225 return strconv .FormatInt (time .Since (tmplStartTime ).Nanoseconds ()/ 1e6 , 10 ) + "ms"
228226 }
229227 if err := ctx .Render .HTML (ctx .Resp , status , string (name ), templates .BaseVars ().Merge (ctx .Data )); err != nil {
230- if status == http .StatusInternalServerError && name == base . TplName ( "status/500" ) {
228+ if status == http .StatusInternalServerError && name == tplStatus500 {
231229 ctx .PlainText (http .StatusInternalServerError , "Unable to find HTML templates, the template system is not initialized, or Gitea can't find your template files." )
232230 return
233231 }
234- if execErr , ok := err .(texttemplate.ExecError ); ok {
235- if groups := templateExecutingErr .FindStringSubmatch (err .Error ()); len (groups ) > 0 {
236- errorTemplateName , lineStr , posStr := groups [1 ], groups [2 ], groups [3 ]
237- target := ""
238- if len (groups ) == 6 {
239- target = groups [5 ]
240- }
241- line , _ := strconv .Atoi (lineStr ) // Cannot error out as groups[2] is [1-9][0-9]*
242- pos , _ := strconv .Atoi (posStr ) // Cannot error out as groups[3] is [1-9][0-9]*
243- assetLayerName := templates .AssetFS ().GetFileLayerName (errorTemplateName + ".tmpl" )
244- filename := fmt .Sprintf ("(%s) %s" , assetLayerName , errorTemplateName )
245- if errorTemplateName != string (name ) {
246- filename += " (subtemplate of " + string (name ) + ")"
247- }
248- err = fmt .Errorf ("failed to render %s, error: %w:\n %s" , filename , err , templates .GetLineFromTemplate (errorTemplateName , line , target , pos ))
249- } else {
250- assetLayerName := templates .AssetFS ().GetFileLayerName (execErr .Name + ".tmpl" )
251- filename := fmt .Sprintf ("(%s) %s" , assetLayerName , execErr .Name )
252- if execErr .Name != string (name ) {
253- filename += " (subtemplate of " + string (name ) + ")"
254- }
255- err = fmt .Errorf ("failed to render %s, error: %w" , filename , err )
256- }
257- }
232+ err = fmt .Errorf ("failed to render template: %s, error: %s" , name , templates .HandleTemplateRenderingError (err ))
258233 ctx .ServerError ("Render failed" , err )
259234 }
260235}
@@ -322,24 +297,25 @@ func (ctx *Context) serverErrorInternal(logMsg string, logErr error) {
322297 return
323298 }
324299
325- if ! setting .IsProd {
300+ // it's safe to show internal error to admin users, and it helps
301+ if ! setting .IsProd || (ctx .Doer != nil && ctx .Doer .IsAdmin ) {
326302 ctx .Data ["ErrorMsg" ] = logErr
327303 }
328304 }
329305
330306 ctx .Data ["Title" ] = "Internal Server Error"
331- ctx .HTML (http .StatusInternalServerError , base . TplName ( "status/500" ) )
307+ ctx .HTML (http .StatusInternalServerError , tplStatus500 )
332308}
333309
334310// NotFoundOrServerError use error check function to determine if the error
335311// is about not found. It responds with 404 status code for not found error,
336312// or error context description for logging purpose of 500 server error.
337- func (ctx * Context ) NotFoundOrServerError (logMsg string , errCheck func (error ) bool , err error ) {
338- if errCheck (err ) {
339- ctx .notFoundInternal (logMsg , err )
313+ func (ctx * Context ) NotFoundOrServerError (logMsg string , errCheck func (error ) bool , logErr error ) {
314+ if errCheck (logErr ) {
315+ ctx .notFoundInternal (logMsg , logErr )
340316 return
341317 }
342- ctx .serverErrorInternal (logMsg , err )
318+ ctx .serverErrorInternal (logMsg , logErr )
343319}
344320
345321// PlainTextBytes renders bytes as plain text
0 commit comments