8cf34fce by astaxie

Merge pull request #344 from vadimi/master

Improve unhandled error handling in prod mode
2 parents de70732c d7997797
...@@ -13,8 +13,8 @@ var ( ...@@ -13,8 +13,8 @@ var (
13 VERSION string 13 VERSION string
14 ) 14 )
15 var tpl = ` 15 var tpl = `
16 <!DOCTYPE html> 16 <!DOCTYPE html>
17 <html> 17 <html>
18 <head> 18 <head>
19 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 19 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
20 <title>beego application error</title> 20 <title>beego application error</title>
...@@ -28,10 +28,10 @@ var tpl = ` ...@@ -28,10 +28,10 @@ var tpl = `
28 #content .stack pre{padding-left: 10px;} 28 #content .stack pre{padding-left: 10px;}
29 table {} 29 table {}
30 td.t {text-align: right; padding-right: 5px; color: #888;} 30 td.t {text-align: right; padding-right: 5px; color: #888;}
31 </style> 31 </style>
32 <script type="text/javascript"> 32 <script type="text/javascript">
33 </script> 33 </script>
34 </head> 34 </head>
35 <body> 35 <body>
36 <div id="header"> 36 <div id="header">
37 <h2>{{.AppError}}</h2> 37 <h2>{{.AppError}}</h2>
...@@ -58,7 +58,7 @@ var tpl = ` ...@@ -58,7 +58,7 @@ var tpl = `
58 <p>golang version: {{.GoVersion}}</p> 58 <p>golang version: {{.GoVersion}}</p>
59 </div> 59 </div>
60 </body> 60 </body>
61 </html> 61 </html>
62 ` 62 `
63 63
64 func ShowErr(err interface{}, rw http.ResponseWriter, r *http.Request, Stack string) { 64 func ShowErr(err interface{}, rw http.ResponseWriter, r *http.Request, Stack string) {
...@@ -262,6 +262,10 @@ func InternalServerError(rw http.ResponseWriter, r *http.Request) { ...@@ -262,6 +262,10 @@ func InternalServerError(rw http.ResponseWriter, r *http.Request) {
262 t.Execute(rw, data) 262 t.Execute(rw, data)
263 } 263 }
264 264
265 func SimpleServerError(rw http.ResponseWriter, r *http.Request) {
266 http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
267 }
268
265 func Errorhandler(err string, h http.HandlerFunc) { 269 func Errorhandler(err string, h http.HandlerFunc) {
266 ErrorMaps[err] = h 270 ErrorMaps[err] = h
267 } 271 }
......
...@@ -368,8 +368,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -368,8 +368,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
368 if _, ok := err.(middleware.HTTPException); ok { 368 if _, ok := err.(middleware.HTTPException); ok {
369 // catch intented errors, only for HTTP 4XX and 5XX 369 // catch intented errors, only for HTTP 4XX and 5XX
370 } else { 370 } else {
371 errstr := fmt.Sprint(err) 371 if ErrorsShow {
372 if handler, ok := middleware.ErrorMaps[errstr]; ok && ErrorsShow { 372 handler := p.getErrorHandler(fmt.Sprint(err))
373 handler(rw, r) 373 handler(rw, r)
374 } else { 374 } else {
375 if !RecoverPanic { 375 if !RecoverPanic {
...@@ -865,6 +865,24 @@ Admin: ...@@ -865,6 +865,24 @@ Admin:
865 } 865 }
866 } 866 }
867 867
868 // there always should be error handler that sets error code accordingly for all unhandled errors
869 // in order to have custom UI for error page it's necessary to override "500" error
870 func (p *ControllerRegistor) getErrorHandler(errorCode string) func(rw http.ResponseWriter, r *http.Request) {
871 handler := middleware.SimpleServerError
872 ok := true
873 if errorCode != "" {
874 handler, ok = middleware.ErrorMaps[errorCode]
875 if !ok {
876 handler, ok = middleware.ErrorMaps["500"]
877 }
878 if !ok || handler == nil {
879 handler = middleware.SimpleServerError
880 }
881 }
882
883 return handler
884 }
885
868 //responseWriter is a wrapper for the http.ResponseWriter 886 //responseWriter is a wrapper for the http.ResponseWriter
869 //started set to true if response was written to then don't execute other handler 887 //started set to true if response was written to then don't execute other handler
870 type responseWriter struct { 888 type responseWriter struct {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!