diff --git a/beego.go b/beego.go index c22719c6..640fb5ab 100644 --- a/beego.go +++ b/beego.go @@ -221,7 +221,7 @@ func Run() { middleware.VERSION = VERSION middleware.AppName = AppName - middleware.RegisterErrorHander() + middleware.RegisterErrorHandler() if EnableAdmin { go BeeAdminApp.Run() diff --git a/middleware/error.go b/middleware/error.go index 35d9eb59..5c12b533 100644 --- a/middleware/error.go +++ b/middleware/error.go @@ -61,6 +61,7 @@ var tpl = ` ` +// render default application error page with error and stack string. func ShowErr(err interface{}, rw http.ResponseWriter, r *http.Request, Stack string) { t, _ := template.New("beegoerrortemp").Parse(tpl) data := make(map[string]string) @@ -175,13 +176,14 @@ var errtpl = ` ` +// map of http handlers for each error string. var ErrorMaps map[string]http.HandlerFunc func init() { ErrorMaps = make(map[string]http.HandlerFunc) } -//404 +// show 404 notfound error. func NotFound(rw http.ResponseWriter, r *http.Request) { t, _ := template.New("beegoerrortemp").Parse(errtpl) data := make(map[string]interface{}) @@ -199,7 +201,7 @@ func NotFound(rw http.ResponseWriter, r *http.Request) { t.Execute(rw, data) } -//401 +// show 401 unauthorized error. func Unauthorized(rw http.ResponseWriter, r *http.Request) { t, _ := template.New("beegoerrortemp").Parse(errtpl) data := make(map[string]interface{}) @@ -215,7 +217,7 @@ func Unauthorized(rw http.ResponseWriter, r *http.Request) { t.Execute(rw, data) } -//403 +// show 403 forbidden error. func Forbidden(rw http.ResponseWriter, r *http.Request) { t, _ := template.New("beegoerrortemp").Parse(errtpl) data := make(map[string]interface{}) @@ -232,7 +234,7 @@ func Forbidden(rw http.ResponseWriter, r *http.Request) { t.Execute(rw, data) } -//503 +// show 503 service unavailable error. func ServiceUnavailable(rw http.ResponseWriter, r *http.Request) { t, _ := template.New("beegoerrortemp").Parse(errtpl) data := make(map[string]interface{}) @@ -248,7 +250,7 @@ func ServiceUnavailable(rw http.ResponseWriter, r *http.Request) { t.Execute(rw, data) } -//500 +// show 500 internal server error. func InternalServerError(rw http.ResponseWriter, r *http.Request) { t, _ := template.New("beegoerrortemp").Parse(errtpl) data := make(map[string]interface{}) @@ -262,15 +264,18 @@ func InternalServerError(rw http.ResponseWriter, r *http.Request) { t.Execute(rw, data) } +// show 500 internal error with simple text string. func SimpleServerError(rw http.ResponseWriter, r *http.Request) { http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } +// add http handler for given error string. func Errorhandler(err string, h http.HandlerFunc) { ErrorMaps[err] = h } -func RegisterErrorHander() { +// register default error http handlers, 404,401,403,500 and 503. +func RegisterErrorHandler() { if _, ok := ErrorMaps["404"]; !ok { ErrorMaps["404"] = NotFound } @@ -292,6 +297,8 @@ func RegisterErrorHander() { } } +// show error string as simple text message. +// if error string is empty, show 500 error as default. func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg string) { if h, ok := ErrorMaps[errcode]; ok { isint, err := strconv.Atoi(errcode) diff --git a/middleware/exceptions.go b/middleware/exceptions.go index 5bf85956..b221dfcb 100644 --- a/middleware/exceptions.go +++ b/middleware/exceptions.go @@ -2,16 +2,19 @@ package middleware import "fmt" +// http exceptions type HTTPException struct { StatusCode int // http status code 4xx, 5xx Description string } +// return http exception error string, e.g. "400 Bad Request". func (e *HTTPException) Error() string { - // return `status description`, e.g. `400 Bad Request` return fmt.Sprintf("%d %s", e.StatusCode, e.Description) } +// map of http exceptions for each http status code int. +// defined 400,401,403,404,405,500,502,503 and 504 default. var HTTPExceptionMaps map[int]HTTPException func init() {