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
32799bc2
Commit
32799bc2
authored
Jan 17, 2014
by
傅小黑
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add comments for middleware packages, fix typo error
parent
91d75e89
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
8 deletions
+18
-8
beego.go
beego.go
+1
-1
error.go
middleware/error.go
+13
-6
exceptions.go
middleware/exceptions.go
+4
-1
No files found.
beego.go
View file @
32799bc2
...
...
@@ -221,7 +221,7 @@ func Run() {
middleware
.
VERSION
=
VERSION
middleware
.
AppName
=
AppName
middleware
.
RegisterErrorHander
()
middleware
.
RegisterErrorHand
l
er
()
if
EnableAdmin
{
go
BeeAdminApp
.
Run
()
...
...
middleware/error.go
View file @
32799bc2
...
...
@@ -61,6 +61,7 @@ var tpl = `
</html>
`
// render default application error page with error and stack string.
func
ShowErr
(
err
interface
{},
rw
http
.
ResponseWriter
,
r
*
http
.
Request
,
Stack
string
)
{
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
tpl
)
data
:=
make
(
map
[
string
]
string
)
...
...
@@ -175,13 +176,14 @@ var errtpl = `
</html>
`
// map of http handlers for each error string.
var
ErrorMaps
map
[
string
]
http
.
HandlerFunc
func
init
()
{
ErrorMaps
=
make
(
map
[
string
]
http
.
HandlerFunc
)
}
//
404
//
show 404 notfound error.
func
NotFound
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
errtpl
)
data
:=
make
(
map
[
string
]
interface
{})
...
...
@@ -199,7 +201,7 @@ func NotFound(rw http.ResponseWriter, r *http.Request) {
t
.
Execute
(
rw
,
data
)
}
//
401
//
show 401 unauthorized error.
func
Unauthorized
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
errtpl
)
data
:=
make
(
map
[
string
]
interface
{})
...
...
@@ -215,7 +217,7 @@ func Unauthorized(rw http.ResponseWriter, r *http.Request) {
t
.
Execute
(
rw
,
data
)
}
//
403
//
show 403 forbidden error.
func
Forbidden
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
errtpl
)
data
:=
make
(
map
[
string
]
interface
{})
...
...
@@ -232,7 +234,7 @@ func Forbidden(rw http.ResponseWriter, r *http.Request) {
t
.
Execute
(
rw
,
data
)
}
//
503
//
show 503 service unavailable error.
func
ServiceUnavailable
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
errtpl
)
data
:=
make
(
map
[
string
]
interface
{})
...
...
@@ -248,7 +250,7 @@ func ServiceUnavailable(rw http.ResponseWriter, r *http.Request) {
t
.
Execute
(
rw
,
data
)
}
//
500
//
show 500 internal server error.
func
InternalServerError
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
errtpl
)
data
:=
make
(
map
[
string
]
interface
{})
...
...
@@ -262,15 +264,18 @@ func InternalServerError(rw http.ResponseWriter, r *http.Request) {
t
.
Execute
(
rw
,
data
)
}
// show 500 internal error with simple text string.
func
SimpleServerError
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
http
.
Error
(
rw
,
http
.
StatusText
(
http
.
StatusInternalServerError
),
http
.
StatusInternalServerError
)
}
// add http handler for given error string.
func
Errorhandler
(
err
string
,
h
http
.
HandlerFunc
)
{
ErrorMaps
[
err
]
=
h
}
func
RegisterErrorHander
()
{
// register default error http handlers, 404,401,403,500 and 503.
func
RegisterErrorHandler
()
{
if
_
,
ok
:=
ErrorMaps
[
"404"
];
!
ok
{
ErrorMaps
[
"404"
]
=
NotFound
}
...
...
@@ -292,6 +297,8 @@ func RegisterErrorHander() {
}
}
// show error string as simple text message.
// if error string is empty, show 500 error as default.
func
Exception
(
errcode
string
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
msg
string
)
{
if
h
,
ok
:=
ErrorMaps
[
errcode
];
ok
{
isint
,
err
:=
strconv
.
Atoi
(
errcode
)
...
...
middleware/exceptions.go
View file @
32799bc2
...
...
@@ -2,16 +2,19 @@ package middleware
import
"fmt"
// http exceptions
type
HTTPException
struct
{
StatusCode
int
// http status code 4xx, 5xx
Description
string
}
// return http exception error string, e.g. "400 Bad Request".
func
(
e
*
HTTPException
)
Error
()
string
{
// return `status description`, e.g. `400 Bad Request`
return
fmt
.
Sprintf
(
"%d %s"
,
e
.
StatusCode
,
e
.
Description
)
}
// map of http exceptions for each http status code int.
// defined 400,401,403,404,405,500,502,503 and 504 default.
var
HTTPExceptionMaps
map
[
int
]
HTTPException
func
init
()
{
...
...
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