package middleware import ( "fmt" "html/template" "net/http" "runtime" "strconv" ) var ( AppName string VERSION string ) 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() rw.WriteHeader(500) 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 has 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 be 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 is 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 is 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 is down right now." + "

") data["BeegoVersion"] = VERSION //rw.WriteHeader(http.StatusInternalServerError) 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 } 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 } } func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg string) { if h, ok := ErrorMaps[errcode]; ok { isint, err := strconv.Atoi(errcode) if err != nil { isint = 500 } w.WriteHeader(isint) 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 } }