From 89abc3349da680ca66d9c9aa8f5a310b61d1e3c5 Mon Sep 17 00:00:00 2001 From: Gerasimos Maropoulos Date: Sat, 15 Oct 2016 20:48:06 +0300 Subject: [PATCH] Replace panic with a message when MustRender fails --- context.go | 2 +- context_test.go | 19 +++++++++++++++++++ template.go | 5 +++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index bdc0b99f..6f5be23e 100644 --- a/context.go +++ b/context.go @@ -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("

Template: %s\nIP: %s

%s", 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) } diff --git a/context_test.go b/context_test.go index c40a0907..143df767 100644 --- a/context_test.go +++ b/context_test.go @@ -771,3 +771,22 @@ func TestContextPreRender(t *testing.T) { expected := "

HI kataras. Error: " + errMsg1 + errMsg2 + "

" 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 := "

Template: %s\nIP: %s

%s" + 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) +} diff --git a/template.go b/template.go index 8776fc26..00ccf283 100644 --- a/template.go +++ b/template.go @@ -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...) }