Panic template execution errors to show error pages accordingly
Showing
2 changed files
with
26 additions
and
8 deletions
| ... | @@ -138,6 +138,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { | ... | @@ -138,6 +138,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { |
| 138 | err := BeeTemplates[c.TplNames].ExecuteTemplate(newbytes, c.TplNames, c.Data) | 138 | err := BeeTemplates[c.TplNames].ExecuteTemplate(newbytes, c.TplNames, c.Data) |
| 139 | if err != nil { | 139 | if err != nil { |
| 140 | Trace("template Execute err:", err) | 140 | Trace("template Execute err:", err) |
| 141 | return nil, err | ||
| 141 | } | 142 | } |
| 142 | tplcontent, _ := ioutil.ReadAll(newbytes) | 143 | tplcontent, _ := ioutil.ReadAll(newbytes) |
| 143 | c.Data["LayoutContent"] = template.HTML(string(tplcontent)) | 144 | c.Data["LayoutContent"] = template.HTML(string(tplcontent)) |
| ... | @@ -145,6 +146,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { | ... | @@ -145,6 +146,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { |
| 145 | err = BeeTemplates[c.Layout].ExecuteTemplate(ibytes, c.Layout, c.Data) | 146 | err = BeeTemplates[c.Layout].ExecuteTemplate(ibytes, c.Layout, c.Data) |
| 146 | if err != nil { | 147 | if err != nil { |
| 147 | Trace("template Execute err:", err) | 148 | Trace("template Execute err:", err) |
| 149 | return nil, err | ||
| 148 | } | 150 | } |
| 149 | icontent, _ := ioutil.ReadAll(ibytes) | 151 | icontent, _ := ioutil.ReadAll(ibytes) |
| 150 | return icontent, nil | 152 | return icontent, nil |
| ... | @@ -163,6 +165,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { | ... | @@ -163,6 +165,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { |
| 163 | err := BeeTemplates[c.TplNames].ExecuteTemplate(ibytes, c.TplNames, c.Data) | 165 | err := BeeTemplates[c.TplNames].ExecuteTemplate(ibytes, c.TplNames, c.Data) |
| 164 | if err != nil { | 166 | if err != nil { |
| 165 | Trace("template Execute err:", err) | 167 | Trace("template Execute err:", err) |
| 168 | return nil, err | ||
| 166 | } | 169 | } |
| 167 | icontent, _ := ioutil.ReadAll(ibytes) | 170 | icontent, _ := ioutil.ReadAll(ibytes) |
| 168 | return icontent, nil | 171 | return icontent, nil | ... | ... |
| ... | @@ -26,7 +26,10 @@ const ( | ... | @@ -26,7 +26,10 @@ const ( |
| 26 | FinishRouter | 26 | FinishRouter |
| 27 | ) | 27 | ) |
| 28 | 28 | ||
| 29 | var HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"} | 29 | var ( |
| 30 | HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"} | ||
| 31 | errorType = reflect.TypeOf((*error)(nil)).Elem() | ||
| 32 | ) | ||
| 30 | 33 | ||
| 31 | type controllerInfo struct { | 34 | type controllerInfo struct { |
| 32 | pattern string | 35 | pattern string |
| ... | @@ -441,7 +444,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -441,7 +444,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 441 | } | 444 | } |
| 442 | } | 445 | } |
| 443 | 446 | ||
| 444 | return false | 447 | return false |
| 445 | } | 448 | } |
| 446 | 449 | ||
| 447 | if context.Input.IsWebsocket() { | 450 | if context.Input.IsWebsocket() { |
| ... | @@ -481,7 +484,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -481,7 +484,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 481 | middleware.Exception("403", rw, r, "403 Forbidden") | 484 | middleware.Exception("403", rw, r, "403 Forbidden") |
| 482 | goto Admin | 485 | goto Admin |
| 483 | } | 486 | } |
| 484 | 487 | ||
| 485 | //This block obtained from (https://github.com/smithfox/beego) - it should probably get merged into astaxie/beego after a pull request | 488 | //This block obtained from (https://github.com/smithfox/beego) - it should probably get merged into astaxie/beego after a pull request |
| 486 | isStaticFileToCompress := false | 489 | isStaticFileToCompress := false |
| 487 | if StaticExtensionsToGzip != nil && len(StaticExtensionsToGzip) > 0 { | 490 | if StaticExtensionsToGzip != nil && len(StaticExtensionsToGzip) > 0 { |
| ... | @@ -513,7 +516,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -513,7 +516,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 513 | } else { | 516 | } else { |
| 514 | http.ServeFile(w, r, file) | 517 | http.ServeFile(w, r, file) |
| 515 | } | 518 | } |
| 516 | 519 | ||
| 517 | w.started = true | 520 | w.started = true |
| 518 | goto Admin | 521 | goto Admin |
| 519 | } | 522 | } |
| ... | @@ -729,7 +732,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -729,7 +732,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 729 | if !w.started && !context.Input.IsWebsocket() { | 732 | if !w.started && !context.Input.IsWebsocket() { |
| 730 | if AutoRender { | 733 | if AutoRender { |
| 731 | method = vc.MethodByName("Render") | 734 | method = vc.MethodByName("Render") |
| 732 | method.Call(in) | 735 | callMethodWithError(method, in) |
| 733 | } | 736 | } |
| 734 | } | 737 | } |
| 735 | } | 738 | } |
| ... | @@ -885,9 +888,9 @@ func (p *ControllerRegistor) getErrorHandler(errorCode string) func(rw http.Resp | ... | @@ -885,9 +888,9 @@ func (p *ControllerRegistor) getErrorHandler(errorCode string) func(rw http.Resp |
| 885 | //responseWriter is a wrapper for the http.ResponseWriter | 888 | //responseWriter is a wrapper for the http.ResponseWriter |
| 886 | //started set to true if response was written to then don't execute other handler | 889 | //started set to true if response was written to then don't execute other handler |
| 887 | type responseWriter struct { | 890 | type responseWriter struct { |
| 888 | writer http.ResponseWriter | 891 | writer http.ResponseWriter |
| 889 | started bool | 892 | started bool |
| 890 | status int | 893 | status int |
| 891 | contentEncoding string | 894 | contentEncoding string |
| 892 | } | 895 | } |
| 893 | 896 | ||
| ... | @@ -920,3 +923,15 @@ func (w *responseWriter) WriteHeader(code int) { | ... | @@ -920,3 +923,15 @@ func (w *responseWriter) WriteHeader(code int) { |
| 920 | w.started = true | 923 | w.started = true |
| 921 | w.writer.WriteHeader(code) | 924 | w.writer.WriteHeader(code) |
| 922 | } | 925 | } |
| 926 | |||
| 927 | // call method and panic with error if error is in result params | ||
| 928 | func callMethodWithError(method reflect.Value, params []reflect.Value) { | ||
| 929 | results := method.Call(params) | ||
| 930 | if len(results) > 0 { | ||
| 931 | for _, result := range results { | ||
| 932 | if result.Type() == errorType && !result.IsNil() { | ||
| 933 | panic(result.Interface().(error)) | ||
| 934 | } | ||
| 935 | } | ||
| 936 | } | ||
| 937 | } | ... | ... |
-
Please register or sign in to post a comment