diff --git a/beego.go b/beego.go index 329e0713..6c964d5a 100644 --- a/beego.go +++ b/beego.go @@ -40,6 +40,7 @@ func AddAPPStartHook(hf hookfunc) { // Run beego application. // beego.Run() default run on HttpPort +// beego.Run("localhost") // beego.Run(":8089") // beego.Run("127.0.0.1:8089") func Run(params ...string) { diff --git a/error.go b/error.go index ff060c4d..2a1a861d 100644 --- a/error.go +++ b/error.go @@ -204,6 +204,7 @@ type errorInfo struct { } // map of http handlers for each error string. +// there is 10 kinds default error(40x and 50x) var ErrorMaps = make(map[string]*errorInfo, 10) // show 401 unauthorized error. @@ -375,13 +376,14 @@ func ErrorController(c ControllerInterface) *App { rt := reflectVal.Type() ct := reflect.Indirect(reflectVal).Type() for i := 0; i < rt.NumMethod(); i++ { - if !utils.InSlice(rt.Method(i).Name, exceptMethod) && strings.HasPrefix(rt.Method(i).Name, "Error") { + methodName := rt.Method(i).Name + if !utils.InSlice(methodName, exceptMethod) && strings.HasPrefix(methodName, "Error") { errinfo := &errorInfo{} errinfo.errorType = errorTypeController errinfo.controllerType = ct - errinfo.method = rt.Method(i).Name - errname := strings.TrimPrefix(rt.Method(i).Name, "Error") - ErrorMaps[errname] = errinfo + errinfo.method = methodName + errName := strings.TrimPrefix(methodName, "Error") + ErrorMaps[errName] = errinfo } } return BeeApp @@ -390,18 +392,22 @@ func ErrorController(c ControllerInterface) *App { // show error string as simple text message. // if error string is empty, show 503 or 500 error as default. func exception(errCode string, ctx *context.Context) { - code, _ := strconv.Atoi(errCode) - if code == 0 { - code = 503 + atoi := func(code string) int { + v, err := strconv.Atoi(code) + if err == nil { + return v + } + return 503 } + for _, ec := range []string{errCode, "503", "500"} { if h, ok := ErrorMaps[ec]; ok { - executeError(h, ctx, code) + executeError(h, ctx, atoi(ec)) return } } //if 50x error has been removed from errorMap - ctx.ResponseWriter.WriteHeader(code) + ctx.ResponseWriter.WriteHeader(atoi(errCode)) ctx.WriteString(errCode) } diff --git a/router.go b/router.go index efb1a1f3..c0a50988 100644 --- a/router.go +++ b/router.go @@ -108,7 +108,7 @@ type controllerInfo struct { controllerType reflect.Type methods map[string]string handler http.Handler - runfunction FilterFunc + runFunction FilterFunc routerType int } @@ -324,7 +324,7 @@ func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) { route := &controllerInfo{} route.pattern = pattern route.routerType = routerTypeRESTFul - route.runfunction = f + route.runFunction = f methods := make(map[string]string) if method == "*" { for _, val := range HTTPMETHOD { @@ -725,7 +725,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) if routerInfo.routerType == routerTypeRESTFul { if _, ok := routerInfo.methods[r.Method]; ok { isRunable = true - routerInfo.runfunction(context) + routerInfo.runFunction(context) } else { exception("405", context) goto Admin