Browse Source

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>
master
赵智超 4 years ago
committed by GitHub
parent
commit
fec1521555
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 26 additions and 28 deletions
  1. +3
    -5
      routers/api/v1/misc/signing.go
  2. +1
    -1
      routers/api/v1/org/label.go
  3. +9
    -9
      routers/api/v1/repo/commits.go
  4. +1
    -1
      routers/api/v1/repo/fork.go
  5. +1
    -1
      routers/api/v1/repo/label.go
  6. +1
    -1
      routers/api/v1/repo/milestone.go
  7. +4
    -4
      routers/api/v1/repo/pull.go
  8. +3
    -3
      routers/api/v1/repo/pull_review.go
  9. +2
    -2
      routers/api/v1/repo/release.go
  10. +1
    -1
      routers/api/v1/repo/repo.go

+ 3
- 5
routers/api/v1/misc/signing.go View File

@ -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))
}
}

+ 1
- 1
routers/api/v1/org/label.go View File

@ -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))

+ 9
- 9
routers/api/v1/repo/commits.go View File

@ -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
}

+ 1
- 1
routers/api/v1/repo/fork.go View File

@ -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))

+ 1
- 1
routers/api/v1/repo/label.go View File

@ -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))

+ 1
- 1
routers/api/v1/repo/milestone.go View File

@ -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))

+ 4
- 4
routers/api/v1/repo/pull.go View File

@ -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) {

+ 3
- 3
routers/api/v1/repo/pull_review.go View File

@ -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
}
}

+ 2
- 2
routers/api/v1/repo/release.go View File

@ -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
}
}

+ 1
- 1
routers/api/v1/repo/repo.go View File

@ -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.")

Loading…
Cancel
Save