Skip to content

Commit 6586190

Browse files
thehowllafriks
authored andcommitted
Handle refactor (#3339)
* Replace all ctx.Handle with ctx.ServerError or ctx.NotFound * Change Handle(403) to NotFound, avoid using macaron's NotFound
1 parent 45c264f commit 6586190

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+622
-610
lines changed

modules/context/context.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package context
66

77
import (
8-
"fmt"
98
"html"
109
"html/template"
1110
"io"
@@ -92,34 +91,43 @@ func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, form interface{}
9291
ctx.HTML(200, tpl)
9392
}
9493

95-
// Handle handles and logs error by given status.
96-
func (ctx *Context) Handle(status int, title string, err error) {
94+
// NotFound displays a 404 (Not Found) page and prints the given error, if any.
95+
func (ctx *Context) NotFound(title string, err error) {
9796
if err != nil {
9897
log.Error(4, "%s: %v", title, err)
9998
if macaron.Env != macaron.PROD {
10099
ctx.Data["ErrorMsg"] = err
101100
}
102101
}
103102

104-
switch status {
105-
case 404:
106-
ctx.Data["Title"] = "Page Not Found"
107-
case 500:
108-
ctx.Data["Title"] = "Internal Server Error"
103+
ctx.Data["Title"] = "Page Not Found"
104+
ctx.HTML(http.StatusNotFound, base.TplName("status/404"))
105+
}
106+
107+
// ServerError displays a 500 (Internal Server Error) page and prints the given
108+
// error, if any.
109+
func (ctx *Context) ServerError(title string, err error) {
110+
if err != nil {
111+
log.Error(4, "%s: %v", title, err)
112+
if macaron.Env != macaron.PROD {
113+
ctx.Data["ErrorMsg"] = err
114+
}
109115
}
110-
ctx.HTML(status, base.TplName(fmt.Sprintf("status/%d", status)))
116+
117+
ctx.Data["Title"] = "Internal Server Error"
118+
ctx.HTML(404, base.TplName("status/500"))
111119
}
112120

113121
// NotFoundOrServerError use error check function to determine if the error
114122
// is about not found. It responses with 404 status code for not found error,
115123
// or error context description for logging purpose of 500 server error.
116124
func (ctx *Context) NotFoundOrServerError(title string, errck func(error) bool, err error) {
117125
if errck(err) {
118-
ctx.Handle(404, title, err)
126+
ctx.NotFound(title, err)
119127
return
120128
}
121129

122-
ctx.Handle(500, title, err)
130+
ctx.ServerError(title, err)
123131
}
124132

125133
// HandleText handles HTTP status code
@@ -218,7 +226,7 @@ func Contexter() macaron.Handler {
218226
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
219227
if ctx.Req.Method == "POST" && strings.Contains(ctx.Req.Header.Get("Content-Type"), "multipart/form-data") {
220228
if err := ctx.Req.ParseMultipartForm(setting.AttachmentMaxSize << 20); err != nil && !strings.Contains(err.Error(), "EOF") { // 32MB max size
221-
ctx.Handle(500, "ParseMultipartForm", err)
229+
ctx.ServerError("ParseMultipartForm", err)
222230
return
223231
}
224232
}

modules/context/org.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
5151
ctx.Org.Organization, err = models.GetUserByName(orgName)
5252
if err != nil {
5353
if models.IsErrUserNotExist(err) {
54-
ctx.Handle(404, "GetUserByName", err)
54+
ctx.NotFound("GetUserByName", err)
5555
} else {
56-
ctx.Handle(500, "GetUserByName", err)
56+
ctx.ServerError("GetUserByName", err)
5757
}
5858
return
5959
}
@@ -75,7 +75,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
7575
} else if ctx.IsSigned {
7676
ctx.Org.IsOwner, err = org.IsOwnedBy(ctx.User.ID)
7777
if err != nil {
78-
ctx.Handle(500, "IsOwnedBy", err)
78+
ctx.ServerError("IsOwnedBy", err)
7979
return
8080
}
8181

@@ -86,7 +86,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
8686
} else {
8787
ctx.Org.IsMember, err = org.IsOrgMember(ctx.User.ID)
8888
if err != nil {
89-
ctx.Handle(500, "IsOrgMember", err)
89+
ctx.ServerError("IsOrgMember", err)
9090
return
9191
}
9292
}
@@ -96,7 +96,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
9696
}
9797
if (requireMember && !ctx.Org.IsMember) ||
9898
(requireOwner && !ctx.Org.IsOwner) {
99-
ctx.Handle(404, "OrgAssignment", err)
99+
ctx.NotFound("OrgAssignment", err)
100100
return
101101
}
102102
ctx.Data["IsOrganizationOwner"] = ctx.Org.IsOwner
@@ -109,13 +109,13 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
109109
if ctx.Org.IsMember {
110110
if ctx.Org.IsOwner {
111111
if err := org.GetTeams(); err != nil {
112-
ctx.Handle(500, "GetTeams", err)
112+
ctx.ServerError("GetTeams", err)
113113
return
114114
}
115115
} else {
116116
org.Teams, err = org.GetUserTeams(ctx.User.ID)
117117
if err != nil {
118-
ctx.Handle(500, "GetUserTeams", err)
118+
ctx.ServerError("GetUserTeams", err)
119119
return
120120
}
121121
}
@@ -135,20 +135,20 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
135135
}
136136

137137
if !teamExists {
138-
ctx.Handle(404, "OrgAssignment", err)
138+
ctx.NotFound("OrgAssignment", err)
139139
return
140140
}
141141

142142
ctx.Data["IsTeamMember"] = ctx.Org.IsTeamMember
143143
if requireTeamMember && !ctx.Org.IsTeamMember {
144-
ctx.Handle(404, "OrgAssignment", err)
144+
ctx.NotFound("OrgAssignment", err)
145145
return
146146
}
147147

148148
ctx.Org.IsTeamAdmin = ctx.Org.Team.IsOwnerTeam() || ctx.Org.Team.Authorize >= models.AccessModeAdmin
149149
ctx.Data["IsTeamAdmin"] = ctx.Org.IsTeamAdmin
150150
if requireTeamAdmin && !ctx.Org.IsTeamAdmin {
151-
ctx.Handle(404, "OrgAssignment", err)
151+
ctx.NotFound("OrgAssignment", err)
152152
return
153153
}
154154
}

0 commit comments

Comments
 (0)