Handle error object in API and use HTTP status 422

This commit is contained in:
Kemal Zebari 2024-04-24 11:36:19 -07:00
parent 6710ff41f0
commit f484e3d44e
3 changed files with 14 additions and 6 deletions

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
}

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

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
}