Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
32799bc2
authored
2014-01-17 16:03:01 +0800
by
傅小黑
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
add comments for middleware packages, fix typo error
1 parent
91d75e89
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
8 deletions
beego.go
middleware/error.go
middleware/exceptions.go
beego.go
View file @
32799bc
...
...
@@ -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 @
32799bc
...
...
@@ -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 @
32799bc
...
...
@@ -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
Styling with
Markdown
is supported
Attach a file
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 post a comment