1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 03:11:30 +00:00

error map refactor

This commit is contained in:
JessonChan 2015-09-17 10:33:12 +08:00
parent cce8d1e934
commit bb776cc4cb

View File

@ -204,11 +204,7 @@ type errorInfo struct {
} }
// map of http handlers for each error string. // map of http handlers for each error string.
var ErrorMaps map[string]*errorInfo var ErrorMaps map[string]*errorInfo = make(map[string]*errorInfo, 10)
func init() {
ErrorMaps = make(map[string]*errorInfo)
}
// show 401 unauthorized error. // show 401 unauthorized error.
func unauthorized(rw http.ResponseWriter, r *http.Request) { func unauthorized(rw http.ResponseWriter, r *http.Request) {
@ -392,23 +388,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 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, err := strconv.Atoi(errcode) for ec, _ := range []string{errCode, "503", "500"} {
if err != nil { code, _ := strconv.Atoi(errCode)
if code == 0 {
code = 503 code = 503
} }
if h, ok := ErrorMaps[errcode]; ok { if h, ok := ErrorMaps[ec]; ok {
executeError(h, ctx, code) executeError(h, ctx, code)
return return
} else if h, ok := ErrorMaps["503"]; ok {
executeError(h, ctx, code)
return
} else {
ctx.ResponseWriter.WriteHeader(code)
ctx.WriteString(errcode)
} }
} }
//if 50x error has been removed from errorMap
ctx.ResponseWriter.WriteHeader(503)
ctx.WriteString(errCode)
}
func executeError(err *errorInfo, ctx *context.Context, code int) { func executeError(err *errorInfo, ctx *context.Context, code int) {
if err.errorType == errorTypeHandler { if err.errorType == errorTypeHandler {