@@ -16,10 +16,8 @@ import (
16
16
"net/http"
17
17
"net/url"
18
18
"path"
19
- "regexp"
20
19
"strconv"
21
20
"strings"
22
- texttemplate "text/template"
23
21
"time"
24
22
25
23
"code.gitea.io/gitea/models/db"
@@ -216,7 +214,7 @@ func (ctx *Context) RedirectToFirst(location ...string) {
216
214
ctx .Redirect (setting .AppSubURL + "/" )
217
215
}
218
216
219
- var templateExecutingErr = regexp . MustCompile ( `^template: (.*):([1-9][0-9]*):([1-9][0-9]*): executing (?:"(.*)" at <(.*)>: )?` )
217
+ const tplStatus500 base. TplName = "status/500"
220
218
221
219
// HTML calls Context.HTML and renders the template to HTTP response
222
220
func (ctx * Context ) HTML (status int , name base.TplName ) {
@@ -229,34 +227,11 @@ func (ctx *Context) HTML(status int, name base.TplName) {
229
227
return strconv .FormatInt (time .Since (tmplStartTime ).Nanoseconds ()/ 1e6 , 10 ) + "ms"
230
228
}
231
229
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 {
233
231
ctx .PlainText (http .StatusInternalServerError , "Unable to find HTML templates, the template system is not initialized, or Gitea can't find your template files." )
234
232
return
235
233
}
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 ))
260
235
ctx .ServerError ("Render failed" , err )
261
236
}
262
237
}
@@ -324,24 +299,25 @@ func (ctx *Context) serverErrorInternal(logMsg string, logErr error) {
324
299
return
325
300
}
326
301
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 ) {
328
304
ctx .Data ["ErrorMsg" ] = logErr
329
305
}
330
306
}
331
307
332
308
ctx .Data ["Title" ] = "Internal Server Error"
333
- ctx .HTML (http .StatusInternalServerError , base . TplName ( "status/500" ) )
309
+ ctx .HTML (http .StatusInternalServerError , tplStatus500 )
334
310
}
335
311
336
312
// NotFoundOrServerError use error check function to determine if the error
337
313
// is about not found. It responds with 404 status code for not found error,
338
314
// 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 )
342
318
return
343
319
}
344
- ctx .serverErrorInternal (logMsg , err )
320
+ ctx .serverErrorInternal (logMsg , logErr )
345
321
}
346
322
347
323
// PlainTextBytes renders bytes as plain text
0 commit comments