1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-26 12:20:49 +00:00

Merge pull request #344 from vadimi/master

Improve unhandled error handling in prod mode
This commit is contained in:
astaxie 2013-12-05 21:55:37 -08:00
commit 8cf34fce98
2 changed files with 29 additions and 7 deletions

View File

@ -13,8 +13,8 @@ var (
VERSION string
)
var tpl = `
<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>beego application error</title>
@ -28,10 +28,10 @@ var tpl = `
#content .stack pre{padding-left: 10px;}
table {}
td.t {text-align: right; padding-right: 5px; color: #888;}
</style>
</style>
<script type="text/javascript">
</script>
</head>
</head>
<body>
<div id="header">
<h2>{{.AppError}}</h2>
@ -58,7 +58,7 @@ var tpl = `
<p>golang version: {{.GoVersion}}</p>
</div>
</body>
</html>
</html>
`
func ShowErr(err interface{}, rw http.ResponseWriter, r *http.Request, Stack string) {
@ -262,6 +262,10 @@ func InternalServerError(rw http.ResponseWriter, r *http.Request) {
t.Execute(rw, data)
}
func SimpleServerError(rw http.ResponseWriter, r *http.Request) {
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
func Errorhandler(err string, h http.HandlerFunc) {
ErrorMaps[err] = h
}

View File

@ -368,8 +368,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if _, ok := err.(middleware.HTTPException); ok {
// catch intented errors, only for HTTP 4XX and 5XX
} else {
errstr := fmt.Sprint(err)
if handler, ok := middleware.ErrorMaps[errstr]; ok && ErrorsShow {
if ErrorsShow {
handler := p.getErrorHandler(fmt.Sprint(err))
handler(rw, r)
} else {
if !RecoverPanic {
@ -865,6 +865,24 @@ Admin:
}
}
// there always should be error handler that sets error code accordingly for all unhandled errors
// in order to have custom UI for error page it's necessary to override "500" error
func (p *ControllerRegistor) getErrorHandler(errorCode string) func(rw http.ResponseWriter, r *http.Request) {
handler := middleware.SimpleServerError
ok := true
if errorCode != "" {
handler, ok = middleware.ErrorMaps[errorCode]
if !ok {
handler, ok = middleware.ErrorMaps["500"]
}
if !ok || handler == nil {
handler = middleware.SimpleServerError
}
}
return handler
}
//responseWriter is a wrapper for the http.ResponseWriter
//started set to true if response was written to then don't execute other handler
type responseWriter struct {