routes/api: change status handle to new style

Also fixed one bug that did not catch team not found error.
This commit is contained in:
Unknwon
2018-12-01 21:41:30 -05:00
parent ce13fbb98a
commit 69c1cd3f38
5 changed files with 64 additions and 37 deletions

View File

@@ -5,6 +5,7 @@
package v1
import (
"net/http"
"strings"
"github.com/go-macaron/binding"
@@ -39,43 +40,34 @@ func repoAssignment() macaron.Handler {
} else {
owner, err = models.GetUserByName(userName)
if err != nil {
if errors.IsUserNotExist(err) {
c.Status(404)
} else {
c.Error(500, "GetUserByName", err)
}
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
return
}
}
c.Repo.Owner = owner
// Get repository.
repo, err := models.GetRepositoryByName(owner.ID, repoName)
if err != nil {
if errors.IsRepoNotExist(err) {
c.Status(404)
} else {
c.Error(500, "GetRepositoryByName", err)
}
c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
return
} else if err = repo.GetOwner(); err != nil {
c.Error(500, "GetOwner", err)
c.ServerError("GetOwner", err)
return
}
if c.IsTokenAuth && c.User.IsAdmin {
c.Repo.AccessMode = models.ACCESS_MODE_OWNER
} else {
mode, err := models.AccessLevel(c.User.ID, repo)
mode, err := models.AccessLevel(c.UserID(), repo)
if err != nil {
c.Error(500, "AccessLevel", err)
c.ServerError("AccessLevel", err)
return
}
c.Repo.AccessMode = mode
}
if !c.Repo.HasAccess() {
c.Status(404)
c.NotFound()
return
}
@@ -87,7 +79,7 @@ func repoAssignment() macaron.Handler {
func reqToken() macaron.Handler {
return func(c *context.Context) {
if !c.IsTokenAuth {
c.Error(401)
c.Error(http.StatusUnauthorized)
return
}
}
@@ -96,7 +88,7 @@ func reqToken() macaron.Handler {
func reqBasicAuth() macaron.Handler {
return func(c *context.Context) {
if !c.IsBasicAuth {
c.Error(401)
c.Error(http.StatusUnauthorized)
return
}
}
@@ -105,7 +97,7 @@ func reqBasicAuth() macaron.Handler {
func reqAdmin() macaron.Handler {
return func(c *context.Context) {
if !c.IsLogged || !c.User.IsAdmin {
c.Error(403)
c.Error(http.StatusForbidden)
return
}
}
@@ -114,7 +106,7 @@ func reqAdmin() macaron.Handler {
func reqRepoWriter() macaron.Handler {
return func(c *context.Context) {
if !c.Repo.IsWriter() {
c.Error(403)
c.Error(http.StatusForbidden)
return
}
}
@@ -138,11 +130,7 @@ func orgAssignment(args ...bool) macaron.Handler {
if assignOrg {
c.Org.Organization, err = models.GetUserByName(c.Params(":orgname"))
if err != nil {
if errors.IsUserNotExist(err) {
c.Status(404)
} else {
c.Error(500, "GetUserByName", err)
}
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
return
}
}
@@ -150,11 +138,7 @@ func orgAssignment(args ...bool) macaron.Handler {
if assignTeam {
c.Org.Team, err = models.GetTeamByID(c.ParamsInt64(":teamid"))
if err != nil {
if errors.IsUserNotExist(err) {
c.Status(404)
} else {
c.Error(500, "GetTeamById", err)
}
c.NotFoundOrServerError("GetTeamByID", errors.IsTeamNotExist, err)
return
}
}
@@ -163,7 +147,7 @@ func orgAssignment(args ...bool) macaron.Handler {
func mustEnableIssues(c *context.APIContext) {
if !c.Repo.Repository.EnableIssues || c.Repo.Repository.EnableExternalTracker {
c.Status(404)
c.NotFound()
return
}
}
@@ -326,7 +310,7 @@ func RegisterRoutes(m *macaron.Macaron) {
}, orgAssignment(true))
m.Any("/*", func(c *context.Context) {
c.Error(404)
c.NotFound()
})
m.Group("/admin", func() {