aboutsummaryrefslogtreecommitdiff
path: root/internal/route/api
diff options
context:
space:
mode:
authorᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-03-16 01:22:27 +0800
committerᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-03-16 01:22:27 +0800
commit9e9ca66467116e9079a2639c00e9e623aca23015 (patch)
treedacdef5392608ff7107e4dd498959d4899e13e54 /internal/route/api
parent82ff0c5852f29daa5f95d965fd50665581e7ea3c (diff)
refactor: unify error handling in routing layer
Diffstat (limited to 'internal/route/api')
-rw-r--r--internal/route/api/v1/admin/org.go6
-rw-r--r--internal/route/api/v1/admin/org_repo.go7
-rw-r--r--internal/route/api/v1/admin/org_team.go18
-rw-r--r--internal/route/api/v1/admin/repo.go8
-rw-r--r--internal/route/api/v1/admin/user.go21
-rw-r--r--internal/route/api/v1/api.go23
-rw-r--r--internal/route/api/v1/misc/markdown.go9
-rw-r--r--internal/route/api/v1/org/org.go22
-rw-r--r--internal/route/api/v1/org/team.go8
-rw-r--r--internal/route/api/v1/repo/branch.go21
-rw-r--r--internal/route/api/v1/repo/collaborators.go37
-rw-r--r--internal/route/api/v1/repo/commits.go19
-rw-r--r--internal/route/api/v1/repo/contents.go10
-rw-r--r--internal/route/api/v1/repo/file.go10
-rw-r--r--internal/route/api/v1/repo/hook.go51
-rw-r--r--internal/route/api/v1/repo/issue.go41
-rw-r--r--internal/route/api/v1/repo/issue_comment.go22
-rw-r--r--internal/route/api/v1/repo/issue_label.go31
-rw-r--r--internal/route/api/v1/repo/key.go41
-rw-r--r--internal/route/api/v1/repo/label.go15
-rw-r--r--internal/route/api/v1/repo/milestone.go14
-rw-r--r--internal/route/api/v1/repo/repo.go72
-rw-r--r--internal/route/api/v1/repo/tree.go6
-rw-r--r--internal/route/api/v1/user/app.go6
-rw-r--r--internal/route/api/v1/user/email.go15
-rw-r--r--internal/route/api/v1/user/follower.go8
-rw-r--r--internal/route/api/v1/user/key.go29
-rw-r--r--internal/route/api/v1/user/user.go3
28 files changed, 274 insertions, 299 deletions
diff --git a/internal/route/api/v1/admin/org.go b/internal/route/api/v1/admin/org.go
index 758f41e6..14350b1c 100644
--- a/internal/route/api/v1/admin/org.go
+++ b/internal/route/api/v1/admin/org.go
@@ -6,12 +6,12 @@ package admin
import (
api "github.com/gogs/go-gogs-client"
- org2 "gogs.io/gogs/internal/route/api/v1/org"
- user2 "gogs.io/gogs/internal/route/api/v1/user"
"gogs.io/gogs/internal/context"
+ "gogs.io/gogs/internal/route/api/v1/org"
+ "gogs.io/gogs/internal/route/api/v1/user"
)
func CreateOrg(c *context.APIContext, form api.CreateOrgOption) {
- org2.CreateOrgForUser(c, form, user2.GetUserByParams(c))
+ org.CreateOrgForUser(c, form, user.GetUserByParams(c))
}
diff --git a/internal/route/api/v1/admin/org_repo.go b/internal/route/api/v1/admin/org_repo.go
index b17b1462..63a638fa 100644
--- a/internal/route/api/v1/admin/org_repo.go
+++ b/internal/route/api/v1/admin/org_repo.go
@@ -7,13 +7,12 @@ package admin
import (
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/db/errors"
)
func GetRepositoryByParams(c *context.APIContext) *db.Repository {
repo, err := db.GetRepositoryByName(c.Org.Team.OrgID, c.Params(":reponame"))
if err != nil {
- c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
+ c.NotFoundOrError(err, "get repository by name")
return nil
}
return repo
@@ -25,7 +24,7 @@ func AddTeamRepository(c *context.APIContext) {
return
}
if err := c.Org.Team.AddRepository(repo); err != nil {
- c.ServerError("AddRepository", err)
+ c.Error(err, "add repository")
return
}
@@ -38,7 +37,7 @@ func RemoveTeamRepository(c *context.APIContext) {
return
}
if err := c.Org.Team.RemoveRepository(repo.ID); err != nil {
- c.ServerError("RemoveRepository", err)
+ c.Error(err, "remove repository")
return
}
diff --git a/internal/route/api/v1/admin/org_team.go b/internal/route/api/v1/admin/org_team.go
index e81b6b1b..953f0936 100644
--- a/internal/route/api/v1/admin/org_team.go
+++ b/internal/route/api/v1/admin/org_team.go
@@ -5,14 +5,14 @@
package admin
import (
- convert2 "gogs.io/gogs/internal/route/api/v1/convert"
- user2 "gogs.io/gogs/internal/route/api/v1/user"
"net/http"
api "github.com/gogs/go-gogs-client"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
+ "gogs.io/gogs/internal/route/api/v1/convert"
+ "gogs.io/gogs/internal/route/api/v1/user"
)
func CreateTeam(c *context.APIContext, form api.CreateTeamOption) {
@@ -24,23 +24,23 @@ func CreateTeam(c *context.APIContext, form api.CreateTeamOption) {
}
if err := db.NewTeam(team); err != nil {
if db.IsErrTeamAlreadyExist(err) {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
- c.ServerError("NewTeam", err)
+ c.Error(err, "new team")
}
return
}
- c.JSON(http.StatusCreated, convert2.ToTeam(team))
+ c.JSON(http.StatusCreated, convert.ToTeam(team))
}
func AddTeamMember(c *context.APIContext) {
- u := user2.GetUserByParams(c)
+ u := user.GetUserByParams(c)
if c.Written() {
return
}
if err := c.Org.Team.AddMember(u.ID); err != nil {
- c.ServerError("AddMember", err)
+ c.Error(err, "add member")
return
}
@@ -48,13 +48,13 @@ func AddTeamMember(c *context.APIContext) {
}
func RemoveTeamMember(c *context.APIContext) {
- u := user2.GetUserByParams(c)
+ u := user.GetUserByParams(c)
if c.Written() {
return
}
if err := c.Org.Team.RemoveMember(u.ID); err != nil {
- c.ServerError("RemoveMember", err)
+ c.Error(err, "remove member")
return
}
diff --git a/internal/route/api/v1/admin/repo.go b/internal/route/api/v1/admin/repo.go
index 68a26297..22931036 100644
--- a/internal/route/api/v1/admin/repo.go
+++ b/internal/route/api/v1/admin/repo.go
@@ -6,17 +6,17 @@ package admin
import (
api "github.com/gogs/go-gogs-client"
- repo2 "gogs.io/gogs/internal/route/api/v1/repo"
- user2 "gogs.io/gogs/internal/route/api/v1/user"
"gogs.io/gogs/internal/context"
+ "gogs.io/gogs/internal/route/api/v1/repo"
+ "gogs.io/gogs/internal/route/api/v1/user"
)
func CreateRepo(c *context.APIContext, form api.CreateRepoOption) {
- owner := user2.GetUserByParams(c)
+ owner := user.GetUserByParams(c)
if c.Written() {
return
}
- repo2.CreateUserRepo(c, owner, form)
+ repo.CreateUserRepo(c, owner, form)
}
diff --git a/internal/route/api/v1/admin/user.go b/internal/route/api/v1/admin/user.go
index 00b13bfd..c339edd2 100644
--- a/internal/route/api/v1/admin/user.go
+++ b/internal/route/api/v1/admin/user.go
@@ -7,9 +7,8 @@ package admin
import (
"net/http"
- log "unknwon.dev/clog/v2"
-
api "github.com/gogs/go-gogs-client"
+ log "unknwon.dev/clog/v2"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
@@ -27,9 +26,9 @@ func parseLoginSource(c *context.APIContext, u *db.User, sourceID int64, loginNa
source, err := db.GetLoginSourceByID(sourceID)
if err != nil {
if errors.IsLoginSourceNotExist(err) {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
- c.ServerError("GetLoginSourceByID", err)
+ c.Error(err, "get login source by ID")
}
return
}
@@ -59,9 +58,9 @@ func CreateUser(c *context.APIContext, form api.CreateUserOption) {
db.IsErrEmailAlreadyUsed(err) ||
db.IsErrNameReserved(err) ||
db.IsErrNamePatternNotAllowed(err) {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
- c.ServerError("CreateUser", err)
+ c.Error(err, "create user")
}
return
}
@@ -90,7 +89,7 @@ func EditUser(c *context.APIContext, form api.EditUserOption) {
u.Passwd = form.Password
var err error
if u.Salt, err = db.GetUserSalt(); err != nil {
- c.ServerError("GetUserSalt", err)
+ c.Error(err, "get user salt")
return
}
u.EncodePasswd()
@@ -119,9 +118,9 @@ func EditUser(c *context.APIContext, form api.EditUserOption) {
if err := db.UpdateUser(u); err != nil {
if db.IsErrEmailAlreadyUsed(err) {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
- c.ServerError("UpdateUser", err)
+ c.Error(err, "update user")
}
return
}
@@ -139,9 +138,9 @@ func DeleteUser(c *context.APIContext) {
if err := db.DeleteUser(u); err != nil {
if db.IsErrUserOwnRepos(err) ||
db.IsErrUserHasOrgs(err) {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
- c.ServerError("DeleteUser", err)
+ c.Error(err, "delete user")
}
return
}
diff --git a/internal/route/api/v1/api.go b/internal/route/api/v1/api.go
index 98394049..b56d640b 100644
--- a/internal/route/api/v1/api.go
+++ b/internal/route/api/v1/api.go
@@ -15,7 +15,6 @@ import (
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/db/errors"
"gogs.io/gogs/internal/form"
"gogs.io/gogs/internal/route/api/v1/admin"
"gogs.io/gogs/internal/route/api/v1/misc"
@@ -40,7 +39,7 @@ func repoAssignment() macaron.Handler {
} else {
owner, err = db.GetUserByName(username)
if err != nil {
- c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
+ c.NotFoundOrError(err, "get user by name")
return
}
}
@@ -48,10 +47,10 @@ func repoAssignment() macaron.Handler {
r, err := db.GetRepositoryByName(owner.ID, reponame)
if err != nil {
- c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
+ c.NotFoundOrError(err, "get repository by name")
return
} else if err = r.GetOwner(); err != nil {
- c.ServerError("GetOwner", err)
+ c.Error(err, "get owner")
return
}
@@ -60,7 +59,7 @@ func repoAssignment() macaron.Handler {
} else {
mode, err := db.UserAccessMode(c.UserID(), r)
if err != nil {
- c.ServerError("UserAccessMode", err)
+ c.Error(err, "get user access mode")
return
}
c.Repo.AccessMode = mode
@@ -94,7 +93,7 @@ func orgAssignment(args ...bool) macaron.Handler {
if assignOrg {
c.Org.Organization, err = db.GetUserByName(c.Params(":orgname"))
if err != nil {
- c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
+ c.NotFoundOrError(err, "get organization by name")
return
}
}
@@ -102,7 +101,7 @@ func orgAssignment(args ...bool) macaron.Handler {
if assignTeam {
c.Org.Team, err = db.GetTeamByID(c.ParamsInt64(":teamid"))
if err != nil {
- c.NotFoundOrServerError("GetTeamByID", errors.IsTeamNotExist, err)
+ c.NotFoundOrError(err, "get team by ID")
return
}
}
@@ -113,7 +112,7 @@ func orgAssignment(args ...bool) macaron.Handler {
func reqToken() macaron.Handler {
return func(c *context.Context) {
if !c.IsTokenAuth {
- c.Error(http.StatusUnauthorized)
+ c.Status(http.StatusUnauthorized)
return
}
}
@@ -123,7 +122,7 @@ func reqToken() macaron.Handler {
func reqBasicAuth() macaron.Handler {
return func(c *context.Context) {
if !c.IsBasicAuth {
- c.Error(http.StatusUnauthorized)
+ c.Status(http.StatusUnauthorized)
return
}
}
@@ -133,7 +132,7 @@ func reqBasicAuth() macaron.Handler {
func reqAdmin() macaron.Handler {
return func(c *context.Context) {
if !c.IsLogged || !c.User.IsAdmin {
- c.Error(http.StatusForbidden)
+ c.Status(http.StatusForbidden)
return
}
}
@@ -143,7 +142,7 @@ func reqAdmin() macaron.Handler {
func reqRepoWriter() macaron.Handler {
return func(c *context.Context) {
if !c.Repo.IsWriter() {
- c.Error(http.StatusForbidden)
+ c.Status(http.StatusForbidden)
return
}
}
@@ -153,7 +152,7 @@ func reqRepoWriter() macaron.Handler {
func reqRepoAdmin() macaron.Handler {
return func(c *context.Context) {
if !c.Repo.IsAdmin() {
- c.Error(http.StatusForbidden)
+ c.Status(http.StatusForbidden)
return
}
}
diff --git a/internal/route/api/v1/misc/markdown.go b/internal/route/api/v1/misc/markdown.go
index 10889d3b..be92a164 100644
--- a/internal/route/api/v1/misc/markdown.go
+++ b/internal/route/api/v1/misc/markdown.go
@@ -5,8 +5,6 @@
package misc
import (
- "net/http"
-
api "github.com/gogs/go-gogs-client"
"gogs.io/gogs/internal/context"
@@ -14,11 +12,6 @@ import (
)
func Markdown(c *context.APIContext, form api.MarkdownOption) {
- if c.HasApiError() {
- c.Error(http.StatusUnprocessableEntity, "", c.GetErrMsg())
- return
- }
-
if len(form.Text) == 0 {
_, _ = c.Write([]byte(""))
return
@@ -30,7 +23,7 @@ func Markdown(c *context.APIContext, form api.MarkdownOption) {
func MarkdownRaw(c *context.APIContext) {
body, err := c.Req.Body().Bytes()
if err != nil {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.Error(err, "read body")
return
}
_, _ = c.Write(markup.SanitizeBytes(markup.RawMarkdown(body, "")))
diff --git a/internal/route/api/v1/org/org.go b/internal/route/api/v1/org/org.go
index dbb3e4dd..0bcefbe4 100644
--- a/internal/route/api/v1/org/org.go
+++ b/internal/route/api/v1/org/org.go
@@ -5,14 +5,14 @@
package org
import (
- convert2 "gogs.io/gogs/internal/route/api/v1/convert"
- user2 "gogs.io/gogs/internal/route/api/v1/user"
"net/http"
api "github.com/gogs/go-gogs-client"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
+ "gogs.io/gogs/internal/route/api/v1/convert"
+ "gogs.io/gogs/internal/route/api/v1/user"
)
func CreateOrgForUser(c *context.APIContext, apiForm api.CreateOrgOption, user *db.User) {
@@ -33,25 +33,25 @@ func CreateOrgForUser(c *context.APIContext, apiForm api.CreateOrgOption, user *
if db.IsErrUserAlreadyExist(err) ||
db.IsErrNameReserved(err) ||
db.IsErrNamePatternNotAllowed(err) {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
- c.ServerError("CreateOrganization", err)
+ c.Error(err, "create organization")
}
return
}
- c.JSON(201, convert2.ToOrganization(org))
+ c.JSON(201, convert.ToOrganization(org))
}
func listUserOrgs(c *context.APIContext, u *db.User, all bool) {
if err := u.GetOrganizations(all); err != nil {
- c.ServerError("GetOrganizations", err)
+ c.Error(err, "get organization")
return
}
apiOrgs := make([]*api.Organization, len(u.Orgs))
for i := range u.Orgs {
- apiOrgs[i] = convert2.ToOrganization(u.Orgs[i])
+ apiOrgs[i] = convert.ToOrganization(u.Orgs[i])
}
c.JSONSuccess(&apiOrgs)
}
@@ -65,7 +65,7 @@ func CreateMyOrg(c *context.APIContext, apiForm api.CreateOrgOption) {
}
func ListUserOrgs(c *context.APIContext) {
- u := user2.GetUserByParams(c)
+ u := user.GetUserByParams(c)
if c.Written() {
return
}
@@ -73,7 +73,7 @@ func ListUserOrgs(c *context.APIContext) {
}
func Get(c *context.APIContext) {
- c.JSONSuccess(convert2.ToOrganization(c.Org.Organization))
+ c.JSONSuccess(convert.ToOrganization(c.Org.Organization))
}
func Edit(c *context.APIContext, form api.EditOrgOption) {
@@ -88,9 +88,9 @@ func Edit(c *context.APIContext, form api.EditOrgOption) {
org.Website = form.Website
org.Location = form.Location
if err := db.UpdateUser(org); err != nil {
- c.ServerError("UpdateUser", err)
+ c.Error(err, "update user")
return
}
- c.JSONSuccess(convert2.ToOrganization(org))
+ c.JSONSuccess(convert.ToOrganization(org))
}
diff --git a/internal/route/api/v1/org/team.go b/internal/route/api/v1/org/team.go
index 528e6183..75a6ae11 100644
--- a/internal/route/api/v1/org/team.go
+++ b/internal/route/api/v1/org/team.go
@@ -6,21 +6,21 @@ package org
import (
api "github.com/gogs/go-gogs-client"
- convert2 "gogs.io/gogs/internal/route/api/v1/convert"
"gogs.io/gogs/internal/context"
+ "gogs.io/gogs/internal/route/api/v1/convert"
)
func ListTeams(c *context.APIContext) {
org := c.Org.Organization
if err := org.GetTeams(); err != nil {
- c.Error(500, "GetTeams", err)
+ c.Error(err, "get teams")
return
}
apiTeams := make([]*api.Team, len(org.Teams))
for i := range org.Teams {
- apiTeams[i] = convert2.ToTeam(org.Teams[i])
+ apiTeams[i] = convert.ToTeam(org.Teams[i])
}
- c.JSON(200, apiTeams)
+ c.JSONSuccess(apiTeams)
}
diff --git a/internal/route/api/v1/repo/branch.go b/internal/route/api/v1/repo/branch.go
index b90d1e24..3ddf62cc 100644
--- a/internal/route/api/v1/repo/branch.go
+++ b/internal/route/api/v1/repo/branch.go
@@ -6,38 +6,33 @@ package repo
import (
api "github.com/gogs/go-gogs-client"
- convert2 "gogs.io/gogs/internal/route/api/v1/convert"
"gogs.io/gogs/internal/context"
- "gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/route/api/v1/convert"
)
// https://github.com/gogs/go-gogs-client/wiki/Repositories#get-branch
func GetBranch(c *context.APIContext) {
branch, err := c.Repo.Repository.GetBranch(c.Params("*"))
if err != nil {
- if errors.IsErrBranchNotExist(err) {
- c.Error(404, "GetBranch", err)
- } else {
- c.Error(500, "GetBranch", err)
- }
+ c.NotFoundOrError(err, "get branch")
return
}
commit, err := branch.GetCommit()
if err != nil {
- c.Error(500, "GetCommit", err)
+ c.Error(err, "get commit")
return
}
- c.JSON(200, convert2.ToBranch(branch, commit))
+ c.JSONSuccess( convert.ToBranch(branch, commit))
}
// https://github.com/gogs/go-gogs-client/wiki/Repositories#list-branches
func ListBranches(c *context.APIContext) {
branches, err := c.Repo.Repository.GetBranches()
if err != nil {
- c.Error(500, "GetBranches", err)
+ c.Error(err, "get branches")
return
}
@@ -45,11 +40,11 @@ func ListBranches(c *context.APIContext) {
for i := range branches {
commit, err := branches[i].GetCommit()
if err != nil {
- c.Error(500, "GetCommit", err)
+ c.Error(err, "get commit")
return
}
- apiBranches[i] = convert2.ToBranch(branches[i], commit)
+ apiBranches[i] = convert.ToBranch(branches[i], commit)
}
- c.JSON(200, &apiBranches)
+ c.JSONSuccess( &apiBranches)
}
diff --git a/internal/route/api/v1/repo/collaborators.go b/internal/route/api/v1/repo/collaborators.go
index e8f74848..c9d2ff30 100644
--- a/internal/route/api/v1/repo/collaborators.go
+++ b/internal/route/api/v1/repo/collaborators.go
@@ -5,17 +5,18 @@
package repo
import (
+ "net/http"
+
api "github.com/gogs/go-gogs-client"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/db/errors"
)
func ListCollaborators(c *context.APIContext) {
collaborators, err := c.Repo.Repository.GetCollaborators()
if err != nil {
- c.ServerError("GetCollaborators", err)
+ c.Error(err, "get collaborators")
return
}
@@ -29,62 +30,62 @@ func ListCollaborators(c *context.APIContext) {
func AddCollaborator(c *context.APIContext, form api.AddCollaboratorOption) {
collaborator, err := db.GetUserByName(c.Params(":collaborator"))
if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(422, "", err)
+ if db.IsErrUserNotExist(err) {
+ c.Status(http.StatusUnprocessableEntity)
} else {
- c.Error(500, "GetUserByName", err)
+ c.Error(err, "get user by name")
}
return
}
if err := c.Repo.Repository.AddCollaborator(collaborator); err != nil {
- c.Error(500, "AddCollaborator", err)
+ c.Error(err, "add collaborator")
return
}
if form.Permission != nil {
if err := c.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, db.ParseAccessMode(*form.Permission)); err != nil {
- c.Error(500, "ChangeCollaborationAccessMode", err)
+ c.Error(err, "change collaboration access mode")
return
}
}
- c.Status(204)
+ c.NoContent()
}
func IsCollaborator(c *context.APIContext) {
collaborator, err := db.GetUserByName(c.Params(":collaborator"))
if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(422, "", err)
+ if db.IsErrUserNotExist(err) {
+ c.Status(http.StatusUnprocessableEntity)
} else {
- c.Error(500, "GetUserByName", err)
+ c.Error(err, "get user by name")
}
return
}
if !c.Repo.Repository.IsCollaborator(collaborator.ID) {
- c.Status(404)
+ c.NotFound()
} else {
- c.Status(204)
+ c.NoContent()
}
}
func DeleteCollaborator(c *context.APIContext) {
collaborator, err := db.GetUserByName(c.Params(":collaborator"))
if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(422, "", err)
+ if db.IsErrUserNotExist(err) {
+ c.Status(http.StatusUnprocessableEntity)
} else {
- c.Error(500, "GetUserByName", err)
+ c.Error(err, "get user by name")
}
return
}
if err := c.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil {
- c.Error(500, "DeleteCollaboration", err)
+ c.Error(err, "delete collaboration")
return
}
- c.Status(204)
+ c.NoContent()
}
diff --git a/internal/route/api/v1/repo/commits.go b/internal/route/api/v1/repo/commits.go
index b3903053..ea6590d7 100644
--- a/internal/route/api/v1/repo/commits.go
+++ b/internal/route/api/v1/repo/commits.go
@@ -15,7 +15,6 @@ import (
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/db/errors"
"gogs.io/gogs/internal/gitutil"
)
@@ -28,20 +27,20 @@ func GetSingleCommit(c *context.APIContext) {
gitRepo, err := git.Open(c.Repo.Repository.RepoPath())
if err != nil {
- c.ServerError("open repository", err)
+ c.Error(err, "open repository")
return
}
commit, err := gitRepo.CatFileCommit(c.Params(":sha"))
if err != nil {
- c.NotFoundOrServerError("get commit", gitutil.IsErrRevisionNotExist, err)
+ c.NotFoundOrError(gitutil.NewError(err), "get commit")
return
}
// Retrieve author and committer information
var apiAuthor, apiCommitter *api.User
author, err := db.GetUserByEmail(commit.Author.Email)
- if err != nil && !errors.IsUserNotExist(err) {
- c.ServerError("Get user by author email", err)
+ if err != nil && !db.IsErrUserNotExist(err) {
+ c.Error(err, "get user by author email")
return
} else if err == nil {
apiAuthor = author.APIFormat()
@@ -51,8 +50,8 @@ func GetSingleCommit(c *context.APIContext) {
apiCommitter = apiAuthor
} else {
committer, err := db.GetUserByEmail(commit.Committer.Email)
- if err != nil && !errors.IsUserNotExist(err) {
- c.ServerError("Get user by committer email", err)
+ if err != nil && !db.IsErrUserNotExist(err) {
+ c.Error(err, "get user by committer email")
return
} else if err == nil {
apiCommitter = committer.APIFormat()
@@ -102,7 +101,7 @@ func GetSingleCommit(c *context.APIContext) {
func GetReferenceSHA(c *context.APIContext) {
gitRepo, err := git.Open(c.Repo.Repository.RepoPath())
if err != nil {
- c.ServerError("open repository", err)
+ c.Error(err, "open repository")
return
}
@@ -132,8 +131,8 @@ func GetReferenceSHA(c *context.APIContext) {
sha, err = gitRepo.TagCommitID(ref)
}
if err != nil {
- c.NotFoundOrServerError("get reference commit ID", gitutil.IsErrRevisionNotExist, err)
+ c.NotFoundOrError(gitutil.NewError(err), "get reference commit ID")
return
}
- c.PlainText(http.StatusOK, []byte(sha))
+ c.PlainText(http.StatusOK, sha)
}
diff --git a/internal/route/api/v1/repo/contents.go b/internal/route/api/v1/repo/contents.go
index e5a9277d..463bbb22 100644
--- a/internal/route/api/v1/repo/contents.go
+++ b/internal/route/api/v1/repo/contents.go
@@ -19,7 +19,7 @@ import (
func GetContents(c *context.APIContext) {
gitRepo, err := git.Open(c.Repo.Repository.RepoPath())
if err != nil {
- c.ServerError("open repository", err)
+ c.Error(err, "open repository")
return
}
@@ -30,14 +30,14 @@ func GetContents(c *context.APIContext) {
commit, err := gitRepo.CatFileCommit(ref)
if err != nil {
- c.NotFoundOrServerError("get commit", gitutil.IsErrRevisionNotExist, err)
+ c.NotFoundOrError(gitutil.NewError(err), "get commit")
return
}
treePath := c.Params("*")
entry, err := commit.TreeEntry(treePath)
if err != nil {
- c.NotFoundOrServerError("get tree entry", gitutil.IsErrRevisionNotExist, err)
+ c.NotFoundOrError(gitutil.NewError(err), "get tree entry")
return
}
@@ -137,13 +137,13 @@ func GetContents(c *context.APIContext) {
// The entry is a directory
dir, err := gitRepo.LsTree(entry.ID().String())
if err != nil {
- c.NotFoundOrServerError("get tree", gitutil.IsErrRevisionNotExist, err)
+ c.NotFoundOrError(gitutil.NewError(err), "get tree")
return
}
entries, err := dir.Entries()
if err != nil {
- c.NotFoundOrServerError("list entries", gitutil.IsErrRevisionNotExist, err)
+ c.NotFoundOrError(gitutil.NewError(err), "list entries")
return
}
diff --git a/internal/route/api/v1/repo/file.go b/internal/route/api/v1/repo/file.go
index 414d0285..999a2a47 100644
--- a/internal/route/api/v1/repo/file.go
+++ b/internal/route/api/v1/repo/file.go
@@ -26,11 +26,11 @@ func GetRawFile(c *context.APIContext) {
blob, err := c.Repo.Commit.Blob(c.Repo.TreePath)
if err != nil {
- c.NotFoundOrServerError("get blob", gitutil.IsErrRevisionNotExist, err)
+ c.NotFoundOrError(gitutil.NewError(err), "get blob")
return
}
if err = repo.ServeBlob(c.Context, blob); err != nil {
- c.ServerError("ServeBlob", err)
+ c.Error(err, "serve blob")
}
}
@@ -38,7 +38,7 @@ func GetArchive(c *context.APIContext) {
repoPath := db.RepoPath(c.Params(":username"), c.Params(":reponame"))
gitRepo, err := git.Open(repoPath)
if err != nil {
- c.ServerError("open repository", err)
+ c.Error(err, "open repository")
return
}
c.Repo.GitRepo = gitRepo
@@ -49,14 +49,14 @@ func GetArchive(c *context.APIContext) {
func GetEditorconfig(c *context.APIContext) {
ec, err := c.Repo.Editorconfig()
if err != nil {
- c.NotFoundOrServerError("get .editorconfig", gitutil.IsErrRevisionNotExist, err)
+ c.NotFoundOrError(gitutil.NewError(err), "get .editorconfig")
return
}
fileName := c.Params("filename")
def, err := ec.GetDefinitionForFilename(fileName)
if err != nil {
- c.ServerError("get definition for filename", err)
+ c.Error(err, "get definition for filename")
return
}
if def == nil {
diff --git a/internal/route/api/v1/repo/hook.go b/internal/route/api/v1/repo/hook.go
index 060d2049..d5730165 100644
--- a/internal/route/api/v1/repo/hook.go
+++ b/internal/route/api/v1/repo/hook.go
@@ -5,46 +5,47 @@
package repo
import (
- "github.com/json-iterator/go"
- "github.com/unknwon/com"
- convert2 "gogs.io/gogs/internal/route/api/v1/convert"
+ "net/http"
api "github.com/gogs/go-gogs-client"
+ "github.com/json-iterator/go"
+ "github.com/pkg/errors"
+ "github.com/unknwon/com"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/route/api/v1/convert"
)
// https://github.com/gogs/go-gogs-client/wiki/Repositories#list-hooks
func ListHooks(c *context.APIContext) {
hooks, err := db.GetWebhooksByRepoID(c.Repo.Repository.ID)
if err != nil {
- c.Error(500, "GetWebhooksByRepoID", err)
+ c.Errorf(err, "get webhooks by repository ID")
return
}
apiHooks := make([]*api.Hook, len(hooks))
for i := range hooks {
- apiHooks[i] = convert2.ToHook(c.Repo.RepoLink, hooks[i])
+ apiHooks[i] = convert.ToHook(c.Repo.RepoLink, hooks[i])
}
- c.JSON(200, &apiHooks)
+ c.JSONSuccess(&apiHooks)
}
// https://github.com/gogs/go-gogs-client/wiki/Repositories#create-a-hook
func CreateHook(c *context.APIContext, form api.CreateHookOption) {
if !db.IsValidHookTaskType(form.Type) {
- c.Error(422, "", "Invalid hook type")
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Invalid hook type."))
return
}
for _, name := range []string{"url", "content_type"} {
if _, ok := form.Config[name]; !ok {
- c.Error(422, "", "Missing config option: "+name)
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Missing config option: "+name))
return
}
}
if !db.IsValidHookContentType(form.Config["content_type"]) {
- c.Error(422, "", "Invalid content type")
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Invalid content type."))
return
}
@@ -75,7 +76,7 @@ func CreateHook(c *context.APIContext, form api.CreateHookOption) {
if w.HookTaskType == db.SLACK {
channel, ok := form.Config["channel"]
if !ok {
- c.Error(422, "", "Missing config option: channel")
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Missing config option: channel"))
return
}
meta, err := jsoniter.Marshal(&db.SlackMeta{
@@ -85,32 +86,28 @@ func CreateHook(c *context.APIContext, form api.CreateHookOption) {
Color: form.Config["color"],
})
if err != nil {
- c.Error(500, "slack: JSON marshal failed", err)
+ c.Errorf(err, "marshal JSON")
return
}
w.Meta = string(meta)
}
if err := w.UpdateEvent(); err != nil {
- c.Error(500, "UpdateEvent", err)
+ c.Errorf(err, "update event")
return
} else if err := db.CreateWebhook(w); err != nil {
- c.Error(500, "CreateWebhook", err)
+ c.Errorf(err, "create webhook")
return
}
- c.JSON(201, convert2.ToHook(c.Repo.RepoLink, w))
+ c.JSON(http.StatusCreated, convert.ToHook(c.Repo.RepoLink, w))
}
// https://github.com/gogs/go-gogs-client/wiki/Repositories#edit-a-hook
func EditHook(c *context.APIContext, form api.EditHookOption) {
w, err := db.GetWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
if err != nil {
- if errors.IsWebhookNotExist(err) {
- c.Status(404)
- } else {
- c.Error(500, "GetWebhookOfRepoByID", err)
- }
+ c.NotFoundOrError(err, "get webhook of repository by ID")
return
}
@@ -120,7 +117,7 @@ func EditHook(c *context.APIContext, form api.EditHookOption) {
}
if ct, ok := form.Config["content_type"]; ok {
if !db.IsValidHookContentType(ct) {
- c.Error(422, "", "Invalid content type")
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Invalid content type."))
return
}
w.ContentType = db.ToHookContentType(ct)
@@ -135,7 +132,7 @@ func EditHook(c *context.APIContext, form api.EditHookOption) {
Color: form.Config["color"],
})
if err != nil {
- c.Error(500, "slack: JSON marshal failed", err)
+ c.Errorf(err, "marshal JSON")
return
}
w.Meta = string(meta)
@@ -159,7 +156,7 @@ func EditHook(c *context.APIContext, form api.EditHookOption) {
w.PullRequest = com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_PULL_REQUEST))
w.Release = com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_RELEASE))
if err = w.UpdateEvent(); err != nil {
- c.Error(500, "UpdateEvent", err)
+ c.Errorf(err, "update event")
return
}
@@ -168,18 +165,18 @@ func EditHook(c *context.APIContext, form api.EditHookOption) {
}
if err := db.UpdateWebhook(w); err != nil {
- c.Error(500, "UpdateWebhook", err)
+ c.Errorf(err, "update webhook")
return
}
- c.JSON(200, convert2.ToHook(c.Repo.RepoLink, w))
+ c.JSONSuccess(convert.ToHook(c.Repo.RepoLink, w))
}
func DeleteHook(c *context.APIContext) {
if err := db.DeleteWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
- c.Error(500, "DeleteWebhookByRepoID", err)
+ c.Errorf(err, "delete webhook of repository by ID")
return
}
- c.Status(204)
+ c.NoContent()
}
diff --git a/internal/route/api/v1/repo/issue.go b/internal/route/api/v1/repo/issue.go
index 39977f91..8a9f4b77 100644
--- a/internal/route/api/v1/repo/issue.go
+++ b/internal/route/api/v1/repo/issue.go
@@ -11,22 +11,21 @@ import (
api "github.com/gogs/go-gogs-client"
+ "gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/db/errors"
- "gogs.io/gogs/internal/conf"
)
func listIssues(c *context.APIContext, opts *db.IssuesOptions) {
issues, err := db.Issues(opts)
if err != nil {
- c.ServerError("Issues", err)
+ c.Error(err, "list issues")
return
}
count, err := db.IssuesCount(opts)
if err != nil {
- c.ServerError("IssuesCount", err)
+ c.Error(err, "count issues")
return
}
@@ -34,7 +33,7 @@ func listIssues(c *context.APIContext, opts *db.IssuesOptions) {
apiIssues := make([]*api.Issue, len(issues))
for i := range issues {
if err = issues[i].LoadAttributes(); err != nil {
- c.ServerError("LoadAttributes", err)
+ c.Error(err, "load attributes")
return
}
apiIssues[i] = issues[i].APIFormat()
@@ -67,7 +66,7 @@ func ListIssues(c *context.APIContext) {
func GetIssue(c *context.APIContext) {
issue, err := db.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
if err != nil {
- c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
+ c.NotFoundOrError(err, "get issue by index")
return
}
c.JSONSuccess(issue.APIFormat())
@@ -86,10 +85,10 @@ func CreateIssue(c *context.APIContext, form api.CreateIssueOption) {
if len(form.Assignee) > 0 {
assignee, err := db.GetUserByName(form.Assignee)
if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("assignee does not exist: [name: %s]", form.Assignee))
+ if db.IsErrUserNotExist(err) {
+ c.ErrorStatus(http.StatusUnprocessableEntity, fmt.Errorf("assignee does not exist: [name: %s]", form.Assignee))
} else {
- c.ServerError("GetUserByName", err)
+ c.Error(err, "get user by name")
}
return
}
@@ -101,13 +100,13 @@ func CreateIssue(c *context.APIContext, form api.CreateIssueOption) {
}
if err := db.NewIssue(c.Repo.Repository, issue, form.Labels, nil); err != nil {
- c.ServerError("NewIssue", err)
+ c.Error(err, "new issue")
return
}
if form.Closed {
if err := issue.ChangeStatus(c.User, c.Repo.Repository, true); err != nil {
- c.ServerError("ChangeStatus", err)
+ c.Error(err, "change status to closed")
return
}
}
@@ -116,7 +115,7 @@ func CreateIssue(c *context.APIContext, form api.CreateIssueOption) {
var err error
issue, err = db.GetIssueByID(issue.ID)
if err != nil {
- c.ServerError("GetIssueByID", err)
+ c.Error(err, "get issue by ID")
return
}
c.JSON(http.StatusCreated, issue.APIFormat())
@@ -125,7 +124,7 @@ func CreateIssue(c *context.APIContext, form api.CreateIssueOption) {
func EditIssue(c *context.APIContext, form api.EditIssueOption) {
issue, err := db.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
if err != nil {
- c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
+ c.NotFoundOrError(err, "get issue by index")
return
}
@@ -148,10 +147,10 @@ func EditIssue(c *context.APIContext, form api.EditIssueOption) {
} else {
assignee, err := db.GetUserByName(*form.Assignee)
if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("assignee does not exist: [name: %s]", *form.Assignee))
+ if db.IsErrUserNotExist(err) {
+ c.ErrorStatus(http.StatusUnprocessableEntity, fmt.Errorf("assignee does not exist: [name: %s]", *form.Assignee))
} else {
- c.ServerError("GetUserByName", err)
+ c.Error(err, "get user by name")
}
return
}
@@ -159,7 +158,7 @@ func EditIssue(c *context.APIContext, form api.EditIssueOption) {
}
if err = db.UpdateIssueUserByAssignee(issue); err != nil {
- c.ServerError("UpdateIssueUserByAssignee", err)
+ c.Error(err, "update issue user by assignee")
return
}
}
@@ -168,18 +167,18 @@ func EditIssue(c *context.APIContext, form api.EditIssueOption) {
oldMilestoneID := issue.MilestoneID
issue.MilestoneID = *form.Milestone
if err = db.ChangeMilestoneAssign(c.User, issue, oldMilestoneID); err != nil {
- c.ServerError("ChangeMilestoneAssign", err)
+ c.Error(err, "change milestone assign")
return
}
}
if err = db.UpdateIssue(issue); err != nil {
- c.ServerError("UpdateIssue", err)
+ c.Error(err, "update issue")
return
}
if form.State != nil {
if err = issue.ChangeStatus(c.User, c.Repo.Repository, api.STATE_CLOSED == api.StateType(*form.State)); err != nil {
- c.ServerError("ChangeStatus", err)
+ c.Error(err, "change status")
return
}
}
@@ -187,7 +186,7 @@ func EditIssue(c *context.APIContext, form api.EditIssueOption) {
// Refetch from database to assign some automatic values
issue, err = db.GetIssueByID(issue.ID)
if err != nil {
- c.ServerError("GetIssueByID", err)
+ c.Error(err, "get issue by ID")
return
}
c.JSON(http.StatusCreated, issue.APIFormat())
diff --git a/internal/route/api/v1/repo/issue_comment.go b/internal/route/api/v1/repo/issue_comment.go
index 4f86e13b..a309cc80 100644
--- a/internal/route/api/v1/repo/issue_comment.go
+++ b/internal/route/api/v1/repo/issue_comment.go
@@ -19,7 +19,7 @@ func ListIssueComments(c *context.APIContext) {
var err error
since, err = time.Parse(time.RFC3339, c.Query("since"))
if err != nil {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
return
}
}
@@ -27,13 +27,13 @@ func ListIssueComments(c *context.APIContext) {
// comments,err:=db.GetCommentsByIssueIDSince(, since)
issue, err := db.GetRawIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
if err != nil {
- c.ServerError("GetRawIssueByIndex", err)
+ c.Error(err, "get raw issue by index")
return
}
comments, err := db.GetCommentsByIssueIDSince(issue.ID, since.Unix())
if err != nil {
- c.ServerError("GetCommentsByIssueIDSince", err)
+ c.Error(err, "get comments by issue ID")
return
}
@@ -50,14 +50,14 @@ func ListRepoIssueComments(c *context.APIContext) {
var err error
since, err = time.Parse(time.RFC3339, c.Query("since"))
if err != nil {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
return
}
}
comments, err := db.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix())
if err != nil {
- c.ServerError("GetCommentsByRepoIDSince", err)
+ c.Error(err, "get comments by repository ID")
return
}
@@ -71,13 +71,13 @@ func ListRepoIssueComments(c *context.APIContext) {
func CreateIssueComment(c *context.APIContext, form api.CreateIssueCommentOption) {
issue, err := db.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
if err != nil {
- c.ServerError("GetIssueByIndex", err)
+ c.Error(err, "get issue by index")
return
}
comment, err := db.CreateIssueComment(c.User, c.Repo.Repository, issue, form.Body, nil)
if err != nil {
- c.ServerError("CreateIssueComment", err)
+ c.Error(err, "create issue comment")
return
}
@@ -87,7 +87,7 @@ func CreateIssueComment(c *context.APIContext, form api.CreateIssueCommentOption
func EditIssueComment(c *context.APIContext, form api.EditIssueCommentOption) {
comment, err := db.GetCommentByID(c.ParamsInt64(":id"))
if err != nil {
- c.NotFoundOrServerError("GetCommentByID", db.IsErrCommentNotExist, err)
+ c.NotFoundOrError(err, "get comment by ID")
return
}
@@ -102,7 +102,7 @@ func EditIssueComment(c *context.APIContext, form api.EditIssueCommentOption) {
oldContent := comment.Content
comment.Content = form.Body
if err := db.UpdateComment(c.User, comment, oldContent); err != nil {
- c.ServerError("UpdateComment", err)
+ c.Error(err, "update comment")
return
}
c.JSONSuccess(comment.APIFormat())
@@ -111,7 +111,7 @@ func EditIssueComment(c *context.APIContext, form api.EditIssueCommentOption) {
func DeleteIssueComment(c *context.APIContext) {
comment, err := db.GetCommentByID(c.ParamsInt64(":id"))
if err != nil {
- c.NotFoundOrServerError("GetCommentByID", db.IsErrCommentNotExist, err)
+ c.NotFoundOrError(err, "get comment by ID")
return
}
@@ -124,7 +124,7 @@ func DeleteIssueComment(c *context.APIContext) {
}
if err = db.DeleteCommentByID(c.User, comment.ID); err != nil {
- c.ServerError("DeleteCommentByID", err)
+ c.Error(err, "delete comment by ID")
return
}
c.NoContent()
diff --git a/internal/route/api/v1/repo/issue_label.go b/internal/route/api/v1/repo/issue_label.go
index 7c8b7982..92fff47d 100644
--- a/internal/route/api/v1/repo/issue_label.go
+++ b/internal/route/api/v1/repo/issue_label.go
@@ -11,13 +11,12 @@ import (
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/db/errors"
)
func ListIssueLabels(c *context.APIContext) {
issue, err := db.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
if err != nil {
- c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
+ c.NotFoundOrError(err, "get issue by index")
return
}
@@ -31,24 +30,24 @@ func ListIssueLabels(c *context.APIContext) {
func AddIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
issue, err := db.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
if err != nil {
- c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
+ c.NotFoundOrError(err, "get issue by index")
return
}
labels, err := db.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
if err != nil {
- c.ServerError("GetLabelsInRepoByIDs", err)
+ c.Error(err, "get labels in repository by IDs")
return
}
if err = issue.AddLabels(c.User, labels); err != nil {
- c.ServerError("AddLabels", err)
+ c.Error(err, "add labels")
return
}
labels, err = db.GetLabelsByIssueID(issue.ID)
if err != nil {
- c.ServerError("GetLabelsByIssueID", err)
+ c.Error(err, "get labels by issue ID")
return
}
@@ -62,22 +61,22 @@ func AddIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
func DeleteIssueLabel(c *context.APIContext) {
issue, err := db.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
if err != nil {
- c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
+ c.NotFoundOrError(err, "get issue by index")
return
}
label, err := db.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
if err != nil {
if db.IsErrLabelNotExist(err) {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
- c.ServerError("GetLabelInRepoByID", err)
+ c.Error(err, "get label of repository by ID")
}
return
}
if err := db.DeleteIssueLabel(issue, label); err != nil {
- c.ServerError("DeleteIssueLabel", err)
+ c.Error(err, "delete issue label")
return
}
@@ -87,24 +86,24 @@ func DeleteIssueLabel(c *context.APIContext) {
func ReplaceIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
issue, err := db.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
if err != nil {
- c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
+ c.NotFoundOrError(err, "get issue by index")
return
}
labels, err := db.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
if err != nil {
- c.ServerError("GetLabelsInRepoByIDs", err)
+ c.Error(err, "get labels in repository by IDs")
return
}
if err := issue.ReplaceLabels(labels); err != nil {
- c.ServerError("ReplaceLabels", err)
+ c.Error(err, "replace labels")
return
}
labels, err = db.GetLabelsByIssueID(issue.ID)
if err != nil {
- c.ServerError("GetLabelsByIssueID", err)
+ c.Error(err, "get labels by issue ID")
return
}
@@ -118,12 +117,12 @@ func ReplaceIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
func ClearIssueLabels(c *context.APIContext) {
issue, err := db.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
if err != nil {
- c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
+ c.NotFoundOrError(err, "get issue by index")
return
}
if err := issue.ClearLabels(c.User); err != nil {
- c.ServerError("ClearLabels", err)
+ c.Error(err, "clear labels")
return
}
diff --git a/internal/route/api/v1/repo/key.go b/internal/route/api/v1/repo/key.go
index d8012933..38c037b6 100644
--- a/internal/route/api/v1/repo/key.go
+++ b/internal/route/api/v1/repo/key.go
@@ -5,14 +5,15 @@
package repo
import (
- "fmt"
- convert2 "gogs.io/gogs/internal/route/api/v1/convert"
+ "net/http"
api "github.com/gogs/go-gogs-client"
+ "github.com/pkg/errors"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
+ "gogs.io/gogs/internal/route/api/v1/convert"
)
func composeDeployKeysAPILink(repoPath string) string {
@@ -23,7 +24,7 @@ func composeDeployKeysAPILink(repoPath string) string {
func ListDeployKeys(c *context.APIContext) {
keys, err := db.ListDeployKeys(c.Repo.Repository.ID)
if err != nil {
- c.Error(500, "ListDeployKeys", err)
+ c.Error(err, "list deploy keys")
return
}
@@ -31,52 +32,48 @@ func ListDeployKeys(c *context.APIContext) {
apiKeys := make([]*api.DeployKey, len(keys))
for i := range keys {
if err = keys[i].GetContent(); err != nil {
- c.Error(500, "GetContent", err)
+ c.Error(err, "get content")
return
}
- apiKeys[i] = convert2.ToDeployKey(apiLink, keys[i])
+ apiKeys[i] = convert.ToDeployKey(apiLink, keys[i])
}
- c.JSON(200, &apiKeys)
+ c.JSONSuccess(&apiKeys)
}
// https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#get-a-deploy-key
func GetDeployKey(c *context.APIContext) {
key, err := db.GetDeployKeyByID(c.ParamsInt64(":id"))
if err != nil {
- if db.IsErrDeployKeyNotExist(err) {
- c.Status(404)
- } else {
- c.Error(500, "GetDeployKeyByID", err)
- }
+ c.NotFoundOrError(err, "get deploy key by ID")
return
}
if err = key.GetContent(); err != nil {
- c.Error(500, "GetContent", err)
+ c.Error(err, "get content")
return
}
apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
- c.JSON(200, convert2.ToDeployKey(apiLink, key))
+ c.JSONSuccess(convert.ToDeployKey(apiLink, key))
}
func HandleCheckKeyStringError(c *context.APIContext, err error) {
if db.IsErrKeyUnableVerify(err) {
- c.Error(422, "", "Unable to verify key content")
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Unable to verify key content"))
} else {
- c.Error(422, "", fmt.Errorf("Invalid key content: %v", err))
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.Wrap(err, "Invalid key content: %v"))
}
}
func HandleAddKeyError(c *context.APIContext, err error) {
switch {
case db.IsErrKeyAlreadyExist(err):
- c.Error(422, "", "Key content has been used as non-deploy key")
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Key content has been used as non-deploy key"))
case db.IsErrKeyNameAlreadyUsed(err):
- c.Error(422, "", "Key title has been used")
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Key title has been used"))
default:
- c.Error(500, "AddKey", err)
+ c.Error(err, "add key")
}
}
@@ -96,19 +93,19 @@ func CreateDeployKey(c *context.APIContext, form api.CreateKeyOption) {
key.Content = content
apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
- c.JSON(201, convert2.ToDeployKey(apiLink, key))
+ c.JSON(http.StatusCreated, convert.ToDeployKey(apiLink, key))
}
// https://github.com/gogs/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key
func DeleteDeploykey(c *context.APIContext) {
if err := db.DeleteDeployKey(c.User, c.ParamsInt64(":id")); err != nil {
if db.IsErrKeyAccessDenied(err) {
- c.Error(403, "", "You do not have access to this key")
+ c.ErrorStatus(http.StatusForbidden, errors.New("You do not have access to this key"))
} else {
- c.Error(500, "DeleteDeployKey", err)
+ c.Error(err, "delete deploy key")
}
return
}
- c.Status(204)
+ c.NoContent()
}
diff --git a/internal/route/api/v1/repo/label.go b/internal/route/api/v1/repo/label.go
index 9dd2d7d0..69fe5399 100644
--- a/internal/route/api/v1/repo/label.go
+++ b/internal/route/api/v1/repo/label.go
@@ -7,9 +7,8 @@ package repo
import (
"net/http"
- "github.com/unknwon/com"
-
api "github.com/gogs/go-gogs-client"
+ "github.com/unknwon/com"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
@@ -18,7 +17,7 @@ import (
func ListLabels(c *context.APIContext) {
labels, err := db.GetLabelsByRepoID(c.Repo.Repository.ID)
if err != nil {
- c.ServerError("GetLabelsByRepoID", err)
+ c.Error(err, "get labels by repository ID")
return
}
@@ -39,7 +38,7 @@ func GetLabel(c *context.APIContext) {
label, err = db.GetLabelOfRepoByName(c.Repo.Repository.ID, idStr)
}
if err != nil {
- c.NotFoundOrServerError("GetLabel", db.IsErrLabelNotExist, err)
+ c.NotFoundOrError(err, "get label")
return
}
@@ -53,7 +52,7 @@ func CreateLabel(c *context.APIContext, form api.CreateLabelOption) {
RepoID: c.Repo.Repository.ID,
}
if err := db.NewLabels(label); err != nil {
- c.ServerError("NewLabel", err)
+ c.Error(err, "new labels")
return
}
c.JSON(http.StatusCreated, label.APIFormat())
@@ -62,7 +61,7 @@ func CreateLabel(c *context.APIContext, form api.CreateLabelOption) {
func EditLabel(c *context.APIContext, form api.EditLabelOption) {
label, err := db.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
if err != nil {
- c.NotFoundOrServerError("GetLabelOfRepoByID", db.IsErrLabelNotExist, err)
+ c.NotFoundOrError(err, "get label of repository by ID")
return
}
@@ -73,7 +72,7 @@ func EditLabel(c *context.APIContext, form api.EditLabelOption) {
label.Color = *form.Color
}
if err := db.UpdateLabel(label); err != nil {
- c.ServerError("UpdateLabel", err)
+ c.Error(err, "update label")
return
}
c.JSONSuccess(label.APIFormat())
@@ -81,7 +80,7 @@ func EditLabel(c *context.APIContext, form api.EditLabelOption) {
func DeleteLabel(c *context.APIContext) {
if err := db.DeleteLabel(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
- c.ServerError("DeleteLabel", err)
+ c.Error(err, "delete label")
return
}
diff --git a/internal/route/api/v1/repo/milestone.go b/internal/route/api/v1/repo/milestone.go
index 6f5fea17..a36a71d8 100644
--- a/internal/route/api/v1/repo/milestone.go
+++ b/internal/route/api/v1/repo/milestone.go
@@ -17,7 +17,7 @@ import (
func ListMilestones(c *context.APIContext) {
milestones, err := db.GetMilestonesByRepoID(c.Repo.Repository.ID)
if err != nil {
- c.ServerError("GetMilestonesByRepoID", err)
+ c.Error(err, "get milestones by repository ID")
return
}
@@ -31,7 +31,7 @@ func ListMilestones(c *context.APIContext) {
func GetMilestone(c *context.APIContext) {
milestone, err := db.GetMilestoneByRepoID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
if err != nil {
- c.NotFoundOrServerError("GetMilestoneByRepoID", db.IsErrMilestoneNotExist, err)
+ c.NotFoundOrError(err, "get milestone by repository ID")
return
}
c.JSONSuccess(milestone.APIFormat())
@@ -51,7 +51,7 @@ func CreateMilestone(c *context.APIContext, form api.CreateMilestoneOption) {
}
if err := db.NewMilestone(milestone); err != nil {
- c.ServerError("NewMilestone", err)
+ c.Error(err, "new milestone")
return
}
c.JSON(http.StatusCreated, milestone.APIFormat())
@@ -60,7 +60,7 @@ func CreateMilestone(c *context.APIContext, form api.CreateMilestoneOption) {
func EditMilestone(c *context.APIContext, form api.EditMilestoneOption) {
milestone, err := db.GetMilestoneByRepoID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
if err != nil {
- c.NotFoundOrServerError("GetMilestoneByRepoID", db.IsErrMilestoneNotExist, err)
+ c.NotFoundOrError(err, "get milestone by repository ID")
return
}
@@ -76,11 +76,11 @@ func EditMilestone(c *context.APIContext, form api.EditMilestoneOption) {
if form.State != nil {
if err = milestone.ChangeStatus(api.STATE_CLOSED == api.StateType(*form.State)); err != nil {
- c.ServerError("ChangeStatus", err)
+ c.Error(err, "change status")
return
}
} else if err = db.UpdateMilestone(milestone); err != nil {
- c.ServerError("UpdateMilestone", err)
+ c.Error(err, "update milestone")
return
}
@@ -89,7 +89,7 @@ func EditMilestone(c *context.APIContext, form api.EditMilestoneOption) {
func DeleteMilestone(c *context.APIContext) {
if err := db.DeleteMilestoneOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
- c.ServerError("DeleteMilestoneByRepoID", err)
+ c.Error(err, "delete milestone of repository by ID")
return
}
c.NoContent()
diff --git a/internal/route/api/v1/repo/repo.go b/internal/route/api/v1/repo/repo.go
index 7ea9a870..e198dffc 100644
--- a/internal/route/api/v1/repo/repo.go
+++ b/internal/route/api/v1/repo/repo.go
@@ -5,18 +5,16 @@
package repo
import (
- "fmt"
"net/http"
"path"
- log "unknwon.dev/clog/v2"
-
api "github.com/gogs/go-gogs-client"
+ "github.com/pkg/errors"
+ log "unknwon.dev/clog/v2"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/db/errors"
"gogs.io/gogs/internal/form"
"gogs.io/gogs/internal/route/api/v1/convert"
)
@@ -81,7 +79,7 @@ func Search(c *context.APIContext) {
func listUserRepositories(c *context.APIContext, username string) {
user, err := db.GetUserByName(username)
if err != nil {
- c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
+ c.NotFoundOrError(err, "get user by name")
return
}
@@ -99,12 +97,12 @@ func listUserRepositories(c *context.APIContext, username string) {
})
}
if err != nil {
- c.ServerError("GetUserRepositories", err)
+ c.Error(err, "get user repositories")
return
}
if err = db.RepositoryList(ownRepos).LoadAttributes(); err != nil {
- c.ServerError("LoadAttributes(ownRepos)", err)
+ c.Error(err, "load attributes")
return
}
@@ -120,7 +118,7 @@ func listUserRepositories(c *context.APIContext, username string) {
accessibleRepos, err := user.GetRepositoryAccesses()
if err != nil {
- c.ServerError("GetRepositoryAccesses", err)
+ c.Error(err, "get repositories accesses")
return
}
@@ -169,14 +167,14 @@ func CreateUserRepo(c *context.APIContext, owner *db.User, opt api.CreateRepoOpt
if db.IsErrRepoAlreadyExist(err) ||
db.IsErrNameReserved(err) ||
db.IsErrNamePatternNotAllowed(err) {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
if repo != nil {
if err = db.DeleteRepository(c.User.ID, repo.ID); err != nil {
- log.Error("DeleteRepository: %v", err)
+ log.Error("Failed to delete repository: %v", err)
}
}
- c.ServerError("CreateRepository", err)
+ c.Error(err, "create repository")
}
return
}
@@ -187,7 +185,7 @@ func CreateUserRepo(c *context.APIContext, owner *db.User, opt api.CreateRepoOpt
func Create(c *context.APIContext, opt api.CreateRepoOption) {
// Shouldn't reach this condition, but just in case.
if c.User.IsOrganization() {
- c.Error(http.StatusUnprocessableEntity, "", "not allowed creating repository for organization")
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Not allowed to create repository for organization."))
return
}
CreateUserRepo(c, c.User, opt)
@@ -196,12 +194,12 @@ func Create(c *context.APIContext, opt api.CreateRepoOption) {
func CreateOrgRepo(c *context.APIContext, opt api.CreateRepoOption) {
org, err := db.GetOrgByName(c.Params(":org"))
if err != nil {
- c.NotFoundOrServerError("GetOrgByName", errors.IsUserNotExist, err)
+ c.NotFoundOrError(err, "get organization by name")
return
}
if !org.IsOwnedBy(c.User.ID) {
- c.Error(http.StatusForbidden, "", "given user is not owner of organization")
+ c.ErrorStatus(http.StatusForbidden, errors.New("Given user is not owner of organization."))
return
}
CreateUserRepo(c, org, opt)
@@ -214,28 +212,28 @@ func Migrate(c *context.APIContext, f form.MigrateRepo) {
if f.Uid != ctxUser.ID {
org, err := db.GetUserByID(f.Uid)
if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ if db.IsErrUserNotExist(err) {
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
- c.Error(http.StatusInternalServerError, "GetUserByID", err)
+ c.Error(err, "get user by ID")
}
return
} else if !org.IsOrganization() && !c.User.IsAdmin {
- c.Error(http.StatusForbidden, "", "given user is not an organization")
+ c.ErrorStatus(http.StatusForbidden, errors.New("Given user is not an organization."))
return
}
ctxUser = org
}
if c.HasError() {
- c.Error(http.StatusUnprocessableEntity, "", c.GetErrMsg())
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New(c.GetErrMsg()))
return
}
if ctxUser.IsOrganization() && !c.User.IsAdmin {
// Check ownership of organization.
if !ctxUser.IsOwnedBy(c.User.ID) {
- c.Error(http.StatusForbidden, "", "Given user is not owner of organization")
+ c.ErrorStatus(http.StatusForbidden, errors.New("Given user is not owner of organization."))
return
}
}
@@ -246,16 +244,16 @@ func Migrate(c *context.APIContext, f form.MigrateRepo) {
addrErr := err.(db.ErrInvalidCloneAddr)
switch {
case addrErr.IsURLError:
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
case addrErr.IsPermissionDenied:
- c.Error(http.StatusUnprocessableEntity, "", "you are not allowed to import local repositories")
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("You are not allowed to import local repositories."))
case addrErr.IsInvalidPath:
- c.Error(http.StatusUnprocessableEntity, "", "invalid local path, it does not exist or not a directory")
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Invalid local path, it does not exist or not a directory."))
default:
- c.ServerError("ParseRemoteAddr", fmt.Errorf("unknown error type (ErrInvalidCloneAddr): %v", err))
+ c.Error(err, "unexpected error")
}
} else {
- c.ServerError("ParseRemoteAddr", err)
+ c.Error(err, "parse remote address")
}
return
}
@@ -274,10 +272,10 @@ func Migrate(c *context.APIContext, f form.MigrateRepo) {
}
}
- if errors.IsReachLimitOfRepo(err) {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ if db.IsErrReachLimitOfRepo(err) {
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
- c.ServerError("MigrateRepository", errors.New(db.HandleMirrorCredentials(err.Error(), true)))
+ c.Error(errors.New(db.HandleMirrorCredentials(err.Error(), true)), "migrate repository")
}
return
}
@@ -290,17 +288,17 @@ func Migrate(c *context.APIContext, f form.MigrateRepo) {
func parseOwnerAndRepo(c *context.APIContext) (*db.User, *db.Repository) {
owner, err := db.GetUserByName(c.Params(":username"))
if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ if db.IsErrUserNotExist(err) {
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
- c.ServerError("GetUserByName", err)
+ c.Error(err, "get user by name")
}
return nil, nil
}
repo, err := db.GetRepositoryByName(owner.ID, c.Params(":reponame"))
if err != nil {
- c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
+ c.NotFoundOrError(err, "get repository by name")
return nil, nil
}
@@ -327,12 +325,12 @@ func Delete(c *context.APIContext) {
}
if owner.IsOrganization() && !owner.IsOwnedBy(c.User.ID) {
- c.Error(http.StatusForbidden, "", "given user is not owner of organization")
+ c.ErrorStatus(http.StatusForbidden, errors.New("Given user is not owner of organization."))
return
}
if err := db.DeleteRepository(owner.ID, repo.ID); err != nil {
- c.ServerError("DeleteRepository", err)
+ c.Error(err, "delete repository")
return
}
@@ -343,14 +341,14 @@ func Delete(c *context.APIContext) {
func ListForks(c *context.APIContext) {
forks, err := c.Repo.Repository.GetForks()
if err != nil {
- c.ServerError("GetForks", err)
+ c.Error(err, "get forks")
return
}
apiForks := make([]*api.Repository, len(forks))
for i := range forks {
if err := forks[i].GetOwner(); err != nil {
- c.ServerError("GetOwner", err)
+ c.Error(err, "get owner")
return
}
apiForks[i] = forks[i].APIFormat(&api.Permission{
@@ -386,7 +384,7 @@ func IssueTracker(c *context.APIContext, form api.EditIssueTrackerOption) {
}
if err := db.UpdateRepository(repo, false); err != nil {
- c.ServerError("UpdateRepository", err)
+ c.Error(err, "update repository")
return
}
diff --git a/internal/route/api/v1/repo/tree.go b/internal/route/api/v1/repo/tree.go
index da976113..cd51282b 100644
--- a/internal/route/api/v1/repo/tree.go
+++ b/internal/route/api/v1/repo/tree.go
@@ -16,20 +16,20 @@ import (
func GetRepoGitTree(c *context.APIContext) {
gitRepo, err := git.Open(c.Repo.Repository.RepoPath())
if err != nil {
- c.ServerError("open repository", err)
+ c.Error(err, "open repository")
return
}
sha := c.Params(":sha")
tree, err := gitRepo.LsTree(sha)
if err != nil {
- c.NotFoundOrServerError("get tree", gitutil.IsErrRevisionNotExist, err)
+ c.NotFoundOrError(gitutil.NewError(err), "get tree")
return
}
entries, err := tree.Entries()
if err != nil {
- c.ServerError("list entries", err)
+ c.Error(err, "list entries")
return
}
diff --git a/internal/route/api/v1/user/app.go b/internal/route/api/v1/user/app.go
index fdf11c09..99a422cc 100644
--- a/internal/route/api/v1/user/app.go
+++ b/internal/route/api/v1/user/app.go
@@ -17,7 +17,7 @@ import (
func ListAccessTokens(c *context.APIContext) {
tokens, err := db.ListAccessTokens(c.User.ID)
if err != nil {
- c.ServerError("ListAccessTokens", err)
+ c.Error(err, "list access tokens")
return
}
@@ -35,9 +35,9 @@ func CreateAccessToken(c *context.APIContext, form api.CreateAccessTokenOption)
}
if err := db.NewAccessToken(t); err != nil {
if errors.IsAccessTokenNameAlreadyExist(err) {
- c.Error(http.StatusUnprocessableEntity, "", err)
+ c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
- c.ServerError("NewAccessToken", err)
+ c.Error(err, "new access token")
}
return
}
diff --git a/internal/route/api/v1/user/email.go b/internal/route/api/v1/user/email.go
index e211221c..07fd4f8a 100644
--- a/internal/route/api/v1/user/email.go
+++ b/internal/route/api/v1/user/email.go
@@ -5,25 +5,26 @@
package user
import (
- convert2 "gogs.io/gogs/internal/route/api/v1/convert"
"net/http"
api "github.com/gogs/go-gogs-client"
+ "github.com/pkg/errors"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
+ "gogs.io/gogs/internal/route/api/v1/convert"
)
func ListEmails(c *context.APIContext) {
emails, err := db.GetEmailAddresses(c.User.ID)
if err != nil {
- c.ServerError("GetEmailAddresses", err)
+ c.Error(err, "get email addresses")
return
}
apiEmails := make([]*api.Email, len(emails))
for i := range emails {
- apiEmails[i] = convert2.ToEmail(emails[i])
+ apiEmails[i] = convert.ToEmail(emails[i])
}
c.JSONSuccess(&apiEmails)
}
@@ -45,16 +46,16 @@ func AddEmail(c *context.APIContext, form api.CreateEmailOption) {
if err := db.AddEmailAddresses(emails); err != nil {
if db.IsErrEmailAlreadyUsed(err) {
- c.Error(http.StatusUnprocessableEntity, "", "email address has been used: "+err.(db.ErrEmailAlreadyUsed).Email)
+ c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("email address has been used: "+err.(db.ErrEmailAlreadyUsed).Email))
} else {
- c.Error(http.StatusInternalServerError, "AddEmailAddresses", err)
+ c.Error(err, "add email addresses")
}
return
}
apiEmails := make([]*api.Email, len(emails))
for i := range emails {
- apiEmails[i] = convert2.ToEmail(emails[i])
+ apiEmails[i] = convert.ToEmail(emails[i])
}
c.JSON(http.StatusCreated, &apiEmails)
}
@@ -74,7 +75,7 @@ func DeleteEmail(c *context.APIContext, form api.CreateEmailOption) {
}
if err := db.DeleteEmailAddresses(emails); err != nil {
- c.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err)
+ c.Error(err, "delete email addresses")
return
}
c.NoContent()
diff --git a/internal/route/api/v1/user/follower.go b/internal/route/api/v1/user/follower.go
index 3a3d0298..c587547b 100644
--- a/internal/route/api/v1/user/follower.go
+++ b/internal/route/api/v1/user/follower.go
@@ -22,7 +22,7 @@ func responseApiUsers(c *context.APIContext, users []*db.User) {
func listUserFollowers(c *context.APIContext, u *db.User) {
users, err := u.GetFollowers(c.QueryInt("page"))
if err != nil {
- c.ServerError("GetUserFollowers", err)
+ c.Error(err, "get followers")
return
}
responseApiUsers(c, users)
@@ -43,7 +43,7 @@ func ListFollowers(c *context.APIContext) {
func listUserFollowing(c *context.APIContext, u *db.User) {
users, err := u.GetFollowing(c.QueryInt("page"))
if err != nil {
- c.ServerError("GetFollowing", err)
+ c.Error(err, "get following")
return
}
responseApiUsers(c, users)
@@ -95,7 +95,7 @@ func Follow(c *context.APIContext) {
return
}
if err := db.FollowUser(c.User.ID, target.ID); err != nil {
- c.ServerError("FollowUser", err)
+ c.Error(err, "follow user")
return
}
c.NoContent()
@@ -107,7 +107,7 @@ func Unfollow(c *context.APIContext) {
return
}
if err := db.UnfollowUser(c.User.ID, target.ID); err != nil {
- c.ServerError("UnfollowUser", err)
+ c.Error(err, "unfollow user")
return
}
c.NoContent()
diff --git a/internal/route/api/v1/user/key.go b/internal/route/api/v1/user/key.go
index 759f9008..0d8c4f16 100644
--- a/internal/route/api/v1/user/key.go
+++ b/internal/route/api/v1/user/key.go
@@ -5,21 +5,22 @@
package user
import (
- api "github.com/gogs/go-gogs-client"
- convert2 "gogs.io/gogs/internal/route/api/v1/convert"
- repo2 "gogs.io/gogs/internal/route/api/v1/repo"
"net/http"
+ api "github.com/gogs/go-gogs-client"
+ "github.com/pkg/errors"
+
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/route/api/v1/convert"
+ "gogs.io/gogs/internal/route/api/v1/repo"
)
func GetUserByParamsName(c *context.APIContext, name string) *db.User {
user, err := db.GetUserByName(c.Params(name))
if err != nil {
- c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
+ c.NotFoundOrError(err, "get user by name")
return nil
}
return user
@@ -37,14 +38,14 @@ func composePublicKeysAPILink() string {
func listPublicKeys(c *context.APIContext, uid int64) {
keys, err := db.ListPublicKeys(uid)
if err != nil {
- c.ServerError("ListPublicKeys", err)
+ c.Error(err, "list public keys")
return
}
apiLink := composePublicKeysAPILink()
apiKeys := make([]*api.PublicKey, len(keys))
for i := range keys {
- apiKeys[i] = convert2.ToPublicKey(apiLink, keys[i])
+ apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
}
c.JSONSuccess(&apiKeys)
@@ -65,29 +66,29 @@ func ListPublicKeys(c *context.APIContext) {
func GetPublicKey(c *context.APIContext) {
key, err := db.GetPublicKeyByID(c.ParamsInt64(":id"))
if err != nil {
- c.NotFoundOrServerError("GetPublicKeyByID", db.IsErrKeyNotExist, err)
+ c.NotFoundOrError(err, "get public key by ID")
return
}
apiLink := composePublicKeysAPILink()
- c.JSONSuccess(convert2.ToPublicKey(apiLink, key))
+ c.JSONSuccess(convert.ToPublicKey(apiLink, key))
}
// CreateUserPublicKey creates new public key to given user by ID.
func CreateUserPublicKey(c *context.APIContext, form api.CreateKeyOption, uid int64) {
content, err := db.CheckPublicKeyString(form.Key)
if err != nil {
- repo2.HandleCheckKeyStringError(c, err)
+ repo.HandleCheckKeyStringError(c, err)
return
}
key, err := db.AddPublicKey(uid, form.Title, content)
if err != nil {
- repo2.HandleAddKeyError(c, err)
+ repo.HandleAddKeyError(c, err)
return
}
apiLink := composePublicKeysAPILink()
- c.JSON(http.StatusCreated, convert2.ToPublicKey(apiLink, key))
+ c.JSON(http.StatusCreated, convert.ToPublicKey(apiLink, key))
}
func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
@@ -97,9 +98,9 @@ func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
func DeletePublicKey(c *context.APIContext) {
if err := db.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil {
if db.IsErrKeyAccessDenied(err) {
- c.Error(http.StatusForbidden, "", "you do not have access to this key")
+ c.ErrorStatus(http.StatusForbidden, errors.New("You do not have access to this key."))
} else {
- c.Error(http.StatusInternalServerError, "DeletePublicKey", err)
+ c.Error(err, "delete public key")
}
return
}
diff --git a/internal/route/api/v1/user/user.go b/internal/route/api/v1/user/user.go
index 8da3b734..695d5311 100644
--- a/internal/route/api/v1/user/user.go
+++ b/internal/route/api/v1/user/user.go
@@ -13,7 +13,6 @@ import (
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/db/errors"
"gogs.io/gogs/internal/markup"
)
@@ -58,7 +57,7 @@ func Search(c *context.APIContext) {
func GetInfo(c *context.APIContext) {
u, err := db.GetUserByName(c.Params(":username"))
if err != nil {
- c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
+ c.NotFoundOrError(err, "get user by name")
return
}