mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
Replace panic with a message when MustRender fails
This commit is contained in:
parent
9a0ef4822c
commit
89abc3349d
|
@ -664,7 +664,7 @@ func (ctx *Context) Render(name string, binding interface{}, options ...map[stri
|
|||
// Note: the options: "gzip" and "charset" are built'n support by Iris, so you can pass these on any template engine or serialize engine
|
||||
func (ctx *Context) MustRender(name string, binding interface{}, options ...map[string]interface{}) {
|
||||
if err := ctx.Render(name, binding, options...); err != nil {
|
||||
ctx.Panic()
|
||||
ctx.HTML(StatusServiceUnavailable, fmt.Sprintf("<h2>Template: %s\nIP: %s</h2><b>%s</b>", name, ctx.RemoteAddr(), err.Error()))
|
||||
if ctx.framework.Config.IsDevelopment {
|
||||
ctx.framework.Logger.Printf("MustRender panics for client with IP: %s On template: %s.Trace: %s\n", ctx.RemoteAddr(), name, err)
|
||||
}
|
||||
|
|
|
@ -771,3 +771,22 @@ func TestContextPreRender(t *testing.T) {
|
|||
expected := "<h1>HI kataras. Error: " + errMsg1 + errMsg2 + "</h1>"
|
||||
e.GET("/").Expect().Status(iris.StatusOK).Body().Contains(expected)
|
||||
}
|
||||
|
||||
func TestTemplatesDisabled(t *testing.T) {
|
||||
iris.ResetDefault()
|
||||
defer iris.Close()
|
||||
|
||||
iris.Default.Config.DisableTemplateEngines = true
|
||||
|
||||
file := "index.html"
|
||||
ip := "0.0.0.0"
|
||||
errTmpl := "<h2>Template: %s\nIP: %s</h2><b>%s</b>"
|
||||
expctedErrMsg := fmt.Sprintf(errTmpl, file, ip, "Error: Unable to execute a template. Trace: Templates are disabled '.Config.DisableTemplatesEngines = true' please turn that to false, as defaulted.\n")
|
||||
|
||||
iris.Get("/renderErr", func(ctx *iris.Context) {
|
||||
ctx.MustRender(file, nil)
|
||||
})
|
||||
|
||||
e := httptest.New(iris.Default, t)
|
||||
e.GET("/renderErr").Expect().Status(iris.StatusServiceUnavailable).Body().Equal(expctedErrMsg)
|
||||
}
|
||||
|
|
|
@ -72,6 +72,10 @@ func (t *templateEngines) usePreRender(pre PreRender) {
|
|||
// the gzip and charset options are built'n with iris
|
||||
// template is passed as file or souce
|
||||
func (t *templateEngines) render(isFile bool, ctx *Context, filenameOrSource string, binding interface{}, options []map[string]interface{}) error {
|
||||
if ctx.framework.Config.DisableTemplateEngines {
|
||||
return errTemplateExecute.Format("Templates are disabled '.Config.DisableTemplatesEngines = true' please turn that to false, as defaulted.")
|
||||
}
|
||||
|
||||
if len(t.prerenders) > 0 {
|
||||
for i := range t.prerenders {
|
||||
// I'm not making any checks here for performance reasons, means that
|
||||
|
@ -114,6 +118,7 @@ func (t *templateEngines) render(isFile bool, ctx *Context, filenameOrSource str
|
|||
} else {
|
||||
out = ctx.Response.BodyWriter()
|
||||
}
|
||||
|
||||
if isFile {
|
||||
return t.ExecuteWriter(out, filenameOrSource, binding, options...)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user