mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 18:50:54 +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.
|
||||
// beego.Run() default run on HttpPort
|
||||
// beego.Run("localhost")
|
||||
// beego.Run(":8089")
|
||||
// beego.Run("127.0.0.1:8089")
|
||||
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.
|
||||
// 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)
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user