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
1e7c1a26
authored
2013-05-07 00:17:25 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
fix #16
1 parent
93babc57
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
277 additions
and
2 deletions
beego.go
controller.go
docs/zh/Quickstart.md
errors.go
router.go
beego.go
View file @
1e7c1a2
...
...
@@ -148,6 +148,11 @@ func RouterHandler(path string, c http.Handler) *App {
return
BeeApp
}
func
Errorhandler
(
err
string
,
h
http
.
HandlerFunc
)
*
App
{
ErrorMaps
[
err
]
=
h
return
BeeApp
}
func
SetViewsPath
(
path
string
)
*
App
{
BeeApp
.
SetViewsPath
(
path
)
return
BeeApp
...
...
@@ -195,5 +200,6 @@ func Run() {
}
}
runtime
.
GOMAXPROCS
(
runtime
.
NumCPU
())
registerErrorHander
()
BeeApp
.
Run
()
}
...
...
controller.go
View file @
1e7c1a2
...
...
@@ -192,6 +192,10 @@ func (c *Controller) Redirect(url string, code int) {
c
.
Ctx
.
Redirect
(
code
,
url
)
}
func
(
c
*
Controller
)
Abort
(
code
string
)
{
panic
(
code
)
}
func
(
c
*
Controller
)
ServeJson
()
{
content
,
err
:=
json
.
MarshalIndent
(
c
.
Data
[
"json"
],
""
,
" "
)
if
err
!=
nil
{
...
...
docs/zh/Quickstart.md
View file @
1e7c1a2
...
...
@@ -482,7 +482,58 @@ XML数据直接输出,设置`content-type`为`application/xml`:
this.Redirect("/", 302)
}
@todo 错误处理还需要后期改进
如何中止此次请求并抛出异常,beego可以在控制器中这操作
func (this *MainController) Get() {
this.Abort("401")
v := this.GetSession("asta")
if v == nil {
this.SetSession("asta", int(1))
this.Data["Email"] = 0
} else {
this.SetSession("asta", v.(int)+1)
this.Data["Email"] = v.(int)
}
this.TplNames = "index.tpl"
}
这样
`this.Abort("401")`
之后的代码不会再执行,而且会默认显示给用户如下页面
!
[
](images/401.png)
beego框架默认支持404、401、403、500、503这几种错误的处理。用户可以自定义相应的错误处理,例如下面重新定义404页面:
func page_not_found(rw http.ResponseWriter, r *http.Request){
t:= template.New("beegoerrortemp").ParseFiles(beego.ViewsPath+"404.html")
data :=make(map[string]interface{})
data["content"] = "page not found"
t.Execute(rw, data)
}
func main() {
beego.Errorhandler("404",PageNotFound)
beego.Router("/", &controllers.MainController{})
beego.Run()
}
我们可以通过自定义错误页面
`404.html`
来处理404错误。
beego更加人性化的还有一个设计就是支持用户自定义字符串错误类型处理函数,例如下面的代码,用户注册了一个数据库出错的处理页面:
func dbError(rw http.ResponseWriter, r *http.Request){
t:= template.New("beegoerrortemp").ParseFiles(beego.ViewsPath+"dberror.html")
data :=make(map[string]interface{})
data["content"] = "database is now down"
t.Execute(rw, data)
}
func main() {
beego.Errorhandler("dbError",dbError)
beego.Router("/", &controllers.MainController{})
beego.Run()
}
一旦在入口注册该错误处理代码,那么你可以在任何你的逻辑中遇到数据库错误调用
`this.Abort("dbError")`
来进行异常页面处理。
## response处理
response可能会有集中情况:
...
...
errors.go
View file @
1e7c1a2
...
...
@@ -57,7 +57,7 @@ var tpl = `
`
func
ShowErr
(
err
interface
{},
rw
http
.
ResponseWriter
,
r
*
http
.
Request
,
Stack
string
)
{
t
,
err
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
tpl
)
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
tpl
)
data
:=
make
(
map
[
string
]
string
)
data
[
"AppError"
]
=
AppName
+
":"
+
fmt
.
Sprint
(
err
)
data
[
"RequestMethod"
]
=
r
.
Method
...
...
@@ -68,3 +68,208 @@ func ShowErr(err interface{}, rw http.ResponseWriter, r *http.Request, Stack str
data
[
"GoVersion"
]
=
runtime
.
Version
()
t
.
Execute
(
rw
,
data
)
}
var
errtpl
=
`
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Page Not Found</title>
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background-color:#EFEFEF;
font: .9em "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
#wrapper{
width:600px;
margin:40px auto 0;
text-align:center;
-moz-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
-webkit-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
}
#wrapper h1{
color:#FFF;
text-align:center;
margin-bottom:20px;
}
#wrapper a{
display:block;
font-size:.9em;
padding-top:20px;
color:#FFF;
text-decoration:none;
text-align:center;
}
#container {
width:600px;
padding-bottom:15px;
background-color:#FFFFFF;
}
.navtop{
height:40px;
background-color:#24B2EB;
padding:13px;
}
.content {
padding:10px 10px 25px;
background: #FFFFFF;
margin:;
color:#333;
}
a.button{
color:white;
padding:15px 20px;
text-shadow:1px 1px 0 #00A5FF;
font-weight:bold;
text-align:center;
border:1px solid #24B2EB;
margin:0px 200px;
clear:both;
background-color: #24B2EB;
border-radius:100px;
-moz-border-radius:100px;
-webkit-border-radius:100px;
}
a.button:hover{
text-decoration:none;
background-color: #24B2EB;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="container">
<div class="navtop">
<h1>{{.Title}}</h1>
</div>
<div id="content">
{{.Content}}
<a href="/" title="Home" class="button">Go Home</a><br />
<br>power by beego {{.BeegoVersion}}
</div>
</div>
</div>
</body>
</html>
`
var
ErrorMaps
map
[
string
]
http
.
HandlerFunc
func
init
()
{
ErrorMaps
=
make
(
map
[
string
]
http
.
HandlerFunc
)
}
//404
func
NotFound
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
errtpl
)
data
:=
make
(
map
[
string
]
interface
{})
data
[
"Title"
]
=
"Page Not Found"
data
[
"Content"
]
=
template
.
HTML
(
"<br>The Page You have requested flown the coop."
+
"<br>Perhaps you are here because:"
+
"<br><br><ul>"
+
"<br>The page has moved"
+
"<br>The page no longer exists"
+
"<br>You were looking for your puppy and got lost"
+
"<br>You like 404 pages"
+
"</ul>"
)
data
[
"BeegoVersion"
]
=
VERSION
t
.
Execute
(
rw
,
data
)
}
//401
func
Unauthorized
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
errtpl
)
data
:=
make
(
map
[
string
]
interface
{})
data
[
"Title"
]
=
"Unauthorized"
data
[
"Content"
]
=
template
.
HTML
(
"<br>The Page You have requested can't authorized."
+
"<br>Perhaps you are here because:"
+
"<br><br><ul>"
+
"<br>Check the credentials that you supplied"
+
"<br>Check the address for errors"
+
"</ul>"
)
data
[
"BeegoVersion"
]
=
VERSION
t
.
Execute
(
rw
,
data
)
}
//403
func
Forbidden
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
errtpl
)
data
:=
make
(
map
[
string
]
interface
{})
data
[
"Title"
]
=
"Forbidden"
data
[
"Content"
]
=
template
.
HTML
(
"<br>The Page You have requested forbidden."
+
"<br>Perhaps you are here because:"
+
"<br><br><ul>"
+
"<br>Your address may be blocked"
+
"<br>The site may be disabled"
+
"<br>You need to log in"
+
"</ul>"
)
data
[
"BeegoVersion"
]
=
VERSION
t
.
Execute
(
rw
,
data
)
}
//503
func
ServiceUnavailable
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
errtpl
)
data
:=
make
(
map
[
string
]
interface
{})
data
[
"Title"
]
=
"Service Unavailable"
data
[
"Content"
]
=
template
.
HTML
(
"<br>The Page You have requested unavailable."
+
"<br>Perhaps you are here because:"
+
"<br><br><ul>"
+
"<br><br>The page is overloaded"
+
"<br>Please try again later."
+
"</ul>"
)
data
[
"BeegoVersion"
]
=
VERSION
t
.
Execute
(
rw
,
data
)
}
//500
func
InternalServerError
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
t
,
_
:=
template
.
New
(
"beegoerrortemp"
)
.
Parse
(
errtpl
)
data
:=
make
(
map
[
string
]
interface
{})
data
[
"Title"
]
=
"Internal Server Error"
data
[
"Content"
]
=
template
.
HTML
(
"<br>The Page You have requested has down now."
+
"<br><br><ul>"
+
"<br>simply try again later"
+
"<br>you should report the fault to the website administrator"
+
"</ul>"
)
data
[
"BeegoVersion"
]
=
VERSION
t
.
Execute
(
rw
,
data
)
}
func
registerErrorHander
()
{
if
_
,
ok
:=
ErrorMaps
[
"404"
];
!
ok
{
ErrorMaps
[
"404"
]
=
NotFound
}
if
_
,
ok
:=
ErrorMaps
[
"401"
];
!
ok
{
ErrorMaps
[
"401"
]
=
Unauthorized
}
if
_
,
ok
:=
ErrorMaps
[
"403"
];
!
ok
{
ErrorMaps
[
"403"
]
=
Forbidden
}
if
_
,
ok
:=
ErrorMaps
[
"503"
];
!
ok
{
ErrorMaps
[
"503"
]
=
ServiceUnavailable
}
if
_
,
ok
:=
ErrorMaps
[
"500"
];
!
ok
{
ErrorMaps
[
"500"
]
=
InternalServerError
}
}
...
...
router.go
View file @
1e7c1a2
...
...
@@ -187,6 +187,10 @@ func (p *ControllerRegistor) FilterPrefixPath(path string, filter http.HandlerFu
func
(
p
*
ControllerRegistor
)
ServeHTTP
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
defer
func
()
{
if
err
:=
recover
();
err
!=
nil
{
errstr
:=
fmt
.
Sprint
(
err
)
if
handler
,
ok
:=
ErrorMaps
[
errstr
];
ok
{
handler
(
rw
,
r
)
}
else
{
if
!
RecoverPanic
{
// go back to panic
panic
(
err
)
...
...
@@ -208,6 +212,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
}
}
}
}()
w
:=
&
responseWriter
{
writer
:
rw
}
...
...
@@ -385,8 +390,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
//if no matches to url, throw a not found exception
if
w
.
started
==
false
{
if
h
,
ok
:=
ErrorMaps
[
"404"
];
ok
{
h
(
w
,
r
)
}
else
{
http
.
NotFound
(
w
,
r
)
}
}
}
//responseWriter is a wrapper for the http.ResponseWriter
...
...
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