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
8cf34fce
authored
2013-12-05 21:55:37 -0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
Merge pull request #344 from vadimi/master
Improve unhandled error handling in prod mode
2 parents
de70732c
d7997797
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
2 deletions
middleware/error.go
router.go
middleware/error.go
View file @
8cf34fc
...
...
@@ -262,6 +262,10 @@ func InternalServerError(rw http.ResponseWriter, r *http.Request) {
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
)
{
ErrorMaps
[
err
]
=
h
}
...
...
router.go
View file @
8cf34fc
...
...
@@ -368,8 +368,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if
_
,
ok
:=
err
.
(
middleware
.
HTTPException
);
ok
{
// catch intented errors, only for HTTP 4XX and 5XX
}
else
{
errstr
:=
fmt
.
Sprint
(
err
)
if
handler
,
ok
:=
middleware
.
ErrorMaps
[
errstr
];
ok
&&
ErrorsShow
{
if
ErrorsShow
{
handler
:=
p
.
getErrorHandler
(
fmt
.
Sprint
(
err
))
handler
(
rw
,
r
)
}
else
{
if
!
RecoverPanic
{
...
...
@@ -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
//started set to true if response was written to then don't execute other handler
type
responseWriter
struct
{
...
...
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