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

@ -31,14 +31,14 @@ const (
// MustBeNotBare render when a repo is a bare git dir
func MustBeNotBare(ctx *context.Context) {
if ctx.Repo.Repository.IsBare {
ctx.Handle(404, "MustBeNotBare", nil)
ctx.NotFound("MustBeNotBare", nil)
}
}
// MustBeEditable check that repo can be edited
func MustBeEditable(ctx *context.Context) {
if !ctx.Repo.Repository.CanEnableEditor() || ctx.Repo.IsViewCommit {
ctx.Handle(404, "", nil)
ctx.NotFound("", nil)
return
}
}
@ -46,14 +46,14 @@ func MustBeEditable(ctx *context.Context) {
// MustBeAbleToUpload check that repo can be uploaded to
func MustBeAbleToUpload(ctx *context.Context) {
if !setting.Repository.Upload.Enabled {
ctx.Handle(404, "", nil)
ctx.NotFound("", nil)
}
}
func checkContextUser(ctx *context.Context, uid int64) *models.User {
orgs, err := models.GetOwnedOrgsByUserIDDesc(ctx.User.ID, "updated_unix")
if err != nil {
ctx.Handle(500, "GetOwnedOrgsByUserIDDesc", err)
ctx.ServerError("GetOwnedOrgsByUserIDDesc", err)
return nil
}
ctx.Data["Orgs"] = orgs
@ -69,7 +69,7 @@ func checkContextUser(ctx *context.Context, uid int64) *models.User {
}
if err != nil {
ctx.Handle(500, "GetUserByID", fmt.Errorf("[%d]: %v", uid, err))
ctx.ServerError("GetUserByID", fmt.Errorf("[%d]: %v", uid, err))
return nil
}
@ -81,7 +81,7 @@ func checkContextUser(ctx *context.Context, uid int64) *models.User {
if !ctx.User.IsAdmin {
isOwner, err := org.IsOwnedBy(ctx.User.ID)
if err != nil {
ctx.Handle(500, "IsOwnedBy", err)
ctx.ServerError("IsOwnedBy", err)
return nil
} else if !isOwner {
ctx.Error(403)
@ -143,7 +143,7 @@ func handleCreateError(ctx *context.Context, owner *models.User, err error, name
ctx.Data["Err_RepoName"] = true
ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form)
default:
ctx.Handle(500, name, err)
ctx.ServerError(name, err)
}
}
@ -235,10 +235,10 @@ func MigratePost(ctx *context.Context, form auth.MigrateRepoForm) {
case addrErr.IsInvalidPath:
ctx.RenderWithErr(ctx.Tr("repo.migrate.invalid_local_path"), tplMigrate, &form)
default:
ctx.Handle(500, "Unknown error", err)
ctx.ServerError("Unknown error", err)
}
} else {
ctx.Handle(500, "ParseRemoteAddr", err)
ctx.ServerError("ParseRemoteAddr", err)
}
return
}
@ -303,7 +303,7 @@ func Action(ctx *context.Context) {
}
if err != nil {
ctx.Handle(500, fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
return
}
@ -342,7 +342,7 @@ func Download(ctx *context.Context) {
if !com.IsDir(archivePath) {
if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err)
ctx.ServerError("Download -> os.MkdirAll(archivePath)", err)
return
}
}
@ -356,30 +356,30 @@ func Download(ctx *context.Context) {
if gitRepo.IsBranchExist(refName) {
commit, err = gitRepo.GetBranchCommit(refName)
if err != nil {
ctx.Handle(500, "GetBranchCommit", err)
ctx.ServerError("GetBranchCommit", err)
return
}
} else if gitRepo.IsTagExist(refName) {
commit, err = gitRepo.GetTagCommit(refName)
if err != nil {
ctx.Handle(500, "GetTagCommit", err)
ctx.ServerError("GetTagCommit", err)
return
}
} else if len(refName) >= 4 && len(refName) <= 40 {
commit, err = gitRepo.GetCommit(refName)
if err != nil {
ctx.Handle(404, "GetCommit", nil)
ctx.NotFound("GetCommit", nil)
return
}
} else {
ctx.Handle(404, "Download", nil)
ctx.NotFound("Download", nil)
return
}
archivePath = path.Join(archivePath, base.ShortSha(commit.ID.String())+ext)
if !com.IsFile(archivePath) {
if err := commit.CreateArchive(archivePath, archiveType); err != nil {
ctx.Handle(500, "Download -> CreateArchive "+archivePath, err)
ctx.ServerError("Download -> CreateArchive "+archivePath, err)
return
}
}