@@ -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"
@@ -214,7 +212,7 @@ func (ctx *Context) RedirectToFirst(location ...string) {
214
212
ctx .Redirect (setting .AppSubURL + "/" )
215
213
}
216
214
217
- var templateExecutingErr = regexp . MustCompile ( `^template: (.*):([1-9][0-9]*):([1-9][0-9]*): executing (?:"(.*)" at <(.*)>: )?` )
215
+ const tplStatus500 base. TplName = "status/500"
218
216
219
217
// HTML calls Context.HTML and renders the template to HTTP response
220
218
func (ctx * Context ) HTML (status int , name base.TplName ) {
@@ -227,34 +225,11 @@ func (ctx *Context) HTML(status int, name base.TplName) {
227
225
return strconv .FormatInt (time .Since (tmplStartTime ).Nanoseconds ()/ 1e6 , 10 ) + "ms"
228
226
}
229
227
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 {
231
229
ctx .PlainText (http .StatusInternalServerError , "Unable to find HTML templates, the template system is not initialized, or Gitea can't find your template files." )
232
230
return
233
231
}
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 ))
258
233
ctx .ServerError ("Render failed" , err )
259
234
}
260
235
}
@@ -322,24 +297,25 @@ func (ctx *Context) serverErrorInternal(logMsg string, logErr error) {
322
297
return
323
298
}
324
299
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 ) {
326
302
ctx .Data ["ErrorMsg" ] = logErr
327
303
}
328
304
}
329
305
330
306
ctx .Data ["Title" ] = "Internal Server Error"
331
- ctx .HTML (http .StatusInternalServerError , base . TplName ( "status/500" ) )
307
+ ctx .HTML (http .StatusInternalServerError , tplStatus500 )
332
308
}
333
309
334
310
// NotFoundOrServerError use error check function to determine if the error
335
311
// is about not found. It responds with 404 status code for not found error,
336
312
// 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 )
340
316
return
341
317
}
342
- ctx .serverErrorInternal (logMsg , err )
318
+ ctx .serverErrorInternal (logMsg , logErr )
343
319
}
344
320
345
321
// PlainTextBytes renders bytes as plain text
0 commit comments