8cf34fce by astaxie

Merge pull request #344 from vadimi/master

Improve unhandled error handling in prod mode
2 parents de70732c d7997797
...@@ -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!