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

Merge pull request #317 from pengfei-xue/master

beego.Context.Abort return immediately
This commit is contained in:
astaxie 2013-11-25 18:30:38 -08:00
commit e09e642a83
3 changed files with 62 additions and 17 deletions

View File

@ -2,6 +2,8 @@ package context
import ( import (
"net/http" "net/http"
"github.com/astaxie/beego/middleware"
) )
type Context struct { type Context struct {
@ -19,6 +21,13 @@ func (ctx *Context) Redirect(status int, localurl string) {
func (ctx *Context) Abort(status int, body string) { func (ctx *Context) Abort(status int, body string) {
ctx.Output.SetStatus(status) ctx.Output.SetStatus(status)
ctx.Output.Body([]byte(body)) ctx.Output.Body([]byte(body))
if e, ok := middleware.HTTPExceptionMaps[status]; ok {
if len(body) >= 1 {
e.Description = body
}
panic(e)
}
} }
func (ctx *Context) WriteString(content string) { func (ctx *Context) WriteString(content string) {

32
middleware/exceptions.go Normal file
View File

@ -0,0 +1,32 @@
package middleware
import "fmt"
type HTTPException struct {
StatusCode int // http status code 4xx, 5xx
Description string
}
func (e *HTTPException) Error() string {
// return `status description`, e.g. `400 Bad Request`
return fmt.Sprintf("%d %s", e.StatusCode, e.Description)
}
var HTTPExceptionMaps map[int]HTTPException
func init() {
HTTPExceptionMaps = make(map[int]HTTPException)
// Normal 4XX HTTP Status
HTTPExceptionMaps[400] = HTTPException{400, "Bad Request"}
HTTPExceptionMaps[401] = HTTPException{401, "Unauthorized"}
HTTPExceptionMaps[403] = HTTPException{403, "Forbidden"}
HTTPExceptionMaps[404] = HTTPException{404, "Not Found"}
HTTPExceptionMaps[405] = HTTPException{405, "Method Not Allowed"}
// Normal 5XX HTTP Status
HTTPExceptionMaps[500] = HTTPException{500, "Internal Server Error"}
HTTPExceptionMaps[502] = HTTPException{502, "Bad Gateway"}
HTTPExceptionMaps[503] = HTTPException{503, "Service Unavailable"}
HTTPExceptionMaps[504] = HTTPException{504, "Gateway Timeout"}
}

View File

@ -407,29 +407,33 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {
func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) { func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
errstr := fmt.Sprint(err) if _, ok := err.(middleware.HTTPException); ok {
if handler, ok := middleware.ErrorMaps[errstr]; ok && ErrorsShow { // catch intented errors, only for HTTP 4XX and 5XX
handler(rw, r)
} else { } else {
if !RecoverPanic { errstr := fmt.Sprint(err)
// go back to panic if handler, ok := middleware.ErrorMaps[errstr]; ok && ErrorsShow {
panic(err) handler(rw, r)
} else { } else {
var stack string if !RecoverPanic {
Critical("Handler crashed with error", err) // go back to panic
for i := 1; ; i++ { panic(err)
_, file, line, ok := runtime.Caller(i) } else {
if !ok { var stack string
break Critical("Handler crashed with error", err)
for i := 1; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
Critical(file, line)
if RunMode == "dev" {
stack = stack + fmt.Sprintln(file, line)
}
} }
Critical(file, line)
if RunMode == "dev" { if RunMode == "dev" {
stack = stack + fmt.Sprintln(file, line) middleware.ShowErr(err, rw, r, stack)
} }
} }
if RunMode == "dev" {
middleware.ShowErr(err, rw, r, stack)
}
} }
} }
} }