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
8cf34fce
Commit
8cf34fce
authored
Dec 06, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #344 from vadimi/master
Improve unhandled error handling in prod mode
parents
de70732c
d7997797
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
7 deletions
+29
-7
error.go
middleware/error.go
+9
-5
router.go
router.go
+20
-2
No files found.
middleware/error.go
View file @
8cf34fce
...
@@ -13,8 +13,8 @@ var (
...
@@ -13,8 +13,8 @@ var (
VERSION
string
VERSION
string
)
)
var
tpl
=
`
var
tpl
=
`
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<html>
<head>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>beego application error</title>
<title>beego application error</title>
...
@@ -28,10 +28,10 @@ var tpl = `
...
@@ -28,10 +28,10 @@ var tpl = `
#content .stack pre{padding-left: 10px;}
#content .stack pre{padding-left: 10px;}
table {}
table {}
td.t {text-align: right; padding-right: 5px; color: #888;}
td.t {text-align: right; padding-right: 5px; color: #888;}
</style>
</style>
<script type="text/javascript">
<script type="text/javascript">
</script>
</script>
</head>
</head>
<body>
<body>
<div id="header">
<div id="header">
<h2>{{.AppError}}</h2>
<h2>{{.AppError}}</h2>
...
@@ -58,7 +58,7 @@ var tpl = `
...
@@ -58,7 +58,7 @@ var tpl = `
<p>golang version: {{.GoVersion}}</p>
<p>golang version: {{.GoVersion}}</p>
</div>
</div>
</body>
</body>
</html>
</html>
`
`
func
ShowErr
(
err
interface
{},
rw
http
.
ResponseWriter
,
r
*
http
.
Request
,
Stack
string
)
{
func
ShowErr
(
err
interface
{},
rw
http
.
ResponseWriter
,
r
*
http
.
Request
,
Stack
string
)
{
...
@@ -262,6 +262,10 @@ func InternalServerError(rw http.ResponseWriter, r *http.Request) {
...
@@ -262,6 +262,10 @@ func InternalServerError(rw http.ResponseWriter, r *http.Request) {
t
.
Execute
(
rw
,
data
)
t
.
Execute
(
rw
,
data
)
}
}
func
SimpleServerError
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
http
.
Error
(
rw
,
http
.
StatusText
(
http
.
StatusInternalServerError
),
http
.
StatusInternalServerError
)
}
func
Errorhandler
(
err
string
,
h
http
.
HandlerFunc
)
{
func
Errorhandler
(
err
string
,
h
http
.
HandlerFunc
)
{
ErrorMaps
[
err
]
=
h
ErrorMaps
[
err
]
=
h
}
}
...
...
router.go
View file @
8cf34fce
...
@@ -368,8 +368,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
...
@@ -368,8 +368,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if
_
,
ok
:=
err
.
(
middleware
.
HTTPException
);
ok
{
if
_
,
ok
:=
err
.
(
middleware
.
HTTPException
);
ok
{
// catch intented errors, only for HTTP 4XX and 5XX
// catch intented errors, only for HTTP 4XX and 5XX
}
else
{
}
else
{
errstr
:=
fmt
.
Sprint
(
err
)
if
ErrorsShow
{
if
handler
,
ok
:=
middleware
.
ErrorMaps
[
errstr
];
ok
&&
ErrorsShow
{
handler
:=
p
.
getErrorHandler
(
fmt
.
Sprint
(
err
))
handler
(
rw
,
r
)
handler
(
rw
,
r
)
}
else
{
}
else
{
if
!
RecoverPanic
{
if
!
RecoverPanic
{
...
@@ -865,6 +865,24 @@ Admin:
...
@@ -865,6 +865,24 @@ Admin:
}
}
}
}
// there always should be error handler that sets error code accordingly for all unhandled errors
// in order to have custom UI for error page it's necessary to override "500" error
func
(
p
*
ControllerRegistor
)
getErrorHandler
(
errorCode
string
)
func
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
handler
:=
middleware
.
SimpleServerError
ok
:=
true
if
errorCode
!=
""
{
handler
,
ok
=
middleware
.
ErrorMaps
[
errorCode
]
if
!
ok
{
handler
,
ok
=
middleware
.
ErrorMaps
[
"500"
]
}
if
!
ok
||
handler
==
nil
{
handler
=
middleware
.
SimpleServerError
}
}
return
handler
}
//responseWriter is a wrapper for the http.ResponseWriter
//responseWriter is a wrapper for the http.ResponseWriter
//started set to true if response was written to then don't execute other handler
//started set to true if response was written to then don't execute other handler
type
responseWriter
struct
{
type
responseWriter
struct
{
...
...
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