Browse Source

Handle error object in API and use HTTP status 422

pull/30686/head
Kemal Zebari 1 week ago
parent
commit
f484e3d44e
3 changed files with 14 additions and 6 deletions
  1. 10
    2
      routers/api/v1/repo/pull_review.go
  2. 2
    2
      routers/web/repo/pull_review.go
  3. 2
    2
      services/pull/review.go

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

@@ -372,7 +372,11 @@ func CreatePullReview(ctx *context.APIContext) {
// create review and associate all pending review comments
review, _, err := pull_service.SubmitReview(ctx, ctx.Doer, ctx.Repo.GitRepo, pr.Issue, reviewType, opts.Body, opts.CommitID, nil)
if err != nil {
ctx.Error(http.StatusInternalServerError, "SubmitReview", err)
if pull_service.IsErrSubmitReviewOnClosedPR(err) {
ctx.Error(http.StatusUnprocessableEntity, "", err)
} else {
ctx.Error(http.StatusInternalServerError, "SubmitReview", err)
}
return
}

@@ -460,7 +464,11 @@ func SubmitPullReview(ctx *context.APIContext) {
// create review and associate all pending review comments
review, _, err = pull_service.SubmitReview(ctx, ctx.Doer, ctx.Repo.GitRepo, pr.Issue, reviewType, opts.Body, headCommitID, nil)
if err != nil {
ctx.Error(http.StatusInternalServerError, "SubmitReview", err)
if pull_service.IsErrSubmitReviewOnClosedPR(err) {
ctx.Error(http.StatusUnprocessableEntity, "", err)
} else {
ctx.Error(http.StatusInternalServerError, "SubmitReview", err)
}
return
}


+ 2
- 2
routers/web/repo/pull_review.go View File

@@ -264,8 +264,8 @@ func SubmitReview(ctx *context.Context) {
if issues_model.IsContentEmptyErr(err) {
ctx.Flash.Error(ctx.Tr("repo.issues.review.content.empty"))
ctx.JSONRedirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
} else if pull_service.IsSubmitReviewOnClosedPR(err) {
ctx.Error(http.StatusForbidden)
} else if pull_service.IsErrSubmitReviewOnClosedPR(err) {
ctx.Error(http.StatusUnprocessableEntity)
} else {
ctx.ServerError("SubmitReview", err)
}

+ 2
- 2
services/pull/review.go View File

@@ -46,8 +46,8 @@ func (err ErrDismissRequestOnClosedPR) Unwrap() error {
// ErrSubmitReviewOnClosedPR represents an error when an user tries to submit an approve or reject review associated to a closed or merged PR.
type ErrSubmitReviewOnClosedPR struct{}

// IsSubmitReviewOnClosedPR checks if an error is an ErrSubmitReviewOnClosedPR.
func IsSubmitReviewOnClosedPR(err error) bool {
// IsErrSubmitReviewOnClosedPR checks if an error is an ErrSubmitReviewOnClosedPR.
func IsErrSubmitReviewOnClosedPR(err error) bool {
_, ok := err.(ErrSubmitReviewOnClosedPR)
return ok
}

Loading…
Cancel
Save