// Copyright 2014 beego Author. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. 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}}
` // 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) 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

Powered by beego {{.BeegoVersion}}
` // map of http handlers for each error string. var ErrorMaps map[string]http.HandlerFunc func init() { ErrorMaps = make(map[string]http.HandlerFunc) } // show 404 notfound error. 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) } // show 401 unauthorized error. 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) } // show 403 forbidden error. 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) } // show 503 service unavailable error. 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) } // show 500 internal server error. 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) } // 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 } // register default error http handlers, 404,401,403,500 and 503. func RegisterErrorHandler() { 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 } } // 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) if err != nil { isint = 500 } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(isint) h(w, r) return } else { isint, err := strconv.Atoi(errcode) if err != nil { isint = 500 } if isint == 404 { msg = "404 page not found" } w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.WriteHeader(isint) fmt.Fprintln(w, msg) return } }