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:
Morgan Bazalgette 2018-01-10 22:34:17 +01:00 committed by Lauris BH
parent 45c264f681
commit 65861900cd
48 changed files with 622 additions and 610 deletions

View file

@ -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
}
}