Browse Source

Handle refactor (#3339)

* Replace all ctx.Handle with ctx.ServerError or ctx.NotFound

* Change Handle(403) to NotFound, avoid using macaron's NotFound
release/v1.4
Morgan Bazalgette 5 years ago
committed by Lauris BH
parent
commit
65861900cd
  1. 32
      modules/context/context.go
  2. 20
      modules/context/org.go
  3. 68
      modules/context/repo.go
  4. 12
      routers/admin/auths.go
  5. 4
      routers/admin/notice.go
  6. 4
      routers/admin/repos.go
  7. 20
      routers/admin/users.go
  8. 2
      routers/api/v1/repo/fork.go
  9. 2
      routers/api/v1/repo/label.go
  10. 2
      routers/api/v1/repo/milestone.go
  11. 8
      routers/api/v1/repo/pull.go
  12. 4
      routers/api/v1/repo/release.go
  13. 2
      routers/api/v1/repo/repo.go
  14. 6
      routers/home.go
  15. 2
      routers/install.go
  16. 2
      routers/org/members.go
  17. 12
      routers/org/org.go
  18. 12
      routers/org/setting.go
  19. 16
      routers/org/teams.go
  20. 2
      routers/repo/activity.go
  21. 12
      routers/repo/branch.go
  22. 34
      routers/repo/commit.go
  23. 6
      routers/repo/download.go
  24. 20
      routers/repo/editor.go
  25. 20
      routers/repo/http.go
  26. 130
      routers/repo/issue.go
  27. 18
      routers/repo/issue_label.go
  28. 8
      routers/repo/issue_stopwatch.go
  29. 4
      routers/repo/issue_timetrack.go
  30. 4
      routers/repo/issue_watch.go
  31. 4
      routers/repo/middlewares.go
  32. 160
      routers/repo/pull.go
  33. 28
      routers/repo/release.go
  34. 32
      routers/repo/repo.go
  35. 2
      routers/repo/search.go
  36. 60
      routers/repo/setting.go
  37. 24
      routers/repo/setting_protected_branch.go
  38. 18
      routers/repo/view.go
  39. 72
      routers/repo/webhook.go
  40. 38
      routers/repo/wiki.go
  41. 20
      routers/routes/routes.go
  42. 88
      routers/user/auth.go
  43. 12
      routers/user/auth_openid.go
  44. 56
      routers/user/home.go
  45. 12
      routers/user/notification.go
  46. 22
      routers/user/profile.go
  47. 82
      routers/user/setting.go
  48. 14
      routers/user/setting_openid.go

32
modules/context/context.go

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

20
modules/context/org.go

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

68
modules/context/repo.go

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

12
routers/admin/auths.go

@ -35,7 +35,7 @@ func Authentications(ctx *context.Context) {
var err error
ctx.Data["Sources"], err = models.LoginSources()
if err != nil {
ctx.Handle(500, "LoginSources", err)
ctx.ServerError("LoginSources", err)
return
}
@ -197,7 +197,7 @@ func NewAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) {
ctx.Data["Err_Name"] = true
ctx.RenderWithErr(ctx.Tr("admin.auths.login_source_exist", err.(models.ErrLoginSourceAlreadyExist).Name), tplAuthNew, form)
} else {
ctx.Handle(500, "CreateSource", err)
ctx.ServerError("CreateSource", err)
}
return
}
@ -221,7 +221,7 @@ func EditAuthSource(ctx *context.Context) {
source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
if err != nil {
ctx.Handle(500, "GetLoginSourceByID", err)
ctx.ServerError("GetLoginSourceByID", err)
return
}
ctx.Data["Source"] = source
@ -245,7 +245,7 @@ func EditAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) {
source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
if err != nil {
ctx.Handle(500, "GetLoginSourceByID", err)
ctx.ServerError("GetLoginSourceByID", err)
return
}
ctx.Data["Source"] = source
@ -282,7 +282,7 @@ func EditAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) {
ctx.Flash.Error(err.Error(), true)
ctx.HTML(200, tplAuthEdit)
} else {
ctx.Handle(500, "UpdateSource", err)
ctx.ServerError("UpdateSource", err)
}
return
}
@ -296,7 +296,7 @@ func EditAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) {
func DeleteAuthSource(ctx *context.Context) {
source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
if err != nil {
ctx.Handle(500, "GetLoginSourceByID", err)
ctx.ServerError("GetLoginSourceByID", err)
return
}

4
routers/admin/notice.go

@ -34,7 +34,7 @@ func Notices(ctx *context.Context) {
notices, err := models.Notices(page, setting.UI.Admin.NoticePagingNum)
if err != nil {
ctx.Handle(500, "Notices", err)
ctx.ServerError("Notices", err)
return
}
ctx.Data["Notices"] = notices
@ -66,7 +66,7 @@ func DeleteNotices(ctx *context.Context) {
// EmptyNotices delete all the notices
func EmptyNotices(ctx *context.Context) {
if err := models.DeleteNotices(0, 0); err != nil {
ctx.Handle(500, "DeleteNotices", err)
ctx.ServerError("DeleteNotices", err)
return
}

4
routers/admin/repos.go

@ -34,12 +34,12 @@ func Repos(ctx *context.Context) {
func DeleteRepo(ctx *context.Context) {
repo, err := models.GetRepositoryByID(ctx.QueryInt64("id"))
if err != nil {
ctx.Handle(500, "GetRepositoryByID", err)
ctx.ServerError("GetRepositoryByID", err)
return
}
if err := models.DeleteRepository(ctx.User, repo.MustOwner().ID, repo.ID); err != nil {
ctx.Handle(500, "DeleteRepository", err)
ctx.ServerError("DeleteRepository", err)
return
}
log.Trace("Repository deleted: %s/%s", repo.MustOwner().Name, repo.Name)

20
routers/admin/users.go

@ -47,7 +47,7 @@ func NewUser(ctx *context.Context) {
sources, err := models.LoginSources()
if err != nil {
ctx.Handle(500, "LoginSources", err)
ctx.ServerError("LoginSources", err)
return
}
ctx.Data["Sources"] = sources
@ -64,7 +64,7 @@ func NewUserPost(ctx *context.Context, form auth.AdminCreateUserForm) {
sources, err := models.LoginSources()
if err != nil {
ctx.Handle(500, "LoginSources", err)
ctx.ServerError("LoginSources", err)
return
}
ctx.Data["Sources"] = sources
@ -108,7 +108,7 @@ func NewUserPost(ctx *context.Context, form auth.AdminCreateUserForm) {
ctx.Data["Err_UserName"] = true
ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplUserNew, &form)
default:
ctx.Handle(500, "CreateUser", err)
ctx.ServerError("CreateUser", err)
}
return
}
@ -126,7 +126,7 @@ func NewUserPost(ctx *context.Context, form auth.AdminCreateUserForm) {
func prepareUserInfo(ctx *context.Context) *models.User {
u, err := models.GetUserByID(ctx.ParamsInt64(":userid"))
if err != nil {
ctx.Handle(500, "GetUserByID", err)
ctx.ServerError("GetUserByID", err)
return nil
}
ctx.Data["User"] = u
@ -134,7 +134,7 @@ func prepareUserInfo(ctx *context.Context) *models.User {
if u.LoginSource > 0 {
ctx.Data["LoginSource"], err = models.GetLoginSourceByID(u.LoginSource)
if err != nil {
ctx.Handle(500, "GetLoginSourceByID", err)
ctx.ServerError("GetLoginSourceByID", err)
return nil
}
} else {
@ -143,7 +143,7 @@ func prepareUserInfo(ctx *context.Context) *models.User {
sources, err := models.LoginSources()
if err != nil {
ctx.Handle(500, "LoginSources", err)
ctx.ServerError("LoginSources", err)
return nil
}
ctx.Data["Sources"] = sources
@ -197,7 +197,7 @@ func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) {
u.Passwd = form.Password
var err error
if u.Salt, err = models.GetUserSalt(); err != nil {
ctx.Handle(500, "UpdateUser", err)
ctx.ServerError("UpdateUser", err)
return
}
u.HashPassword()
@ -221,7 +221,7 @@ func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) {
ctx.Data["Err_Email"] = true
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplUserEdit, &form)
} else {
ctx.Handle(500, "UpdateUser", err)
ctx.ServerError("UpdateUser", err)
}
return
}
@ -235,7 +235,7 @@ func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) {
func DeleteUser(ctx *context.Context) {
u, err := models.GetUserByID(ctx.ParamsInt64(":userid"))
if err != nil {
ctx.Handle(500, "GetUserByID", err)
ctx.ServerError("GetUserByID", err)
return
}
@ -252,7 +252,7 @@ func DeleteUser(ctx *context.Context) {
"redirect": setting.AppSubURL + "/admin/users/" + ctx.Params(":userid"),
})
default:
ctx.Handle(500, "DeleteUser", err)
ctx.ServerError("DeleteUser", err)
}
return
}

2
routers/api/v1/repo/fork.go

@ -91,7 +91,7 @@ func CreateFork(ctx *context.APIContext, form api.CreateForkOption) {
}
isMember, err := org.IsOrgMember(ctx.User.ID)
if err != nil {
ctx.Handle(500, "IsOrgMember", err)
ctx.ServerError("IsOrgMember", err)
return
} else if !isMember {
ctx.Status(403)

2
routers/api/v1/repo/label.go

@ -193,7 +193,7 @@ func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
label.Color = *form.Color
}
if err := models.UpdateLabel(label); err != nil {
ctx.Handle(500, "UpdateLabel", err)
ctx.ServerError("UpdateLabel", err)
return
}
ctx.JSON(200, label.APIFormat())

2
routers/api/v1/repo/milestone.go

@ -180,7 +180,7 @@ func EditMilestone(ctx *context.APIContext, form api.EditMilestoneOption) {
}
if err := models.UpdateMilestone(milestone); err != nil {
ctx.Handle(500, "UpdateMilestone", err)
ctx.ServerError("UpdateMilestone", err)
return
}
ctx.JSON(200, milestone.APIFormat())

8
routers/api/v1/repo/pull.go

@ -465,7 +465,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
if models.IsErrPullRequestNotExist(err) {
ctx.Handle(404, "GetPullRequestByIndex", err)
ctx.NotFound("GetPullRequestByIndex", err)
} else {
ctx.Error(500, "GetPullRequestByIndex", err)
}
@ -473,7 +473,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
}
if err = pr.GetHeadRepo(); err != nil {
ctx.Handle(500, "GetHeadRepo", err)
ctx.ServerError("GetHeadRepo", err)
return
}
@ -560,9 +560,9 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
headUser, err = models.GetUserByName(headInfos[0])
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.Handle(404, "GetUserByName", nil)
ctx.NotFound("GetUserByName", nil)
} else {
ctx.Handle(500, "GetUserByName", err)
ctx.ServerError("GetUserByName", err)
}
return nil, nil, nil, nil, "", ""
}

4
routers/api/v1/repo/release.go

@ -132,7 +132,7 @@ func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) {
rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName)
if err != nil {
if !models.IsErrReleaseNotExist(err) {
ctx.Handle(500, "GetRelease", err)
ctx.ServerError("GetRelease", err)
return
}
rel = &models.Release{
@ -169,7 +169,7 @@ func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) {
rel.IsTag = false
if err = models.UpdateRelease(ctx.Repo.GitRepo, rel, nil); err != nil {
ctx.Handle(500, "UpdateRelease", err)
ctx.ServerError("UpdateRelease", err)
return
}
}

2
routers/api/v1/repo/repo.go

@ -258,7 +258,7 @@ func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) {
isOwner, err := org.IsOwnedBy(ctx.User.ID)
if err != nil {
ctx.Handle(500, "IsOwnedBy", err)
ctx.ServerError("IsOwnedBy", err)
return
} else if !isOwner {
ctx.Error(403, "", "Given user is not owner of organization.")

6
routers/home.go

@ -117,7 +117,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
AllPublic: true,
})
if err != nil {
ctx.Handle(500, "SearchRepositoryByName", err)
ctx.ServerError("SearchRepositoryByName", err)
return
}
ctx.Data["Keyword"] = keyword
@ -185,7 +185,7 @@ func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplN
if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
users, count, err = models.SearchUsers(opts)
if err != nil {
ctx.Handle(500, "SearchUsers", err)
ctx.ServerError("SearchUsers", err)
return
}
}
@ -226,5 +226,5 @@ func ExploreOrganizations(ctx *context.Context) {
// NotFound render 404 page
func NotFound(ctx *context.Context) {
ctx.Data["Title"] = "Page Not Found"
ctx.Handle(404, "home.NotFound", nil)
ctx.NotFound("home.NotFound", nil)
}

2
routers/install.go

@ -33,7 +33,7 @@ const (
// InstallInit prepare for rendering installation page
func InstallInit(ctx *context.Context) {
if setting.InstallLock {
ctx.Handle(404, "Install", errors.New("Installation is prohibited"))
ctx.NotFound("Install", errors.New("Installation is prohibited"))
return
}

2
routers/org/members.go

@ -26,7 +26,7 @@ func Members(ctx *context.Context) {
ctx.Data["PageIsOrgMembers"] = true
if err := org.GetMembers(); err != nil {
ctx.Handle(500, "GetMembers", err)
ctx.ServerError("GetMembers", err)
return
}
ctx.Data["Members"] = org.Members

12
routers/org/org.go

@ -22,9 +22,13 @@ const (
// Create render the page for create organization
func Create(ctx *context.Context) {
if !ctx.User.CanCreateOrganization() {
ctx.NotFound("CanCreateOrganization", nil)
}
ctx.Data["Title"] = ctx.Tr("new_org")
if !ctx.User.CanCreateOrganization() {
ctx.Handle(500, "Not allowed", errors.New(ctx.Tr("org.form.create_org_not_allowed")))
ctx.ServerError("Not allowed", errors.New(ctx.Tr("org.form.create_org_not_allowed")))
return
}
ctx.HTML(200, tplCreateOrg)
@ -32,6 +36,10 @@ func Create(ctx *context.Context) {
// CreatePost response for create organization
func CreatePost(ctx *context.Context, form auth.CreateOrgForm) {
if !ctx.User.CanCreateOrganization() {
ctx.NotFound("CanCreateOrganization", nil)
}
ctx.Data["Title"] = ctx.Tr("new_org")
if ctx.HasError() {
@ -57,7 +65,7 @@ func CreatePost(ctx *context.Context, form auth.CreateOrgForm) {
case models.IsErrUserNotAllowedCreateOrg(err):
ctx.RenderWithErr(ctx.Tr("org.form.create_org_not_allowed"), tplCreateOrg, &form)
default:
ctx.Handle(500, "CreateOrganization", err)
ctx.ServerError("CreateOrganization", err)
}
return
}

12
routers/org/setting.go

@ -48,7 +48,7 @@ func SettingsPost(ctx *context.Context, form auth.UpdateOrgSettingForm) {
if org.LowerName != strings.ToLower(form.Name) {
isExist, err := models.IsUserExist(org.ID, form.Name)
if err != nil {
ctx.Handle(500, "IsUserExist", err)
ctx.ServerError("IsUserExist", err)
return
} else if isExist {
ctx.Data["OrgName"] = true
@ -59,7 +59,7 @@ func SettingsPost(ctx *context.Context, form auth.UpdateOrgSettingForm) {
ctx.Data["OrgName"] = true
ctx.RenderWithErr(ctx.Tr("form.illegal_username"), tplSettingsOptions, &form)
} else {
ctx.Handle(500, "ChangeUserName", err)
ctx.ServerError("ChangeUserName", err)
}
return
}
@ -80,7 +80,7 @@ func SettingsPost(ctx *context.Context, form auth.UpdateOrgSettingForm) {
org.Website = form.Website
org.Location = form.Location
if err := models.UpdateUser(org); err != nil {
ctx.Handle(500, "UpdateUser", err)
ctx.ServerError("UpdateUser", err)
return
}
log.Trace("Organization setting updated: %s", org.Name)
@ -120,7 +120,7 @@ func SettingsDelete(ctx *context.Context) {
if models.IsErrUserNotExist(err) {
ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsDelete, nil)
} else {
ctx.Handle(500, "UserSignIn", err)
ctx.ServerError("UserSignIn", err)
}
return
}
@ -130,7 +130,7 @@ func SettingsDelete(ctx *context.Context) {
ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
ctx.Redirect(ctx.Org.OrgLink + "/settings/delete")
} else {
ctx.Handle(500, "DeleteOrganization", err)
ctx.ServerError("DeleteOrganization", err)
}
} else {
log.Trace("Organization deleted: %s", org.Name)
@ -151,7 +151,7 @@ func Webhooks(ctx *context.Context) {
ws, err := models.GetWebhooksByOrgID(ctx.Org.Organization.ID)
if err != nil {
ctx.Handle(500, "GetWebhooksByOrgId", err)
ctx.ServerError("GetWebhooksByOrgId", err)
return
}

16
routers/org/teams.go

@ -37,7 +37,7 @@ func Teams(ctx *context.Context) {
for _, t := range org.Teams {
if err := t.GetMembers(); err != nil {
ctx.Handle(500, "GetMembers", err)
ctx.ServerError("GetMembers", err)
return
}
}
@ -85,7 +85,7 @@ func TeamsAction(ctx *context.Context) {
ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName)
} else {
ctx.Handle(500, " GetUserByName", err)
ctx.ServerError(" GetUserByName", err)
}
return
}
@ -142,7 +142,7 @@ func TeamsRepoAction(ctx *context.Context) {
ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories")
return
}
ctx.Handle(500, "GetRepositoryByName", err)
ctx.ServerError("GetRepositoryByName", err)
return
}
err = ctx.Org.Team.AddRepository(repo)
@ -152,7 +152,7 @@ func TeamsRepoAction(ctx *context.Context) {
if err != nil {
log.Error(3, "Action(%s): '%s' %v", ctx.Params(":action"), ctx.Org.Team.Name, err)
ctx.Handle(500, "TeamsRepoAction", err)
ctx.ServerError("TeamsRepoAction", err)
return
}
ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories")
@ -203,7 +203,7 @@ func NewTeamPost(ctx *context.Context, form auth.CreateTeamForm) {
case models.IsErrTeamAlreadyExist(err):
ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), tplTeamNew, &form)
default:
ctx.Handle(500, "NewTeam", err)
ctx.ServerError("NewTeam", err)
}
return
}
@ -216,7 +216,7 @@ func TeamMembers(ctx *context.Context) {
ctx.Data["Title"] = ctx.Org.Team.Name
ctx.Data["PageIsOrgTeams"] = true
if err := ctx.Org.Team.GetMembers(); err != nil {
ctx.Handle(500, "GetMembers", err)
ctx.ServerError("GetMembers", err)
return
}
ctx.HTML(200, tplTeamMembers)
@ -227,7 +227,7 @@ func TeamRepositories(ctx *context.Context) {
ctx.Data["Title"] = ctx.Org.Team.Name
ctx.Data["PageIsOrgTeams"] = true
if err := ctx.Org.Team.GetRepositories(); err != nil {
ctx.Handle(500, "GetRepositories", err)
ctx.ServerError("GetRepositories", err)
return
}
ctx.HTML(200, tplTeamRepositories)
@ -285,7 +285,7 @@ func EditTeamPost(ctx *context.Context, form auth.CreateTeamForm) {
case models.IsErrTeamAlreadyExist(err):
ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), tplTeamNew, &form)
default:
ctx.Handle(500, "UpdateTeam", err)
ctx.ServerError("UpdateTeam", err)
}
return
}

2
routers/repo/activity.go

@ -48,7 +48,7 @@ func Activity(ctx *context.Context) {
ctx.Repo.Repository.UnitEnabled(models.UnitTypeReleases),
ctx.Repo.Repository.UnitEnabled(models.UnitTypeIssues),
ctx.Repo.Repository.UnitEnabled(models.UnitTypePullRequests)); err != nil {
ctx.Handle(500, "GetActivityStats", err)
ctx.ServerError("GetActivityStats", err)
return
}

12
routers/repo/branch.go

@ -136,7 +136,7 @@ func deleteBranch(ctx *context.Context, branchName string) error {
func loadBranches(ctx *context.Context) []*Branch {
rawBranches, err := ctx.Repo.Repository.GetBranches()
if err != nil {
ctx.Handle(500, "GetBranches", err)
ctx.ServerError("GetBranches", err)
return nil
}
@ -144,13 +144,13 @@ func loadBranches(ctx *context.Context) []*Branch {
for i := range rawBranches {
commit, err := rawBranches[i].GetCommit()
if err != nil {
ctx.Handle(500, "GetCommit", err)
ctx.ServerError("GetCommit", err)
return nil
}
isProtected, err := ctx.Repo.Repository.IsProtectedBranch(rawBranches[i].Name, ctx.User)
if err != nil {
ctx.Handle(500, "IsProtectedBranch", err)
ctx.ServerError("IsProtectedBranch", err)
return nil
}
@ -164,7 +164,7 @@ func loadBranches(ctx *context.Context) []*Branch {
if ctx.Repo.IsWriter() {
deletedBranches, err := getDeletedBranches(ctx)
if err != nil {
ctx.Handle(500, "getDeletedBranches", err)
ctx.ServerError("getDeletedBranches", err)
return nil
}
branches = append(branches, deletedBranches...)
@ -196,7 +196,7 @@ func getDeletedBranches(ctx *context.Context) ([]*Branch, error) {
// CreateBranch creates new branch in repository
func CreateBranch(ctx *context.Context, form auth.NewBranchForm) {
if !ctx.Repo.CanCreateBranch() {
ctx.Handle(404, "CreateBranch", nil)
ctx.NotFound("CreateBranch", nil)
return
}
@ -232,7 +232,7 @@ func CreateBranch(ctx *context.Context, form auth.NewBranchForm) {
return
}
ctx.Handle(500, "CreateNewBranch", err)
ctx.ServerError("CreateNewBranch", err)
return
}

34
routers/repo/commit.go

@ -40,14 +40,14 @@ func RefCommits(ctx *context.Context) {
func Commits(ctx *context.Context) {
ctx.Data["PageIsCommits"] = true
if ctx.Repo.Commit == nil {
ctx.Handle(404, "Commit not found", nil)
ctx.NotFound("Commit not found", nil)
return
}
ctx.Data["PageIsViewCode"] = true
commitsCount, err := ctx.Repo.GetCommitsCount()
if err != nil {
ctx.Handle(500, "GetCommitsCount", err)
ctx.ServerError("GetCommitsCount", err)
return
}
@ -60,7 +60,7 @@ func Commits(ctx *context.Context) {
// Both `git log branchName` and `git log commitId` work.
commits, err := ctx.Repo.Commit.CommitsByRange(page)
if err != nil {
ctx.Handle(500, "CommitsByRange", err)
ctx.ServerError("CommitsByRange", err)
return
}
commits = models.ValidateCommitsWithEmails(commits)
@ -82,13 +82,13 @@ func Graph(ctx *context.Context) {
commitsCount, err := ctx.Repo.GetCommitsCount()
if err != nil {
ctx.Handle(500, "GetCommitsCount", err)
ctx.ServerError("GetCommitsCount", err)
return
}
graph, err := models.GetCommitGraph(ctx.Repo.GitRepo)
if err != nil {
ctx.Handle(500, "GetCommitGraph", err)
ctx.ServerError("GetCommitGraph", err)
return
}
@ -116,7 +116,7 @@ func SearchCommits(ctx *context.Context) {
commits, err := ctx.Repo.Commit.SearchCommits(keyword, all)
if err != nil {
ctx.Handle(500, "SearchCommits", err)
ctx.ServerError("SearchCommits", err)
return
}
commits = models.ValidateCommitsWithEmails(commits)
@ -148,10 +148,10 @@ func FileHistory(ctx *context.Context) {
branchName := ctx.Repo.BranchName
commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
if err != nil {
ctx.Handle(500, "FileCommitsCount", err)
ctx.ServerError("FileCommitsCount", err)
return
} else if commitsCount == 0 {
ctx.Handle(404, "FileCommitsCount", nil)
ctx.NotFound("FileCommitsCount", nil)
return
}
@ -163,7 +163,7 @@ func FileHistory(ctx *context.Context) {
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page)
if err != nil {
ctx.Handle(500, "CommitsByFileAndRange", err)
ctx.ServerError("CommitsByFileAndRange", err)
return
}
commits = models.ValidateCommitsWithEmails(commits)
@ -191,9 +191,9 @@ func Diff(ctx *context.Context) {
commit, err := ctx.Repo.GitRepo.GetCommit(commitID)
if err != nil {
if git.IsErrNotExist(err) {
ctx.Handle(404, "Repo.GitRepo.GetCommit", err)
ctx.NotFound("Repo.GitRepo.GetCommit", err)
} else {
ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
ctx.ServerError("Repo.GitRepo.GetCommit", err)
}
return
}
@ -212,7 +212,7 @@ func Diff(ctx *context.Context) {
commitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
if err != nil {
ctx.Handle(404, "GetDiffCommit", err)
ctx.NotFound("GetDiffCommit", err)
return
}
@ -221,7 +221,7 @@ func Diff(ctx *context.Context) {
sha, err := commit.ParentID(i)
parents[i] = sha.String()
if err != nil {
ctx.Handle(404, "repo.Diff", err)
ctx.NotFound("repo.Diff", err)
return
}
}
@ -253,7 +253,7 @@ func RawDiff(ctx *context.Context) {
models.RawDiffType(ctx.Params(":ext")),
ctx.Resp,
); err != nil {
ctx.Handle(500, "GetRawDiff", err)
ctx.ServerError("GetRawDiff", err)
return
}
}
@ -269,7 +269,7 @@ func CompareDiff(ctx *context.Context) {
commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitID)
if err != nil {
ctx.Handle(404, "GetCommit", err)
ctx.NotFound("GetCommit", err)
return
}
@ -277,13 +277,13 @@ func CompareDiff(ctx *context.Context) {
afterCommitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
if err != nil {
ctx.Handle(404, "GetDiffRange", err)
ctx.NotFound("GetDiffRange", err)
return
}
commits, err := commit.CommitsBeforeUntil(beforeCommitID)
if err != nil {
ctx.Handle(500, "CommitsBeforeUntil", err)
ctx.ServerError("CommitsBeforeUntil", err)
return
}
commits = models.ValidateCommitsWithEmails(commits)

6
routers/repo/download.go

@ -59,13 +59,13 @@ func SingleDownload(ctx *context.Context) {
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
if err != nil {
if git.IsErrNotExist(err) {
ctx.Handle(404, "GetBlobByPath", nil)
ctx.NotFound("GetBlobByPath", nil)
} else {
ctx.Handle(500, "GetBlobByPath", err)
ctx.ServerError("GetBlobByPath", err)
}
return
}
if err = ServeBlob(ctx, blob); err != nil {
ctx.Handle(500, "ServeBlob", err)
ctx.ServerError("ServeBlob", err)
}
}

20
routers/repo/editor.go

@ -73,19 +73,19 @@ func editFile(ctx *context.Context, isNewFile bool) {
// No way to edit a directory online.
if entry.IsDir() {
ctx.Handle(404, "entry.IsDir", nil)
ctx.NotFound("entry.IsDir", nil)
return
}
blob := entry.Blob()
if blob.Size() >= setting.UI.MaxDisplayFileSize {
ctx.Handle(404, "blob.Size", err)
ctx.NotFound("blob.Size", err)
return
}
dataRc, err := blob.Data()
if err != nil {
ctx.Handle(404, "blob.Data", err)
ctx.NotFound("blob.Data", err)
return
}
@ -98,7 +98,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
// Only text file are editable online.
if !base.IsTextFile(buf) {
ctx.Handle(404, "base.IsTextFile", nil)
ctx.NotFound("base.IsTextFile", nil)
return
}
@ -214,7 +214,7 @@ func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bo
break
}
ctx.Handle(500, "Repo.Commit.GetTreeEntryByPath", err)
ctx.ServerError("Repo.Commit.GetTreeEntryByPath", err)
return
}
if index != len(treeNames)-1 {
@ -244,14 +244,14 @@ func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bo
ctx.Data["Err_TreePath"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.file_editing_no_longer_exists", oldTreePath), tplEditFile, &form)
} else {
ctx.Handle(500, "GetTreeEntryByPath", err)
ctx.ServerError("GetTreeEntryByPath", err)
}
return
}
if lastCommit != ctx.Repo.CommitID {
files, err := ctx.Repo.Commit.GetFilesChangedSinceCommit(lastCommit)
if err != nil {
ctx.Handle(500, "GetFilesChangedSinceCommit", err)
ctx.ServerError("GetFilesChangedSinceCommit", err)
return
}
@ -269,7 +269,7 @@ func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bo
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(form.TreePath)
if err != nil {
if !git.IsErrNotExist(err) {
ctx.Handle(500, "GetTreeEntryByPath", err)
ctx.ServerError("GetTreeEntryByPath", err)
return
}
}
@ -422,7 +422,7 @@ func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) {
TreePath: ctx.Repo.TreePath,
Message: message,
}); err != nil {
ctx.Handle(500, "DeleteRepoFile", err)
ctx.ServerError("DeleteRepoFile", err)
return
}
@ -521,7 +521,7 @@ func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) {
break
}
c