mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 12:51:29 +00:00
Merge pull request #377 from vadimi/master
Panic template execution errors to show error pages accordingly
This commit is contained in:
commit
55f638ac78
@ -138,6 +138,7 @@ func (c *Controller) RenderBytes() ([]byte, error) {
|
|||||||
err := BeeTemplates[c.TplNames].ExecuteTemplate(newbytes, c.TplNames, c.Data)
|
err := BeeTemplates[c.TplNames].ExecuteTemplate(newbytes, c.TplNames, c.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Trace("template Execute err:", err)
|
Trace("template Execute err:", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
tplcontent, _ := ioutil.ReadAll(newbytes)
|
tplcontent, _ := ioutil.ReadAll(newbytes)
|
||||||
c.Data["LayoutContent"] = template.HTML(string(tplcontent))
|
c.Data["LayoutContent"] = template.HTML(string(tplcontent))
|
||||||
@ -145,6 +146,7 @@ func (c *Controller) RenderBytes() ([]byte, error) {
|
|||||||
err = BeeTemplates[c.Layout].ExecuteTemplate(ibytes, c.Layout, c.Data)
|
err = BeeTemplates[c.Layout].ExecuteTemplate(ibytes, c.Layout, c.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Trace("template Execute err:", err)
|
Trace("template Execute err:", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
icontent, _ := ioutil.ReadAll(ibytes)
|
icontent, _ := ioutil.ReadAll(ibytes)
|
||||||
return icontent, nil
|
return icontent, nil
|
||||||
@ -163,6 +165,7 @@ func (c *Controller) RenderBytes() ([]byte, error) {
|
|||||||
err := BeeTemplates[c.TplNames].ExecuteTemplate(ibytes, c.TplNames, c.Data)
|
err := BeeTemplates[c.TplNames].ExecuteTemplate(ibytes, c.TplNames, c.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Trace("template Execute err:", err)
|
Trace("template Execute err:", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
icontent, _ := ioutil.ReadAll(ibytes)
|
icontent, _ := ioutil.ReadAll(ibytes)
|
||||||
return icontent, nil
|
return icontent, nil
|
||||||
|
27
router.go
27
router.go
@ -26,7 +26,10 @@ const (
|
|||||||
FinishRouter
|
FinishRouter
|
||||||
)
|
)
|
||||||
|
|
||||||
var HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"}
|
var (
|
||||||
|
HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"}
|
||||||
|
errorType = reflect.TypeOf((*error)(nil)).Elem()
|
||||||
|
)
|
||||||
|
|
||||||
type controllerInfo struct {
|
type controllerInfo struct {
|
||||||
pattern string
|
pattern string
|
||||||
@ -441,7 +444,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if context.Input.IsWebsocket() {
|
if context.Input.IsWebsocket() {
|
||||||
@ -729,7 +732,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
if !w.started && !context.Input.IsWebsocket() {
|
if !w.started && !context.Input.IsWebsocket() {
|
||||||
if AutoRender {
|
if AutoRender {
|
||||||
method = vc.MethodByName("Render")
|
method = vc.MethodByName("Render")
|
||||||
method.Call(in)
|
callMethodWithError(method, in)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -885,9 +888,9 @@ func (p *ControllerRegistor) getErrorHandler(errorCode string) func(rw http.Resp
|
|||||||
//responseWriter is a wrapper for the http.ResponseWriter
|
//responseWriter is a wrapper for the http.ResponseWriter
|
||||||
//started set to true if response was written to then don't execute other handler
|
//started set to true if response was written to then don't execute other handler
|
||||||
type responseWriter struct {
|
type responseWriter struct {
|
||||||
writer http.ResponseWriter
|
writer http.ResponseWriter
|
||||||
started bool
|
started bool
|
||||||
status int
|
status int
|
||||||
contentEncoding string
|
contentEncoding string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -920,3 +923,15 @@ func (w *responseWriter) WriteHeader(code int) {
|
|||||||
w.started = true
|
w.started = true
|
||||||
w.writer.WriteHeader(code)
|
w.writer.WriteHeader(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// call method and panic with error if error is in result params
|
||||||
|
func callMethodWithError(method reflect.Value, params []reflect.Value) {
|
||||||
|
results := method.Call(params)
|
||||||
|
if len(results) > 0 {
|
||||||
|
for _, result := range results {
|
||||||
|
if result.Type() == errorType && !result.IsNil() {
|
||||||
|
panic(result.Interface().(error))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user