aboutsummaryrefslogtreecommitdiff
path: root/internal/route/api/v1/repo
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/v1/repo
parent82ff0c5852f29daa5f95d965fd50665581e7ea3c (diff)
refactor: unify error handling in routing layer
Diffstat (limited to 'internal/route/api/v1/repo')
-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
14 files changed, 187 insertions, 203 deletions
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
}