Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
e09e642a
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e09e642a
编写于
11月 25, 2013
作者:
A
astaxie
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #317 from pengfei-xue/master
beego.Context.Abort return immediately
上级
c7a02985
76c06361
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
62 addition
and
17 deletion
+62
-17
context/context.go
context/context.go
+9
-0
middleware/exceptions.go
middleware/exceptions.go
+32
-0
router.go
router.go
+21
-17
未找到文件。
context/context.go
浏览文件 @
e09e642a
...
...
@@ -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
浏览文件 @
e09e642a
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
浏览文件 @
e09e642a
...
...
@@ -407,29 +407,33 @@ 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
{
errstr
:=
fmt
.
Sprint
(
err
)
if
handler
,
ok
:=
middleware
.
ErrorMaps
[
errstr
];
ok
&&
ErrorsShow
{
handler
(
rw
,
r
)
if
_
,
ok
:=
err
.
(
middleware
.
HTTPException
);
ok
{
// catch intented errors, only for HTTP 4XX and 5XX
}
else
{
if
!
RecoverPanic
{
// go back to panic
panic
(
er
r
)
errstr
:=
fmt
.
Sprint
(
err
)
if
handler
,
ok
:=
middleware
.
ErrorMaps
[
errstr
];
ok
&&
ErrorsShow
{
handler
(
rw
,
r
)
}
else
{
var
stack
string
Critical
(
"Handler crashed with error"
,
err
)
for
i
:=
1
;
;
i
++
{
_
,
file
,
line
,
ok
:=
runtime
.
Caller
(
i
)
if
!
ok
{
break
if
!
RecoverPanic
{
// go back to panic
panic
(
err
)
}
else
{
var
stack
string
Critical
(
"Handler crashed with error"
,
err
)
for
i
:=
1
;
;
i
++
{
_
,
file
,
line
,
ok
:=
runtime
.
Caller
(
i
)
if
!
ok
{
break
}
Critical
(
file
,
line
)
if
RunMode
==
"dev"
{
stack
=
stack
+
fmt
.
Sprintln
(
file
,
line
)
}
}
Critical
(
file
,
line
)
if
RunMode
==
"dev"
{
stack
=
stack
+
fmt
.
Sprintln
(
file
,
line
)
middleware
.
ShowErr
(
err
,
rw
,
r
,
stack
)
}
}
if
RunMode
==
"dev"
{
middleware
.
ShowErr
(
err
,
rw
,
r
,
stack
)
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录