Merge pull request #317 from pengfei-xue/master
beego.Context.Abort return immediately
Showing
3 changed files
with
62 additions
and
17 deletions
| ... | @@ -2,6 +2,8 @@ package context | ... | @@ -2,6 +2,8 @@ package context |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "net/http" | 4 | "net/http" |
| 5 | |||
| 6 | "github.com/astaxie/beego/middleware" | ||
| 5 | ) | 7 | ) |
| 6 | 8 | ||
| 7 | type Context struct { | 9 | type Context struct { |
| ... | @@ -19,6 +21,13 @@ func (ctx *Context) Redirect(status int, localurl string) { | ... | @@ -19,6 +21,13 @@ func (ctx *Context) Redirect(status int, localurl string) { |
| 19 | func (ctx *Context) Abort(status int, body string) { | 21 | func (ctx *Context) Abort(status int, body string) { |
| 20 | ctx.Output.SetStatus(status) | 22 | ctx.Output.SetStatus(status) |
| 21 | ctx.Output.Body([]byte(body)) | 23 | ctx.Output.Body([]byte(body)) |
| 24 | |||
| 25 | if e, ok := middleware.HTTPExceptionMaps[status]; ok { | ||
| 26 | if len(body) >= 1 { | ||
| 27 | e.Description = body | ||
| 28 | } | ||
| 29 | panic(e) | ||
| 30 | } | ||
| 22 | } | 31 | } |
| 23 | 32 | ||
| 24 | func (ctx *Context) WriteString(content string) { | 33 | func (ctx *Context) WriteString(content string) { | ... | ... |
middleware/exceptions.go
0 → 100644
| 1 | package middleware | ||
| 2 | |||
| 3 | import "fmt" | ||
| 4 | |||
| 5 | type HTTPException struct { | ||
| 6 | StatusCode int // http status code 4xx, 5xx | ||
| 7 | Description string | ||
| 8 | } | ||
| 9 | |||
| 10 | func (e *HTTPException) Error() string { | ||
| 11 | // return `status description`, e.g. `400 Bad Request` | ||
| 12 | return fmt.Sprintf("%d %s", e.StatusCode, e.Description) | ||
| 13 | } | ||
| 14 | |||
| 15 | var HTTPExceptionMaps map[int]HTTPException | ||
| 16 | |||
| 17 | func init() { | ||
| 18 | HTTPExceptionMaps = make(map[int]HTTPException) | ||
| 19 | |||
| 20 | // Normal 4XX HTTP Status | ||
| 21 | HTTPExceptionMaps[400] = HTTPException{400, "Bad Request"} | ||
| 22 | HTTPExceptionMaps[401] = HTTPException{401, "Unauthorized"} | ||
| 23 | HTTPExceptionMaps[403] = HTTPException{403, "Forbidden"} | ||
| 24 | HTTPExceptionMaps[404] = HTTPException{404, "Not Found"} | ||
| 25 | HTTPExceptionMaps[405] = HTTPException{405, "Method Not Allowed"} | ||
| 26 | |||
| 27 | // Normal 5XX HTTP Status | ||
| 28 | HTTPExceptionMaps[500] = HTTPException{500, "Internal Server Error"} | ||
| 29 | HTTPExceptionMaps[502] = HTTPException{502, "Bad Gateway"} | ||
| 30 | HTTPExceptionMaps[503] = HTTPException{503, "Service Unavailable"} | ||
| 31 | HTTPExceptionMaps[504] = HTTPException{504, "Gateway Timeout"} | ||
| 32 | } |
| ... | @@ -407,29 +407,33 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { | ... | @@ -407,29 +407,33 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { |
| 407 | func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) { | 407 | func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) { |
| 408 | defer func() { | 408 | defer func() { |
| 409 | if err := recover(); err != nil { | 409 | if err := recover(); err != nil { |
| 410 | errstr := fmt.Sprint(err) | 410 | if _, ok := err.(middleware.HTTPException); ok { |
| 411 | if handler, ok := middleware.ErrorMaps[errstr]; ok && ErrorsShow { | 411 | // catch intented errors, only for HTTP 4XX and 5XX |
| 412 | handler(rw, r) | ||
| 413 | } else { | 412 | } else { |
| 414 | if !RecoverPanic { | 413 | errstr := fmt.Sprint(err) |
| 415 | // go back to panic | 414 | if handler, ok := middleware.ErrorMaps[errstr]; ok && ErrorsShow { |
| 416 | panic(err) | 415 | handler(rw, r) |
| 417 | } else { | 416 | } else { |
| 418 | var stack string | 417 | if !RecoverPanic { |
| 419 | Critical("Handler crashed with error", err) | 418 | // go back to panic |
| 420 | for i := 1; ; i++ { | 419 | panic(err) |
| 421 | _, file, line, ok := runtime.Caller(i) | 420 | } else { |
| 422 | if !ok { | 421 | var stack string |
| 423 | break | 422 | Critical("Handler crashed with error", err) |
| 423 | for i := 1; ; i++ { | ||
| 424 | _, file, line, ok := runtime.Caller(i) | ||
| 425 | if !ok { | ||
| 426 | break | ||
| 427 | } | ||
| 428 | Critical(file, line) | ||
| 429 | if RunMode == "dev" { | ||
| 430 | stack = stack + fmt.Sprintln(file, line) | ||
| 431 | } | ||
| 424 | } | 432 | } |
| 425 | Critical(file, line) | ||
| 426 | if RunMode == "dev" { | 433 | if RunMode == "dev" { |
| 427 | stack = stack + fmt.Sprintln(file, line) | 434 | middleware.ShowErr(err, rw, r, stack) |
| 428 | } | 435 | } |
| 429 | } | 436 | } |
| 430 | if RunMode == "dev" { | ||
| 431 | middleware.ShowErr(err, rw, r, stack) | ||
| 432 | } | ||
| 433 | } | 437 | } |
| 434 | } | 438 | } |
| 435 | } | 439 | } | ... | ... |
-
Please register or sign in to post a comment