mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 17:00:55 +00:00
commit
0d100fef7d
35
error.go
35
error.go
@ -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,22 +388,21 @@ 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)
|
||||||
code = 503
|
if code == 0 {
|
||||||
}
|
code = 503
|
||||||
if h, ok := ErrorMaps[errcode]; ok {
|
}
|
||||||
executeError(h, ctx, code)
|
if h, ok := ErrorMaps[ec]; ok {
|
||||||
return
|
executeError(h, ctx, code)
|
||||||
} else if h, ok := ErrorMaps["503"]; ok {
|
return
|
||||||
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) {
|
||||||
|
52
hooks.go
52
hooks.go
@ -5,6 +5,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/astaxie/beego/session"
|
"github.com/astaxie/beego/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,42 +20,22 @@ func registerMime() error {
|
|||||||
|
|
||||||
// register default error http handlers, 404,401,403,500 and 503.
|
// register default error http handlers, 404,401,403,500 and 503.
|
||||||
func registerDefaultErrorHandler() error {
|
func registerDefaultErrorHandler() error {
|
||||||
if _, ok := ErrorMaps["401"]; !ok {
|
|
||||||
ErrorHandler("401", unauthorized)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := ErrorMaps["402"]; !ok {
|
for e, h := range map[string]func(http.ResponseWriter, *http.Request){
|
||||||
ErrorHandler("402", paymentRequired)
|
"401": unauthorized,
|
||||||
}
|
"402": paymentRequired,
|
||||||
|
"403": forbidden,
|
||||||
if _, ok := ErrorMaps["403"]; !ok {
|
"404": notFound,
|
||||||
ErrorHandler("403", forbidden)
|
"405": methodNotAllowed,
|
||||||
}
|
"500": internalServerError,
|
||||||
|
"501": notImplemented,
|
||||||
if _, ok := ErrorMaps["404"]; !ok {
|
"502": badGateway,
|
||||||
ErrorHandler("404", notFound)
|
"503": serviceUnavailable,
|
||||||
}
|
"504": gatewayTimeout,
|
||||||
|
} {
|
||||||
if _, ok := ErrorMaps["405"]; !ok {
|
if _, ok := ErrorMaps[e]; !ok {
|
||||||
ErrorHandler("405", methodNotAllowed)
|
ErrorHandler(e, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := ErrorMaps["500"]; !ok {
|
|
||||||
ErrorHandler("500", internalServerError)
|
|
||||||
}
|
|
||||||
if _, ok := ErrorMaps["501"]; !ok {
|
|
||||||
ErrorHandler("501", notImplemented)
|
|
||||||
}
|
|
||||||
if _, ok := ErrorMaps["502"]; !ok {
|
|
||||||
ErrorHandler("502", badGateway)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := ErrorMaps["503"]; !ok {
|
|
||||||
ErrorHandler("503", serviceUnavailable)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := ErrorMaps["504"]; !ok {
|
|
||||||
ErrorHandler("504", gatewayTimeout)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user