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 } }