From fec152155543e72f2e03885d431642887ce09d69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=99=BA=E8=B6=85?= <1012112796@qq.com> Date: Mon, 21 Sep 2020 04:20:14 +0800 Subject: [PATCH] Not using "ctx.ServerError" in api (#12907) This function will render a whole html page which is not useful for API. Signed-off-by: a1012112796 <1012112796@qq.com> --- routers/api/v1/misc/signing.go | 8 +++----- routers/api/v1/org/label.go | 2 +- routers/api/v1/repo/commits.go | 18 +++++++++--------- routers/api/v1/repo/fork.go | 2 +- routers/api/v1/repo/label.go | 2 +- routers/api/v1/repo/milestone.go | 2 +- routers/api/v1/repo/pull.go | 8 ++++---- routers/api/v1/repo/pull_review.go | 6 +++--- routers/api/v1/repo/release.go | 4 ++-- routers/api/v1/repo/repo.go | 2 +- 10 files changed, 26 insertions(+), 28 deletions(-) diff --git a/routers/api/v1/misc/signing.go b/routers/api/v1/misc/signing.go index 0b295f389..3c3391bfe 100644 --- a/routers/api/v1/misc/signing.go +++ b/routers/api/v1/misc/signing.go @@ -10,11 +10,10 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/log" ) // SigningKey returns the public key of the default signing key if it exists -func SigningKey(ctx *context.Context) { +func SigningKey(ctx *context.APIContext) { // swagger:operation GET /signing-key.gpg miscellaneous getSigningKey // --- // summary: Get default signing-key.gpg @@ -55,12 +54,11 @@ func SigningKey(ctx *context.Context) { content, err := models.PublicSigningKey(path) if err != nil { - ctx.ServerError("gpg export", err) + ctx.Error(http.StatusInternalServerError, "gpg export", err) return } _, err = ctx.Write([]byte(content)) if err != nil { - log.Error("Error writing key content %v", err) - ctx.Error(http.StatusInternalServerError, fmt.Sprintf("%v", err)) + ctx.Error(http.StatusInternalServerError, "gpg export", fmt.Errorf("Error writing key content %v", err)) } } diff --git a/routers/api/v1/org/label.go b/routers/api/v1/org/label.go index b4a8a5ac3..9a8a4fa29 100644 --- a/routers/api/v1/org/label.go +++ b/routers/api/v1/org/label.go @@ -201,7 +201,7 @@ func EditLabel(ctx *context.APIContext, form api.EditLabelOption) { label.Description = *form.Description } if err := models.UpdateLabel(label); err != nil { - ctx.ServerError("UpdateLabel", err) + ctx.Error(http.StatusInternalServerError, "UpdateLabel", err) return } ctx.JSON(http.StatusOK, convert.ToLabel(label)) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 220bb0fd7..8c877285a 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -63,7 +63,7 @@ func GetSingleCommit(ctx *context.APIContext) { func getCommit(ctx *context.APIContext, identifier string) { gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath()) if err != nil { - ctx.ServerError("OpenRepository", err) + ctx.Error(http.StatusInternalServerError, "OpenRepository", err) return } defer gitRepo.Close() @@ -75,7 +75,7 @@ func getCommit(ctx *context.APIContext, identifier string) { json, err := convert.ToCommit(ctx.Repo.Repository, commit, nil) if err != nil { - ctx.ServerError("toCommit", err) + ctx.Error(http.StatusInternalServerError, "toCommit", err) return } ctx.JSON(http.StatusOK, json) @@ -129,7 +129,7 @@ func GetAllCommits(ctx *context.APIContext) { gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath()) if err != nil { - ctx.ServerError("OpenRepository", err) + ctx.Error(http.StatusInternalServerError, "OpenRepository", err) return } defer gitRepo.Close() @@ -150,20 +150,20 @@ func GetAllCommits(ctx *context.APIContext) { // no sha supplied - use default branch head, err := gitRepo.GetHEADBranch() if err != nil { - ctx.ServerError("GetHEADBranch", err) + ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err) return } baseCommit, err = gitRepo.GetBranchCommit(head.Name) if err != nil { - ctx.ServerError("GetCommit", err) + ctx.Error(http.StatusInternalServerError, "GetCommit", err) return } } else { // get commit specified by sha baseCommit, err = gitRepo.GetCommit(sha) if err != nil { - ctx.ServerError("GetCommit", err) + ctx.Error(http.StatusInternalServerError, "GetCommit", err) return } } @@ -171,7 +171,7 @@ func GetAllCommits(ctx *context.APIContext) { // Total commit count commitsCountTotal, err := baseCommit.CommitsCount() if err != nil { - ctx.ServerError("GetCommitsCount", err) + ctx.Error(http.StatusInternalServerError, "GetCommitsCount", err) return } @@ -180,7 +180,7 @@ func GetAllCommits(ctx *context.APIContext) { // Query commits commits, err := baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize) if err != nil { - ctx.ServerError("CommitsByRange", err) + ctx.Error(http.StatusInternalServerError, "CommitsByRange", err) return } @@ -195,7 +195,7 @@ func GetAllCommits(ctx *context.APIContext) { // Create json struct apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache) if err != nil { - ctx.ServerError("toCommit", err) + ctx.Error(http.StatusInternalServerError, "toCommit", err) return } diff --git a/routers/api/v1/repo/fork.go b/routers/api/v1/repo/fork.go index 0dd961d7f..e6f5f5e63 100644 --- a/routers/api/v1/repo/fork.go +++ b/routers/api/v1/repo/fork.go @@ -109,7 +109,7 @@ func CreateFork(ctx *context.APIContext, form api.CreateForkOption) { } isMember, err := org.IsOrgMember(ctx.User.ID) if err != nil { - ctx.ServerError("IsOrgMember", err) + ctx.Error(http.StatusInternalServerError, "IsOrgMember", err) return } else if !isMember { ctx.Error(http.StatusForbidden, "isMemberNot", fmt.Sprintf("User is no Member of Organisation '%s'", org.Name)) diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go index a293801c4..fef6ebf07 100644 --- a/routers/api/v1/repo/label.go +++ b/routers/api/v1/repo/label.go @@ -222,7 +222,7 @@ func EditLabel(ctx *context.APIContext, form api.EditLabelOption) { label.Description = *form.Description } if err := models.UpdateLabel(label); err != nil { - ctx.ServerError("UpdateLabel", err) + ctx.Error(http.StatusInternalServerError, "UpdateLabel", err) return } ctx.JSON(http.StatusOK, convert.ToLabel(label)) diff --git a/routers/api/v1/repo/milestone.go b/routers/api/v1/repo/milestone.go index 5a250dbd8..db86d0868 100644 --- a/routers/api/v1/repo/milestone.go +++ b/routers/api/v1/repo/milestone.go @@ -215,7 +215,7 @@ func EditMilestone(ctx *context.APIContext, form api.EditMilestoneOption) { } if err := models.UpdateMilestone(milestone, oldIsClosed); err != nil { - ctx.ServerError("UpdateMilestone", err) + ctx.Error(http.StatusInternalServerError, "UpdateMilestone", err) return } ctx.JSON(http.StatusOK, convert.ToAPIMilestone(milestone)) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 126815839..afbe7ddab 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -725,7 +725,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) { } if err = pr.LoadHeadRepo(); err != nil { - ctx.ServerError("LoadHeadRepo", err) + ctx.Error(http.StatusInternalServerError, "LoadHeadRepo", err) return } @@ -885,7 +885,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) if models.IsErrUserNotExist(err) { ctx.NotFound("GetUserByName") } else { - ctx.ServerError("GetUserByName", err) + ctx.Error(http.StatusInternalServerError, "GetUserByName", err) } return nil, nil, nil, nil, "", "" } @@ -929,7 +929,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) permBase, err := models.GetUserRepoPermission(baseRepo, ctx.User) if err != nil { headGitRepo.Close() - ctx.ServerError("GetUserRepoPermission", err) + ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err) return nil, nil, nil, nil, "", "" } if !permBase.CanReadIssuesOrPulls(true) || !permBase.CanRead(models.UnitTypeCode) { @@ -948,7 +948,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) permHead, err := models.GetUserRepoPermission(headRepo, ctx.User) if err != nil { headGitRepo.Close() - ctx.ServerError("GetUserRepoPermission", err) + ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err) return nil, nil, nil, nil, "", "" } if !permHead.CanRead(models.UnitTypeCode) { diff --git a/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index 3f2cb011d..86c084acd 100644 --- a/routers/api/v1/repo/pull_review.go +++ b/routers/api/v1/repo/pull_review.go @@ -318,14 +318,14 @@ func CreatePullReview(ctx *context.APIContext, opts api.CreatePullReviewOptions) if opts.CommitID == "" { gitRepo, err := git.OpenRepository(pr.Issue.Repo.RepoPath()) if err != nil { - ctx.ServerError("git.OpenRepository", err) + ctx.Error(http.StatusInternalServerError, "git.OpenRepository", err) return } defer gitRepo.Close() headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName()) if err != nil { - ctx.ServerError("GetRefCommitID", err) + ctx.Error(http.StatusInternalServerError, "GetRefCommitID", err) return } @@ -350,7 +350,7 @@ func CreatePullReview(ctx *context.APIContext, opts api.CreatePullReviewOptions) 0, // no reply opts.CommitID, ); err != nil { - ctx.ServerError("CreateCodeComment", err) + ctx.Error(http.StatusInternalServerError, "CreateCodeComment", err) return } } diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index 752b5c76e..e7cc1e867 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -151,7 +151,7 @@ func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) { rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName) if err != nil { if !models.IsErrReleaseNotExist(err) { - ctx.ServerError("GetRelease", err) + ctx.Error(http.StatusInternalServerError, "GetRelease", err) return } // If target is not provided use default branch @@ -195,7 +195,7 @@ func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) { rel.Publisher = ctx.User if err = releaseservice.UpdateReleaseOrCreatReleaseFromTag(ctx.User, ctx.Repo.GitRepo, rel, nil, true); err != nil { - ctx.ServerError("UpdateReleaseOrCreatReleaseFromTag", err) + ctx.Error(http.StatusInternalServerError, "UpdateReleaseOrCreatReleaseFromTag", err) return } } diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index baf8110a1..b8a24e253 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -373,7 +373,7 @@ func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) { if !ctx.User.IsAdmin { canCreate, err := org.CanCreateOrgRepo(ctx.User.ID) if err != nil { - ctx.ServerError("CanCreateOrgRepo", err) + ctx.Error(http.StatusInternalServerError, "CanCreateOrgRepo", err) return } else if !canCreate { ctx.Error(http.StatusForbidden, "", "Given user is not allowed to create repository in organization.")