mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 13:11:28 +00:00
Merge pull request #1368 from JessonChan/fargo
error bug fixed and clean code
This commit is contained in:
commit
8af8936ee0
1
beego.go
1
beego.go
@ -40,6 +40,7 @@ func AddAPPStartHook(hf hookfunc) {
|
|||||||
|
|
||||||
// Run beego application.
|
// Run beego application.
|
||||||
// beego.Run() default run on HttpPort
|
// beego.Run() default run on HttpPort
|
||||||
|
// beego.Run("localhost")
|
||||||
// beego.Run(":8089")
|
// beego.Run(":8089")
|
||||||
// beego.Run("127.0.0.1:8089")
|
// beego.Run("127.0.0.1:8089")
|
||||||
func Run(params ...string) {
|
func Run(params ...string) {
|
||||||
|
24
error.go
24
error.go
@ -204,6 +204,7 @@ type errorInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// map of http handlers for each error string.
|
// map of http handlers for each error string.
|
||||||
|
// there is 10 kinds default error(40x and 50x)
|
||||||
var ErrorMaps = make(map[string]*errorInfo, 10)
|
var ErrorMaps = make(map[string]*errorInfo, 10)
|
||||||
|
|
||||||
// show 401 unauthorized error.
|
// show 401 unauthorized error.
|
||||||
@ -375,13 +376,14 @@ func ErrorController(c ControllerInterface) *App {
|
|||||||
rt := reflectVal.Type()
|
rt := reflectVal.Type()
|
||||||
ct := reflect.Indirect(reflectVal).Type()
|
ct := reflect.Indirect(reflectVal).Type()
|
||||||
for i := 0; i < rt.NumMethod(); i++ {
|
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 := &errorInfo{}
|
||||||
errinfo.errorType = errorTypeController
|
errinfo.errorType = errorTypeController
|
||||||
errinfo.controllerType = ct
|
errinfo.controllerType = ct
|
||||||
errinfo.method = rt.Method(i).Name
|
errinfo.method = methodName
|
||||||
errname := strings.TrimPrefix(rt.Method(i).Name, "Error")
|
errName := strings.TrimPrefix(methodName, "Error")
|
||||||
ErrorMaps[errname] = errinfo
|
ErrorMaps[errName] = errinfo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return BeeApp
|
return BeeApp
|
||||||
@ -390,18 +392,22 @@ func ErrorController(c ControllerInterface) *App {
|
|||||||
// show error string as simple text message.
|
// show error string as simple text message.
|
||||||
// if error string is empty, show 503 or 500 error as default.
|
// if error string is empty, show 503 or 500 error as default.
|
||||||
func exception(errCode string, ctx *context.Context) {
|
func exception(errCode string, ctx *context.Context) {
|
||||||
code, _ := strconv.Atoi(errCode)
|
atoi := func(code string) int {
|
||||||
if code == 0 {
|
v, err := strconv.Atoi(code)
|
||||||
code = 503
|
if err == nil {
|
||||||
|
return v
|
||||||
}
|
}
|
||||||
|
return 503
|
||||||
|
}
|
||||||
|
|
||||||
for _, ec := range []string{errCode, "503", "500"} {
|
for _, ec := range []string{errCode, "503", "500"} {
|
||||||
if h, ok := ErrorMaps[ec]; ok {
|
if h, ok := ErrorMaps[ec]; ok {
|
||||||
executeError(h, ctx, code)
|
executeError(h, ctx, atoi(ec))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//if 50x error has been removed from errorMap
|
//if 50x error has been removed from errorMap
|
||||||
ctx.ResponseWriter.WriteHeader(code)
|
ctx.ResponseWriter.WriteHeader(atoi(errCode))
|
||||||
ctx.WriteString(errCode)
|
ctx.WriteString(errCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +108,7 @@ type controllerInfo struct {
|
|||||||
controllerType reflect.Type
|
controllerType reflect.Type
|
||||||
methods map[string]string
|
methods map[string]string
|
||||||
handler http.Handler
|
handler http.Handler
|
||||||
runfunction FilterFunc
|
runFunction FilterFunc
|
||||||
routerType int
|
routerType int
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -324,7 +324,7 @@ func (p *ControllerRegister) AddMethod(method, pattern string, f FilterFunc) {
|
|||||||
route := &controllerInfo{}
|
route := &controllerInfo{}
|
||||||
route.pattern = pattern
|
route.pattern = pattern
|
||||||
route.routerType = routerTypeRESTFul
|
route.routerType = routerTypeRESTFul
|
||||||
route.runfunction = f
|
route.runFunction = f
|
||||||
methods := make(map[string]string)
|
methods := make(map[string]string)
|
||||||
if method == "*" {
|
if method == "*" {
|
||||||
for _, val := range HTTPMETHOD {
|
for _, val := range HTTPMETHOD {
|
||||||
@ -725,7 +725,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
if routerInfo.routerType == routerTypeRESTFul {
|
if routerInfo.routerType == routerTypeRESTFul {
|
||||||
if _, ok := routerInfo.methods[r.Method]; ok {
|
if _, ok := routerInfo.methods[r.Method]; ok {
|
||||||
isRunable = true
|
isRunable = true
|
||||||
routerInfo.runfunction(context)
|
routerInfo.runFunction(context)
|
||||||
} else {
|
} else {
|
||||||
exception("405", context)
|
exception("405", context)
|
||||||
goto Admin
|
goto Admin
|
||||||
|
Loading…
Reference in New Issue
Block a user