From 9f6b803a10c87aa52943a1d5804c2840951599e3 Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 11 Sep 2013 17:00:39 +0800 Subject: [PATCH] update middleware & beego's error --- beego.go | 9 +- errors.go | 280 -------------------------------------------- middleware/error.go | 20 ++++ router.go | 23 +--- 4 files changed, 33 insertions(+), 299 deletions(-) delete mode 100644 errors.go diff --git a/beego.go b/beego.go index 6acf4ac1..55049090 100644 --- a/beego.go +++ b/beego.go @@ -1,6 +1,7 @@ package beego import ( + "github.com/astaxie/beego/middleware" "github.com/astaxie/beego/session" "net/http" "path" @@ -25,7 +26,7 @@ func AutoRouter(c ControllerInterface) *App { } func Errorhandler(err string, h http.HandlerFunc) *App { - ErrorMaps[err] = h + middleware.Errorhandler(err, h) return BeeApp } @@ -78,6 +79,10 @@ func Run() { } } } - registerErrorHander() + + middleware.VERSION = VERSION + middleware.AppName = AppName + middleware.RegisterErrorHander() + BeeApp.Run() } diff --git a/errors.go b/errors.go deleted file mode 100644 index 45ce9114..00000000 --- a/errors.go +++ /dev/null @@ -1,280 +0,0 @@ -package beego - -import ( - "fmt" - "html/template" - "net/http" - "runtime" -) - -var tpl = ` - - - - - beego application error - - - - - -
- - - - - - - - - - -
Request Method: {{.RequestMethod}}
Request URL: {{.RequestURL}}
RemoteAddr: {{.RemoteAddr }}
-
- Stack -
{{.Stack}}
-
-
- - - -` - -func ShowErr(err interface{}, rw http.ResponseWriter, r *http.Request, Stack string) { - t, _ := template.New("beegoerrortemp").Parse(tpl) - data := make(map[string]string) - data["AppError"] = AppName + ":" + fmt.Sprint(err) - data["RequestMethod"] = r.Method - data["RequestURL"] = r.RequestURI - data["RemoteAddr"] = r.RemoteAddr - data["Stack"] = Stack - data["BeegoVersion"] = VERSION - data["GoVersion"] = runtime.Version() - t.Execute(rw, data) -} - -var errtpl = ` - - - - - {{.Title}} - - - -
-
- -
- {{.Content}} - Go Home
- -
power by beego {{.BeegoVersion}} -
-
-
- - -` - -var ErrorMaps map[string]http.HandlerFunc - -func init() { - ErrorMaps = make(map[string]http.HandlerFunc) -} - -//404 -func NotFound(rw http.ResponseWriter, r *http.Request) { - t, _ := template.New("beegoerrortemp").Parse(errtpl) - data := make(map[string]interface{}) - data["Title"] = "Page Not Found" - data["Content"] = template.HTML("
The Page You have requested flown the coop." + - "
Perhaps you are here because:" + - "

") - data["BeegoVersion"] = VERSION - rw.WriteHeader(http.StatusNotFound) - t.Execute(rw, data) -} - -//401 -func Unauthorized(rw http.ResponseWriter, r *http.Request) { - t, _ := template.New("beegoerrortemp").Parse(errtpl) - data := make(map[string]interface{}) - data["Title"] = "Unauthorized" - data["Content"] = template.HTML("
The Page You have requested can't authorized." + - "
Perhaps you are here because:" + - "

") - data["BeegoVersion"] = VERSION - rw.WriteHeader(http.StatusUnauthorized) - t.Execute(rw, data) -} - -//403 -func Forbidden(rw http.ResponseWriter, r *http.Request) { - t, _ := template.New("beegoerrortemp").Parse(errtpl) - data := make(map[string]interface{}) - data["Title"] = "Forbidden" - data["Content"] = template.HTML("
The Page You have requested forbidden." + - "
Perhaps you are here because:" + - "

") - data["BeegoVersion"] = VERSION - rw.WriteHeader(http.StatusForbidden) - t.Execute(rw, data) -} - -//503 -func ServiceUnavailable(rw http.ResponseWriter, r *http.Request) { - t, _ := template.New("beegoerrortemp").Parse(errtpl) - data := make(map[string]interface{}) - data["Title"] = "Service Unavailable" - data["Content"] = template.HTML("
The Page You have requested unavailable." + - "
Perhaps you are here because:" + - "

") - data["BeegoVersion"] = VERSION - rw.WriteHeader(http.StatusServiceUnavailable) - t.Execute(rw, data) -} - -//500 -func InternalServerError(rw http.ResponseWriter, r *http.Request) { - t, _ := template.New("beegoerrortemp").Parse(errtpl) - data := make(map[string]interface{}) - data["Title"] = "Internal Server Error" - data["Content"] = template.HTML("
The Page You have requested has down now." + - "

") - data["BeegoVersion"] = VERSION - rw.WriteHeader(http.StatusInternalServerError) - t.Execute(rw, data) -} - -func registerErrorHander() { - if _, ok := ErrorMaps["404"]; !ok { - ErrorMaps["404"] = NotFound - } - - if _, ok := ErrorMaps["401"]; !ok { - ErrorMaps["401"] = Unauthorized - } - - if _, ok := ErrorMaps["403"]; !ok { - ErrorMaps["403"] = Forbidden - } - - if _, ok := ErrorMaps["503"]; !ok { - ErrorMaps["503"] = ServiceUnavailable - } - - if _, ok := ErrorMaps["500"]; !ok { - ErrorMaps["500"] = InternalServerError - } -} diff --git a/middleware/error.go b/middleware/error.go index 50f810b8..566b658a 100644 --- a/middleware/error.go +++ b/middleware/error.go @@ -5,6 +5,7 @@ import ( "html/template" "net/http" "runtime" + "strconv" ) var ( @@ -286,3 +287,22 @@ func RegisterErrorHander() { ErrorMaps["500"] = InternalServerError } } + +func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg string) { + if h, ok := ErrorMaps[errcode]; ok { + h(w, r) + return + } else { + isint, err := strconv.Atoi(errcode) + if err != nil { + isint = 500 + } + if isint == 400 { + msg = "404 page not found" + } + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.WriteHeader(isint) + fmt.Fprintln(w, msg) + return + } +} diff --git a/router.go b/router.go index b3b5ffa1..387c24ed 100644 --- a/router.go +++ b/router.go @@ -3,6 +3,7 @@ package beego import ( "fmt" beecontext "github.com/astaxie/beego/context" + "github.com/astaxie/beego/middleware" "net/http" "net/url" "os" @@ -222,7 +223,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) defer func() { if err := recover(); err != nil { errstr := fmt.Sprint(err) - if handler, ok := ErrorMaps[errstr]; ok && ErrorsShow { + if handler, ok := middleware.ErrorMaps[errstr]; ok && ErrorsShow { handler(rw, r) } else { if !RecoverPanic { @@ -242,7 +243,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) } } if RunMode == "dev" { - ShowErr(err, rw, r, stack) + middleware.ShowErr(err, rw, r, stack) } } } @@ -296,15 +297,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) } //if the request is dir and DirectoryIndex is false then if finfo.IsDir() && !DirectoryIndex { - if h, ok := ErrorMaps["403"]; ok { - h(w, r) - return - } else { - w.Header().Set("Content-Type", "text/plain; charset=utf-8") - w.WriteHeader(403) - fmt.Fprintln(w, "403 Forbidden") - return - } + middleware.Exception("403", rw, r, "403 Forbidden") + return } http.ServeFile(w, r, file) w.started = true @@ -641,12 +635,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) Last: //if no matches to url, throw a not found exception if !findrouter { - if h, ok := ErrorMaps["404"]; ok { - w.status = 404 - h(w, r) - } else { - http.NotFound(w, r) - } + middleware.Exception("404", rw, r, "") } }