76c06361 by Pengfei Xue

beego.Context.Abort return immediately

* add common 4XX/5XX HTTP exceptions
1 parent c7a02985
...@@ -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) {
......
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,6 +407,9 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { ...@@ -407,6 +407,9 @@ 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 if _, ok := err.(middleware.HTTPException); ok {
411 // catch intented errors, only for HTTP 4XX and 5XX
412 } else {
410 errstr := fmt.Sprint(err) 413 errstr := fmt.Sprint(err)
411 if handler, ok := middleware.ErrorMaps[errstr]; ok && ErrorsShow { 414 if handler, ok := middleware.ErrorMaps[errstr]; ok && ErrorsShow {
412 handler(rw, r) 415 handler(rw, r)
...@@ -433,6 +436,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -433,6 +436,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
433 } 436 }
434 } 437 }
435 } 438 }
439 }
436 }() 440 }()
437 441
438 starttime := time.Now() 442 starttime := time.Now()
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!