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>
This commit is contained in:
赵智超 2020-09-21 04:20:14 +08:00 committed by GitHub
parent e7ffc67ad5
commit fec1521555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 26 additions and 28 deletions

View File

@ -10,11 +10,10 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context" "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 // 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 // swagger:operation GET /signing-key.gpg miscellaneous getSigningKey
// --- // ---
// summary: Get default signing-key.gpg // summary: Get default signing-key.gpg
@ -55,12 +54,11 @@ func SigningKey(ctx *context.Context) {
content, err := models.PublicSigningKey(path) content, err := models.PublicSigningKey(path)
if err != nil { if err != nil {
ctx.ServerError("gpg export", err) ctx.Error(http.StatusInternalServerError, "gpg export", err)
return return
} }
_, err = ctx.Write([]byte(content)) _, err = ctx.Write([]byte(content))
if err != nil { if err != nil {
log.Error("Error writing key content %v", err) ctx.Error(http.StatusInternalServerError, "gpg export", fmt.Errorf("Error writing key content %v", err))
ctx.Error(http.StatusInternalServerError, fmt.Sprintf("%v", err))
} }
} }

View File

@ -201,7 +201,7 @@ func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
label.Description = *form.Description label.Description = *form.Description
} }
if err := models.UpdateLabel(label); err != nil { if err := models.UpdateLabel(label); err != nil {
ctx.ServerError("UpdateLabel", err) ctx.Error(http.StatusInternalServerError, "UpdateLabel", err)
return return
} }
ctx.JSON(http.StatusOK, convert.ToLabel(label)) ctx.JSON(http.StatusOK, convert.ToLabel(label))

View File

@ -63,7 +63,7 @@ func GetSingleCommit(ctx *context.APIContext) {
func getCommit(ctx *context.APIContext, identifier string) { func getCommit(ctx *context.APIContext, identifier string) {
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath()) gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
if err != nil { if err != nil {
ctx.ServerError("OpenRepository", err) ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
return return
} }
defer gitRepo.Close() defer gitRepo.Close()
@ -75,7 +75,7 @@ func getCommit(ctx *context.APIContext, identifier string) {
json, err := convert.ToCommit(ctx.Repo.Repository, commit, nil) json, err := convert.ToCommit(ctx.Repo.Repository, commit, nil)
if err != nil { if err != nil {
ctx.ServerError("toCommit", err) ctx.Error(http.StatusInternalServerError, "toCommit", err)
return return
} }
ctx.JSON(http.StatusOK, json) ctx.JSON(http.StatusOK, json)
@ -129,7 +129,7 @@ func GetAllCommits(ctx *context.APIContext) {
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath()) gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
if err != nil { if err != nil {
ctx.ServerError("OpenRepository", err) ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
return return
} }
defer gitRepo.Close() defer gitRepo.Close()
@ -150,20 +150,20 @@ func GetAllCommits(ctx *context.APIContext) {
// no sha supplied - use default branch // no sha supplied - use default branch
head, err := gitRepo.GetHEADBranch() head, err := gitRepo.GetHEADBranch()
if err != nil { if err != nil {
ctx.ServerError("GetHEADBranch", err) ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err)
return return
} }
baseCommit, err = gitRepo.GetBranchCommit(head.Name) baseCommit, err = gitRepo.GetBranchCommit(head.Name)
if err != nil { if err != nil {
ctx.ServerError("GetCommit", err) ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return return
} }
} else { } else {
// get commit specified by sha // get commit specified by sha
baseCommit, err = gitRepo.GetCommit(sha) baseCommit, err = gitRepo.GetCommit(sha)
if err != nil { if err != nil {
ctx.ServerError("GetCommit", err) ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return return
} }
} }
@ -171,7 +171,7 @@ func GetAllCommits(ctx *context.APIContext) {
// Total commit count // Total commit count
commitsCountTotal, err := baseCommit.CommitsCount() commitsCountTotal, err := baseCommit.CommitsCount()
if err != nil { if err != nil {
ctx.ServerError("GetCommitsCount", err) ctx.Error(http.StatusInternalServerError, "GetCommitsCount", err)
return return
} }
@ -180,7 +180,7 @@ func GetAllCommits(ctx *context.APIContext) {
// Query commits // Query commits
commits, err := baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize) commits, err := baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize)
if err != nil { if err != nil {
ctx.ServerError("CommitsByRange", err) ctx.Error(http.StatusInternalServerError, "CommitsByRange", err)
return return
} }
@ -195,7 +195,7 @@ func GetAllCommits(ctx *context.APIContext) {
// Create json struct // Create json struct
apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache) apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache)
if err != nil { if err != nil {
ctx.ServerError("toCommit", err) ctx.Error(http.StatusInternalServerError, "toCommit", err)
return return
} }

View File

@ -109,7 +109,7 @@ func CreateFork(ctx *context.APIContext, form api.CreateForkOption) {
} }
isMember, err := org.IsOrgMember(ctx.User.ID) isMember, err := org.IsOrgMember(ctx.User.ID)
if err != nil { if err != nil {
ctx.ServerError("IsOrgMember", err) ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
return return
} else if !isMember { } else if !isMember {
ctx.Error(http.StatusForbidden, "isMemberNot", fmt.Sprintf("User is no Member of Organisation '%s'", org.Name)) ctx.Error(http.StatusForbidden, "isMemberNot", fmt.Sprintf("User is no Member of Organisation '%s'", org.Name))

View File

@ -222,7 +222,7 @@ func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
label.Description = *form.Description label.Description = *form.Description
} }
if err := models.UpdateLabel(label); err != nil { if err := models.UpdateLabel(label); err != nil {
ctx.ServerError("UpdateLabel", err) ctx.Error(http.StatusInternalServerError, "UpdateLabel", err)
return return
} }
ctx.JSON(http.StatusOK, convert.ToLabel(label)) ctx.JSON(http.StatusOK, convert.ToLabel(label))

View File

@ -215,7 +215,7 @@ func EditMilestone(ctx *context.APIContext, form api.EditMilestoneOption) {
} }
if err := models.UpdateMilestone(milestone, oldIsClosed); err != nil { if err := models.UpdateMilestone(milestone, oldIsClosed); err != nil {
ctx.ServerError("UpdateMilestone", err) ctx.Error(http.StatusInternalServerError, "UpdateMilestone", err)
return return
} }
ctx.JSON(http.StatusOK, convert.ToAPIMilestone(milestone)) ctx.JSON(http.StatusOK, convert.ToAPIMilestone(milestone))

View File

@ -725,7 +725,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
} }
if err = pr.LoadHeadRepo(); err != nil { if err = pr.LoadHeadRepo(); err != nil {
ctx.ServerError("LoadHeadRepo", err) ctx.Error(http.StatusInternalServerError, "LoadHeadRepo", err)
return return
} }
@ -885,7 +885,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
if models.IsErrUserNotExist(err) { if models.IsErrUserNotExist(err) {
ctx.NotFound("GetUserByName") ctx.NotFound("GetUserByName")
} else { } else {
ctx.ServerError("GetUserByName", err) ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
} }
return nil, nil, nil, nil, "", "" return nil, nil, nil, nil, "", ""
} }
@ -929,7 +929,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
permBase, err := models.GetUserRepoPermission(baseRepo, ctx.User) permBase, err := models.GetUserRepoPermission(baseRepo, ctx.User)
if err != nil { if err != nil {
headGitRepo.Close() headGitRepo.Close()
ctx.ServerError("GetUserRepoPermission", err) ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
return nil, nil, nil, nil, "", "" return nil, nil, nil, nil, "", ""
} }
if !permBase.CanReadIssuesOrPulls(true) || !permBase.CanRead(models.UnitTypeCode) { 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) permHead, err := models.GetUserRepoPermission(headRepo, ctx.User)
if err != nil { if err != nil {
headGitRepo.Close() headGitRepo.Close()
ctx.ServerError("GetUserRepoPermission", err) ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
return nil, nil, nil, nil, "", "" return nil, nil, nil, nil, "", ""
} }
if !permHead.CanRead(models.UnitTypeCode) { if !permHead.CanRead(models.UnitTypeCode) {

View File

@ -318,14 +318,14 @@ func CreatePullReview(ctx *context.APIContext, opts api.CreatePullReviewOptions)
if opts.CommitID == "" { if opts.CommitID == "" {
gitRepo, err := git.OpenRepository(pr.Issue.Repo.RepoPath()) gitRepo, err := git.OpenRepository(pr.Issue.Repo.RepoPath())
if err != nil { if err != nil {
ctx.ServerError("git.OpenRepository", err) ctx.Error(http.StatusInternalServerError, "git.OpenRepository", err)
return return
} }
defer gitRepo.Close() defer gitRepo.Close()
headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName()) headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
if err != nil { if err != nil {
ctx.ServerError("GetRefCommitID", err) ctx.Error(http.StatusInternalServerError, "GetRefCommitID", err)
return return
} }
@ -350,7 +350,7 @@ func CreatePullReview(ctx *context.APIContext, opts api.CreatePullReviewOptions)
0, // no reply 0, // no reply
opts.CommitID, opts.CommitID,
); err != nil { ); err != nil {
ctx.ServerError("CreateCodeComment", err) ctx.Error(http.StatusInternalServerError, "CreateCodeComment", err)
return return
} }
} }

View File

@ -151,7 +151,7 @@ func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) {
rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName) rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName)
if err != nil { if err != nil {
if !models.IsErrReleaseNotExist(err) { if !models.IsErrReleaseNotExist(err) {
ctx.ServerError("GetRelease", err) ctx.Error(http.StatusInternalServerError, "GetRelease", err)
return return
} }
// If target is not provided use default branch // If target is not provided use default branch
@ -195,7 +195,7 @@ func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) {
rel.Publisher = ctx.User rel.Publisher = ctx.User
if err = releaseservice.UpdateReleaseOrCreatReleaseFromTag(ctx.User, ctx.Repo.GitRepo, rel, nil, true); err != nil { 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 return
} }
} }

View File

@ -373,7 +373,7 @@ func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) {
if !ctx.User.IsAdmin { if !ctx.User.IsAdmin {
canCreate, err := org.CanCreateOrgRepo(ctx.User.ID) canCreate, err := org.CanCreateOrgRepo(ctx.User.ID)
if err != nil { if err != nil {
ctx.ServerError("CanCreateOrgRepo", err) ctx.Error(http.StatusInternalServerError, "CanCreateOrgRepo", err)
return return
} else if !canCreate { } else if !canCreate {
ctx.Error(http.StatusForbidden, "", "Given user is not allowed to create repository in organization.") ctx.Error(http.StatusForbidden, "", "Given user is not allowed to create repository in organization.")