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
0d100fef
Commit
0d100fef
authored
Sep 17, 2015
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1364 from JessonChan/fargo
error and hook refactor
parents
eac09e6f
de209604
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
55 deletions
+32
-55
error.go
error.go
+15
-20
hooks.go
hooks.go
+17
-35
No files found.
error.go
View file @
0d100fef
...
...
@@ -204,11 +204,7 @@ type errorInfo struct {
}
// map of http handlers for each error string.
var
ErrorMaps
map
[
string
]
*
errorInfo
func
init
()
{
ErrorMaps
=
make
(
map
[
string
]
*
errorInfo
)
}
var
ErrorMaps
map
[
string
]
*
errorInfo
=
make
(
map
[
string
]
*
errorInfo
,
10
)
// show 401 unauthorized error.
func
unauthorized
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
...
@@ -392,22 +388,21 @@ func ErrorController(c ControllerInterface) *App {
}
// show error string as simple text message.
// if error string is empty, show 500 error as default.
func
exception
(
errcode
string
,
ctx
*
context
.
Context
)
{
code
,
err
:=
strconv
.
Atoi
(
errcode
)
if
err
!=
nil
{
code
=
503
}
if
h
,
ok
:=
ErrorMaps
[
errcode
];
ok
{
executeError
(
h
,
ctx
,
code
)
return
}
else
if
h
,
ok
:=
ErrorMaps
[
"503"
];
ok
{
executeError
(
h
,
ctx
,
code
)
return
}
else
{
ctx
.
ResponseWriter
.
WriteHeader
(
code
)
ctx
.
WriteString
(
errcode
)
// if error string is empty, show 503 or 500 error as default.
func
exception
(
errCode
string
,
ctx
*
context
.
Context
)
{
for
ec
,
_
:=
range
[]
string
{
errCode
,
"503"
,
"500"
}
{
code
,
_
:=
strconv
.
Atoi
(
errCode
)
if
code
==
0
{
code
=
503
}
if
h
,
ok
:=
ErrorMaps
[
ec
];
ok
{
executeError
(
h
,
ctx
,
code
)
return
}
}
//if 50x error has been removed from errorMap
ctx
.
ResponseWriter
.
WriteHeader
(
503
)
ctx
.
WriteString
(
errCode
)
}
func
executeError
(
err
*
errorInfo
,
ctx
*
context
.
Context
,
code
int
)
{
...
...
hooks.go
View file @
0d100fef
...
...
@@ -5,6 +5,8 @@ import (
"path/filepath"
"strconv"
"net/http"
"github.com/astaxie/beego/session"
)
...
...
@@ -18,42 +20,22 @@ func registerMime() error {
// register default error http handlers, 404,401,403,500 and 503.
func
registerDefaultErrorHandler
()
error
{
if
_
,
ok
:=
ErrorMaps
[
"401"
];
!
ok
{
ErrorHandler
(
"401"
,
unauthorized
)
}
if
_
,
ok
:=
ErrorMaps
[
"402"
];
!
ok
{
ErrorHandler
(
"402"
,
paymentRequired
)
}
if
_
,
ok
:=
ErrorMaps
[
"403"
];
!
ok
{
ErrorHandler
(
"403"
,
forbidden
)
}
if
_
,
ok
:=
ErrorMaps
[
"404"
];
!
ok
{
ErrorHandler
(
"404"
,
notFound
)
}
if
_
,
ok
:=
ErrorMaps
[
"405"
];
!
ok
{
ErrorHandler
(
"405"
,
methodNotAllowed
)
}
if
_
,
ok
:=
ErrorMaps
[
"500"
];
!
ok
{
ErrorHandler
(
"500"
,
internalServerError
)
}
if
_
,
ok
:=
ErrorMaps
[
"501"
];
!
ok
{
ErrorHandler
(
"501"
,
notImplemented
)
}
if
_
,
ok
:=
ErrorMaps
[
"502"
];
!
ok
{
ErrorHandler
(
"502"
,
badGateway
)
}
if
_
,
ok
:=
ErrorMaps
[
"503"
];
!
ok
{
ErrorHandler
(
"503"
,
serviceUnavailable
)
}
if
_
,
ok
:=
ErrorMaps
[
"504"
];
!
ok
{
ErrorHandler
(
"504"
,
gatewayTimeout
)
for
e
,
h
:=
range
map
[
string
]
func
(
http
.
ResponseWriter
,
*
http
.
Request
){
"401"
:
unauthorized
,
"402"
:
paymentRequired
,
"403"
:
forbidden
,
"404"
:
notFound
,
"405"
:
methodNotAllowed
,
"500"
:
internalServerError
,
"501"
:
notImplemented
,
"502"
:
badGateway
,
"503"
:
serviceUnavailable
,
"504"
:
gatewayTimeout
,
}
{
if
_
,
ok
:=
ErrorMaps
[
e
];
!
ok
{
ErrorHandler
(
e
,
h
)
}
}
return
nil
}
...
...
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