Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
76c06361
Commit
76c06361
authored
Nov 26, 2013
by
Pengfei Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
beego.Context.Abort return immediately
* add common 4XX/5XX HTTP exceptions
parent
c7a02985
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
0 deletions
+45
-0
context.go
context/context.go
+9
-0
exceptions.go
middleware/exceptions.go
+32
-0
router.go
router.go
+4
-0
No files found.
context/context.go
View file @
76c06361
...
...
@@ -2,6 +2,8 @@ package context
import
(
"net/http"
"github.com/astaxie/beego/middleware"
)
type
Context
struct
{
...
...
@@ -19,6 +21,13 @@ func (ctx *Context) Redirect(status int, localurl string) {
func
(
ctx
*
Context
)
Abort
(
status
int
,
body
string
)
{
ctx
.
Output
.
SetStatus
(
status
)
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
)
{
...
...
middleware/exceptions.go
0 → 100644
View file @
76c06361
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"
}
}
router.go
View file @
76c06361
...
...
@@ -407,6 +407,9 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {
func
(
p
*
ControllerRegistor
)
ServeHTTP
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
defer
func
()
{
if
err
:=
recover
();
err
!=
nil
{
if
_
,
ok
:=
err
.
(
middleware
.
HTTPException
);
ok
{
// catch intented errors, only for HTTP 4XX and 5XX
}
else
{
errstr
:=
fmt
.
Sprint
(
err
)
if
handler
,
ok
:=
middleware
.
ErrorMaps
[
errstr
];
ok
&&
ErrorsShow
{
handler
(
rw
,
r
)
...
...
@@ -433,6 +436,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
}
}
}
}()
starttime
:=
time
.
Now
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment