Handle refactor (#3339)
* Replace all ctx.Handle with ctx.ServerError or ctx.NotFound * Change Handle(403) to NotFound, avoid using macaron's NotFound
This commit is contained in:
parent
45c264f681
commit
65861900cd
48 changed files with 622 additions and 610 deletions
|
@ -5,7 +5,6 @@
|
|||
package context
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"html/template"
|
||||
"io"
|
||||
|
@ -92,8 +91,8 @@ func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, form interface{}
|
|||
ctx.HTML(200, tpl)
|
||||
}
|
||||
|
||||
// Handle handles and logs error by given status.
|
||||
func (ctx *Context) Handle(status int, title string, err error) {
|
||||
// NotFound displays a 404 (Not Found) page and prints the given error, if any.
|
||||
func (ctx *Context) NotFound(title string, err error) {
|
||||
if err != nil {
|
||||
log.Error(4, "%s: %v", title, err)
|
||||
if macaron.Env != macaron.PROD {
|
||||
|
@ -101,13 +100,22 @@ func (ctx *Context) Handle(status int, title string, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
switch status {
|
||||
case 404:
|
||||
ctx.Data["Title"] = "Page Not Found"
|
||||
case 500:
|
||||
ctx.Data["Title"] = "Internal Server Error"
|
||||
ctx.Data["Title"] = "Page Not Found"
|
||||
ctx.HTML(http.StatusNotFound, base.TplName("status/404"))
|
||||
}
|
||||
|
||||
// ServerError displays a 500 (Internal Server Error) page and prints the given
|
||||
// error, if any.
|
||||
func (ctx *Context) ServerError(title string, err error) {
|
||||
if err != nil {
|
||||
log.Error(4, "%s: %v", title, err)
|
||||
if macaron.Env != macaron.PROD {
|
||||
ctx.Data["ErrorMsg"] = err
|
||||
}
|
||||
}
|
||||
ctx.HTML(status, base.TplName(fmt.Sprintf("status/%d", status)))
|
||||
|
||||
ctx.Data["Title"] = "Internal Server Error"
|
||||
ctx.HTML(404, base.TplName("status/500"))
|
||||
}
|
||||
|
||||
// NotFoundOrServerError use error check function to determine if the error
|
||||
|
@ -115,11 +123,11 @@ func (ctx *Context) Handle(status int, title string, err error) {
|
|||
// or error context description for logging purpose of 500 server error.
|
||||
func (ctx *Context) NotFoundOrServerError(title string, errck func(error) bool, err error) {
|
||||
if errck(err) {
|
||||
ctx.Handle(404, title, err)
|
||||
ctx.NotFound(title, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Handle(500, title, err)
|
||||
ctx.ServerError(title, err)
|
||||
}
|
||||
|
||||
// HandleText handles HTTP status code
|
||||
|
@ -218,7 +226,7 @@ func Contexter() macaron.Handler {
|
|||
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
|
||||
if ctx.Req.Method == "POST" && strings.Contains(ctx.Req.Header.Get("Content-Type"), "multipart/form-data") {
|
||||
if err := ctx.Req.ParseMultipartForm(setting.AttachmentMaxSize << 20); err != nil && !strings.Contains(err.Error(), "EOF") { // 32MB max size
|
||||
ctx.Handle(500, "ParseMultipartForm", err)
|
||||
ctx.ServerError("ParseMultipartForm", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,9 +51,9 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
|
|||
ctx.Org.Organization, err = models.GetUserByName(orgName)
|
||||
if err != nil {
|
||||
if models.IsErrUserNotExist(err) {
|
||||
ctx.Handle(404, "GetUserByName", err)
|
||||
ctx.NotFound("GetUserByName", err)
|
||||
} else {
|
||||
ctx.Handle(500, "GetUserByName", err)
|
||||
ctx.ServerError("GetUserByName", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
|
|||
} else if ctx.IsSigned {
|
||||
ctx.Org.IsOwner, err = org.IsOwnedBy(ctx.User.ID)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "IsOwnedBy", err)
|
||||
ctx.ServerError("IsOwnedBy", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -86,7 +86,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
|
|||
} else {
|
||||
ctx.Org.IsMember, err = org.IsOrgMember(ctx.User.ID)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "IsOrgMember", err)
|
||||
ctx.ServerError("IsOrgMember", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
|
|||
}
|
||||
if (requireMember && !ctx.Org.IsMember) ||
|
||||
(requireOwner && !ctx.Org.IsOwner) {
|
||||
ctx.Handle(404, "OrgAssignment", err)
|
||||
ctx.NotFound("OrgAssignment", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["IsOrganizationOwner"] = ctx.Org.IsOwner
|
||||
|
@ -109,13 +109,13 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
|
|||
if ctx.Org.IsMember {
|
||||
if ctx.Org.IsOwner {
|
||||
if err := org.GetTeams(); err != nil {
|
||||
ctx.Handle(500, "GetTeams", err)
|
||||
ctx.ServerError("GetTeams", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
org.Teams, err = org.GetUserTeams(ctx.User.ID)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetUserTeams", err)
|
||||
ctx.ServerError("GetUserTeams", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -135,20 +135,20 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
|
|||
}
|
||||
|
||||
if !teamExists {
|
||||
ctx.Handle(404, "OrgAssignment", err)
|
||||
ctx.NotFound("OrgAssignment", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["IsTeamMember"] = ctx.Org.IsTeamMember
|
||||
if requireTeamMember && !ctx.Org.IsTeamMember {
|
||||
ctx.Handle(404, "OrgAssignment", err)
|
||||
ctx.NotFound("OrgAssignment", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Org.IsTeamAdmin = ctx.Org.Team.IsOwnerTeam() || ctx.Org.Team.Authorize >= models.AccessModeAdmin
|
||||
ctx.Data["IsTeamAdmin"] = ctx.Org.IsTeamAdmin
|
||||
if requireTeamAdmin && !ctx.Org.IsTeamAdmin {
|
||||
ctx.Handle(404, "OrgAssignment", err)
|
||||
ctx.NotFound("OrgAssignment", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -166,10 +166,10 @@ func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
|
|||
repo.ForkID = 0
|
||||
return
|
||||
}
|
||||
ctx.Handle(500, "GetBaseRepo", err)
|
||||
ctx.ServerError("GetBaseRepo", err)
|
||||
return
|
||||
} else if err = repo.BaseRepo.GetOwner(); err != nil {
|
||||
ctx.Handle(500, "BaseRepo.GetOwner", err)
|
||||
ctx.ServerError("BaseRepo.GetOwner", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -201,7 +201,7 @@ func RedirectToRepo(ctx *Context, redirectRepoID int64) {
|
|||
|
||||
repo, err := models.GetRepositoryByID(redirectRepoID)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetRepositoryByID", err)
|
||||
ctx.ServerError("GetRepositoryByID", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -225,7 +225,7 @@ func repoAssignment(ctx *Context, repo *models.Repository) {
|
|||
}
|
||||
mode, err := models.AccessLevel(userID, repo)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "AccessLevel", err)
|
||||
ctx.ServerError("AccessLevel", err)
|
||||
return
|
||||
}
|
||||
ctx.Repo.AccessMode = mode
|
||||
|
@ -237,7 +237,7 @@ func repoAssignment(ctx *Context, repo *models.Repository) {
|
|||
EarlyResponseForGoGetMeta(ctx)
|
||||
return
|
||||
}
|
||||
ctx.Handle(404, "no access right", nil)
|
||||
ctx.NotFound("no access right", nil)
|
||||
return
|
||||
}
|
||||
ctx.Data["HasAccess"] = true
|
||||
|
@ -246,7 +246,7 @@ func repoAssignment(ctx *Context, repo *models.Repository) {
|
|||
var err error
|
||||
ctx.Repo.Mirror, err = models.GetMirrorByRepoID(repo.ID)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetMirror", err)
|
||||
ctx.ServerError("GetMirror", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["MirrorEnablePrune"] = ctx.Repo.Mirror.EnablePrune
|
||||
|
@ -268,15 +268,15 @@ func RepoIDAssignment() macaron.Handler {
|
|||
repo, err := models.GetRepositoryByID(repoID)
|
||||
if err != nil {
|
||||
if models.IsErrRepoNotExist(err) {
|
||||
ctx.Handle(404, "GetRepositoryByID", nil)
|
||||
ctx.NotFound("GetRepositoryByID", nil)
|
||||
} else {
|
||||
ctx.Handle(500, "GetRepositoryByID", err)
|
||||
ctx.ServerError("GetRepositoryByID", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err = repo.GetOwner(); err != nil {
|
||||
ctx.Handle(500, "GetOwner", err)
|
||||
ctx.ServerError("GetOwner", err)
|
||||
return
|
||||
}
|
||||
repoAssignment(ctx, repo)
|
||||
|
@ -305,9 +305,9 @@ func RepoAssignment() macaron.Handler {
|
|||
EarlyResponseForGoGetMeta(ctx)
|
||||
return
|
||||
}
|
||||
ctx.Handle(404, "GetUserByName", nil)
|
||||
ctx.NotFound("GetUserByName", nil)
|
||||
} else {
|
||||
ctx.Handle(500, "GetUserByName", err)
|
||||
ctx.ServerError("GetUserByName", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -327,12 +327,12 @@ func RepoAssignment() macaron.Handler {
|
|||
EarlyResponseForGoGetMeta(ctx)
|
||||
return
|
||||
}
|
||||
ctx.Handle(404, "GetRepositoryByName", nil)
|
||||
ctx.NotFound("GetRepositoryByName", nil)
|
||||
} else {
|
||||
ctx.Handle(500, "LookupRepoRedirect", err)
|
||||
ctx.ServerError("LookupRepoRedirect", err)
|
||||
}
|
||||
} else {
|
||||
ctx.Handle(500, "GetRepositoryByName", err)
|
||||
ctx.ServerError("GetRepositoryByName", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -345,7 +345,7 @@ func RepoAssignment() macaron.Handler {
|
|||
|
||||
gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
|
||||
if err != nil {
|
||||
ctx.Handle(500, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
|
||||
ctx.ServerError("RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
|
||||
return
|
||||
}
|
||||
ctx.Repo.GitRepo = gitRepo
|
||||
|
@ -355,7 +355,7 @@ func RepoAssignment() macaron.Handler {
|
|||
|
||||
tags, err := ctx.Repo.GitRepo.GetTags()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetTags", err)
|
||||
ctx.ServerError("GetTags", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Tags"] = tags
|
||||
|
@ -365,7 +365,7 @@ func RepoAssignment() macaron.Handler {
|
|||
IncludeTags: true,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetReleaseCountByRepoID", err)
|
||||
ctx.ServerError("GetReleaseCountByRepoID", err)
|
||||
return
|
||||
}
|
||||
ctx.Repo.Repository.NumReleases = int(count)
|
||||
|
@ -378,7 +378,7 @@ func RepoAssignment() macaron.Handler {
|
|||
ctx.Data["IsRepositoryWriter"] = ctx.Repo.IsWriter()
|
||||
|
||||
if ctx.Data["CanSignedUserFork"], err = ctx.Repo.Repository.CanUserFork(ctx.User); err != nil {
|
||||
ctx.Handle(500, "CanUserFork", err)
|
||||
ctx.ServerError("CanUserFork", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -403,7 +403,7 @@ func RepoAssignment() macaron.Handler {
|
|||
ctx.Data["TagName"] = ctx.Repo.TagName
|
||||
brs, err := ctx.Repo.GitRepo.GetBranches()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetBranches", err)
|
||||
ctx.ServerError("GetBranches", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Branches"] = brs
|
||||
|
@ -550,7 +550,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
|
|||
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
|
||||
ctx.Repo.GitRepo, err = git.OpenRepository(repoPath)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "RepoRef Invalid repo "+repoPath, err)
|
||||
ctx.ServerError("RepoRef Invalid repo "+repoPath, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -562,19 +562,19 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
|
|||
if !ctx.Repo.GitRepo.IsBranchExist(refName) {
|
||||
brs, err := ctx.Repo.GitRepo.GetBranches()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetBranches", err)
|
||||
ctx.ServerError("GetBranches", err)
|
||||
return
|
||||
} else if len(brs) == 0 {
|
||||
err = fmt.Errorf("No branches in non-bare repository %s",
|
||||
ctx.Repo.GitRepo.Path)
|
||||
ctx.Handle(500, "GetBranches", err)
|
||||
ctx.ServerError("GetBranches", err)
|
||||
return
|
||||
}
|
||||
refName = brs[0]
|
||||
}
|
||||
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetBranchCommit", err)
|
||||
ctx.ServerError("GetBranchCommit", err)
|
||||
return
|
||||
}
|
||||
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
|
||||
|
@ -588,7 +588,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
|
|||
|
||||
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetBranchCommit", err)
|
||||
ctx.ServerError("GetBranchCommit", err)
|
||||
return
|
||||
}
|
||||
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
|
||||
|
@ -597,7 +597,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
|
|||
ctx.Repo.IsViewTag = true
|
||||
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetTagCommit(refName)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetTagCommit", err)
|
||||
ctx.ServerError("GetTagCommit", err)
|
||||
return
|
||||
}
|
||||
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
|
||||
|
@ -607,11 +607,11 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
|
|||
|
||||
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
|
||||
if err != nil {
|
||||
ctx.Handle(404, "GetCommit", nil)
|
||||
ctx.NotFound("GetCommit", nil)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
ctx.Handle(404, "RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
|
||||
ctx.NotFound("RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -637,7 +637,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
|
|||
|
||||
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetCommitsCount", err)
|
||||
ctx.ServerError("GetCommitsCount", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
|
||||
|
@ -648,7 +648,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
|
|||
func RequireRepoAdmin() macaron.Handler {
|
||||
return func(ctx *Context) {
|
||||
if !ctx.IsSigned || (!ctx.Repo.IsAdmin() && !ctx.User.IsAdmin) {
|
||||
ctx.Handle(404, ctx.Req.RequestURI, nil)
|
||||
ctx.NotFound(ctx.Req.RequestURI, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -658,7 +658,7 @@ func RequireRepoAdmin() macaron.Handler {
|
|||
func RequireRepoWriter() macaron.Handler {
|
||||
return func(ctx *Context) {
|
||||
if !ctx.IsSigned || (!ctx.Repo.IsWriter() && !ctx.User.IsAdmin) {
|
||||
ctx.Handle(404, ctx.Req.RequestURI, nil)
|
||||
ctx.NotFound(ctx.Req.RequestURI, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -678,7 +678,7 @@ func LoadRepoUnits() macaron.Handler {
|
|||
}
|
||||
err := ctx.Repo.Repository.LoadUnitsByUserID(userID, isAdmin)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "LoadUnitsByUserID", err)
|
||||
ctx.ServerError("LoadUnitsByUserID", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -688,7 +688,7 @@ func LoadRepoUnits() macaron.Handler {
|
|||
func CheckUnit(unitType models.UnitType) macaron.Handler {
|
||||
return func(ctx *Context) {
|
||||
if !ctx.Repo.Repository.UnitEnabled(unitType) {
|
||||
ctx.Handle(404, "CheckUnit", fmt.Errorf("%s: %v", ctx.Tr("units.error.unit_not_allowed"), unitType))
|
||||
ctx.NotFound("CheckUnit", fmt.Errorf("%s: %v", ctx.Tr("units.error.unit_not_allowed"), unitType))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -697,7 +697,7 @@ func CheckUnit(unitType models.UnitType) macaron.Handler {
|
|||
func CheckAnyUnit(unitTypes ...models.UnitType) macaron.Handler {
|
||||
return func(ctx *Context) {
|
||||
if !ctx.Repo.Repository.AnyUnitEnabled(unitTypes...) {
|
||||
ctx.Handle(404, "CheckAnyUnit", fmt.Errorf("%s: %v", ctx.Tr("units.error.unit_not_allowed"), unitTypes))
|
||||
ctx.NotFound("CheckAnyUnit", fmt.Errorf("%s: %v", ctx.Tr("units.error.unit_not_allowed"), unitTypes))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -706,7 +706,7 @@ func CheckAnyUnit(unitTypes ...models.UnitType) macaron.Handler {
|
|||
func GitHookService() macaron.Handler {
|
||||
return func(ctx *Context) {
|
||||
if !ctx.User.CanEditGitHook() {
|
||||
ctx.Handle(404, "GitHookService", nil)
|
||||
ctx.NotFound("GitHookService", nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue