refactor: simplify APIError message arguments

This commit is contained in:
copilot-swe-agent[bot]
2026-06-06 12:18:41 +00:00
committed by GitHub
parent c96b4fe019
commit d924988534
19 changed files with 48 additions and 48 deletions
+6 -6
View File
@@ -112,7 +112,7 @@ func CreateUser(ctx *context.APIContext) {
if password.IsErrIsPwnedRequest(err) {
log.Error(err.Error())
}
ctx.APIError(http.StatusBadRequest, errors.New("PasswordPwned").Error())
ctx.APIError(http.StatusBadRequest, "PasswordPwned")
return
}
}
@@ -204,7 +204,7 @@ func EditUser(ctx *context.APIContext) {
if err := user_service.UpdateAuth(ctx, ctx.ContextUser, authOpts); err != nil {
switch {
case errors.Is(err, password.ErrMinLength):
ctx.APIError(http.StatusBadRequest, fmt.Errorf("password must be at least %d characters", setting.MinPasswordLength).Error())
ctx.APIError(http.StatusBadRequest, fmt.Sprintf("password must be at least %d characters", setting.MinPasswordLength))
case errors.Is(err, password.ErrComplexity):
ctx.APIError(http.StatusBadRequest, err.Error())
case errors.Is(err, password.ErrIsPwned), password.IsErrIsPwnedRequest(err):
@@ -289,13 +289,13 @@ func DeleteUser(ctx *context.APIContext) {
// "$ref": "#/responses/validationError"
if ctx.ContextUser.IsOrganization() {
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("%s is an organization not a user", ctx.ContextUser.Name).Error())
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("%s is an organization not a user", ctx.ContextUser.Name))
return
}
// admin should not delete themself
if ctx.ContextUser.ID == ctx.Doer.ID {
ctx.APIError(http.StatusUnprocessableEntity, errors.New("you cannot delete yourself").Error())
ctx.APIError(http.StatusUnprocessableEntity, "you cannot delete yourself")
return
}
@@ -475,7 +475,7 @@ func SearchUsers(ctx *context.APIContext) {
if visibility, ok := api.VisibilityModes[visibilityParam]; ok {
visible = []api.VisibleType{visibility}
} else {
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("Invalid visibility: \"%s\"", visibilityParam).Error())
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid visibility: \"%s\"", visibilityParam))
return
}
}
@@ -551,7 +551,7 @@ func RenameUser(ctx *context.APIContext) {
// "$ref": "#/responses/validationError"
if ctx.ContextUser.IsOrganization() {
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("%s is an organization not a user", ctx.ContextUser.Name).Error())
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("%s is an organization not a user", ctx.ContextUser.Name))
return
}
+2 -2
View File
@@ -734,14 +734,14 @@ func mustEnableWiki(ctx *context.APIContext) {
// FIXME: for consistency, maybe most mustNotBeArchived checks should be replaced with mustEnableEditor
func mustNotBeArchived(ctx *context.APIContext) {
if ctx.Repo.Repository.IsArchived {
ctx.APIError(http.StatusLocked, fmt.Errorf("%s is archived", ctx.Repo.Repository.FullName()).Error())
ctx.APIError(http.StatusLocked, fmt.Sprintf("%s is archived", ctx.Repo.Repository.FullName()))
return
}
}
func mustEnableEditor(ctx *context.APIContext) {
if !ctx.Repo.Repository.CanEnableEditor() {
ctx.APIError(http.StatusLocked, fmt.Errorf("%s is not allowed to edit", ctx.Repo.Repository.FullName()).Error())
ctx.APIError(http.StatusLocked, fmt.Sprintf("%s is not allowed to edit", ctx.Repo.Repository.FullName()))
return
}
}
+2 -2
View File
@@ -155,9 +155,9 @@ func DeleteBranch(ctx *context.APIContext) {
case git.IsErrBranchNotExist(err):
ctx.APIErrorNotFound(err)
case errors.Is(err, repo_service.ErrBranchIsDefault):
ctx.APIError(http.StatusForbidden, errors.New("can not delete default or pull request target branch").Error())
ctx.APIError(http.StatusForbidden, "can not delete default or pull request target branch")
case errors.Is(err, git_model.ErrBranchIsProtected):
ctx.APIError(http.StatusForbidden, errors.New("branch protected").Error())
ctx.APIError(http.StatusForbidden, "branch protected")
default:
ctx.APIErrorInternal(err)
}
+1 -1
View File
@@ -326,7 +326,7 @@ func GetReviewers(ctx *context.APIContext) {
canChooseReviewer := issue_service.CanDoerChangeReviewRequests(ctx, ctx.Doer, ctx.Repo.Repository, 0)
if !canChooseReviewer {
ctx.APIError(http.StatusForbidden, errors.New("doer has no permission to get reviewers").Error())
ctx.APIError(http.StatusForbidden, "doer has no permission to get reviewers")
return
}
+1 -1
View File
@@ -392,7 +392,7 @@ func CreateIssueComment(ctx *context.APIContext) {
}
if issue.IsLocked && !ctx.Repo.Permission.CanWriteIssuesOrPulls(issue.IsPull) && !ctx.Doer.IsAdmin {
ctx.APIError(http.StatusForbidden, errors.New(ctx.Locale.TrString("repo.issues.comment_on_locked")).Error())
ctx.APIError(http.StatusForbidden, ctx.Locale.TrString("repo.issues.comment_on_locked"))
return
}
+2 -2
View File
@@ -63,7 +63,7 @@ func LockIssue(ctx *context.APIContext) {
}
if !ctx.Repo.Permission.CanWriteIssuesOrPulls(issue.IsPull) {
ctx.APIError(http.StatusForbidden, errors.New("no permission to lock this issue").Error())
ctx.APIError(http.StatusForbidden, "no permission to lock this issue")
return
}
@@ -130,7 +130,7 @@ func UnlockIssue(ctx *context.APIContext) {
}
if !ctx.Repo.Permission.CanWriteIssuesOrPulls(issue.IsPull) {
ctx.APIError(http.StatusForbidden, errors.New("no permission to unlock this issue").Error())
ctx.APIError(http.StatusForbidden, "no permission to unlock this issue")
return
}
+4 -4
View File
@@ -72,7 +72,7 @@ func GetIssueCommentReactions(ctx *context.APIContext) {
}
if !ctx.Repo.Permission.CanReadIssuesOrPulls(comment.Issue.IsPull) {
ctx.APIError(http.StatusForbidden, errors.New("no permission to get reactions").Error())
ctx.APIError(http.StatusForbidden, "no permission to get reactions")
return
}
@@ -214,7 +214,7 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp
}
if comment.Issue.IsLocked && !ctx.Repo.Permission.CanWriteIssuesOrPulls(comment.Issue.IsPull) {
ctx.APIError(http.StatusForbidden, errors.New("no permission to change reaction").Error())
ctx.APIError(http.StatusForbidden, "no permission to change reaction")
return
}
@@ -305,7 +305,7 @@ func GetIssueReactions(ctx *context.APIContext) {
}
if !ctx.Repo.Permission.CanReadIssuesOrPulls(issue.IsPull) {
ctx.APIError(http.StatusForbidden, errors.New("no permission to get reactions").Error())
ctx.APIError(http.StatusForbidden, "no permission to get reactions")
return
}
@@ -429,7 +429,7 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i
}
if issue.IsLocked && !ctx.Repo.Permission.CanWriteIssuesOrPulls(issue.IsPull) {
ctx.APIError(http.StatusForbidden, errors.New("no permission to change reaction").Error())
ctx.APIError(http.StatusForbidden, "no permission to change reaction")
return
}
+1 -1
View File
@@ -128,7 +128,7 @@ func setIssueSubscription(ctx *context.APIContext, watch bool) {
// only admin and user for itself can change subscription
if user.ID != ctx.Doer.ID && !ctx.Doer.IsAdmin {
ctx.APIError(http.StatusForbidden, fmt.Errorf("%s is not permitted to change subscriptions for %s", ctx.Doer.Name, user.Name).Error())
ctx.APIError(http.StatusForbidden, fmt.Sprintf("%s is not permitted to change subscriptions for %s", ctx.Doer.Name, user.Name))
return
}
+3 -3
View File
@@ -116,7 +116,7 @@ func ListTrackedTimes(ctx *context.APIContext) {
if opts.UserID == 0 {
opts.UserID = ctx.Doer.ID
} else {
ctx.APIError(http.StatusForbidden, errors.New("query by user not allowed; not enough rights").Error())
ctx.APIError(http.StatusForbidden, "query by user not allowed; not enough rights")
return
}
}
@@ -437,7 +437,7 @@ func ListTrackedTimesByUser(ctx *context.APIContext) {
}
if !ctx.IsUserRepoAdmin() && !ctx.Doer.IsAdmin && ctx.Doer.ID != user.ID {
ctx.APIError(http.StatusForbidden, errors.New("query by user not allowed; not enough rights").Error())
ctx.APIError(http.StatusForbidden, "query by user not allowed; not enough rights")
return
}
@@ -545,7 +545,7 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
if opts.UserID == 0 {
opts.UserID = ctx.Doer.ID
} else {
ctx.APIError(http.StatusForbidden, errors.New("query by user not allowed; not enough rights").Error())
ctx.APIError(http.StatusForbidden, "query by user not allowed; not enough rights")
return
}
}
+1 -1
View File
@@ -179,7 +179,7 @@ func HandleCheckKeyStringError(ctx *context.APIContext, err error) {
} else if asymkey_model.IsErrKeyUnableVerify(err) {
ctx.APIError(http.StatusUnprocessableEntity, "Unable to verify key content")
} else {
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("Invalid key content: %w", err).Error())
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid key content: %v", err))
}
}
+2 -2
View File
@@ -110,12 +110,12 @@ func Migrate(ctx *context.APIContext) {
gitServiceType := convert.ToGitServiceType(form.Service)
if form.Mirror && setting.Mirror.DisableNewPull {
ctx.APIError(http.StatusForbidden, errors.New("the site administrator has disabled the creation of new pull mirrors").Error())
ctx.APIError(http.StatusForbidden, "the site administrator has disabled the creation of new pull mirrors")
return
}
if setting.Repository.DisableMigrations {
ctx.APIError(http.StatusForbidden, errors.New("the site administrator has disabled migrations").Error())
ctx.APIError(http.StatusForbidden, "the site administrator has disabled migrations")
return
}
+3 -3
View File
@@ -788,7 +788,7 @@ func EditPullRequest(ctx *context.APIContext) {
return
}
if !branchExist {
ctx.APIError(http.StatusNotFound, fmt.Errorf("new base '%s' not exist", form.Base).Error())
ctx.APIError(http.StatusNotFound, fmt.Sprintf("new base '%s' not exist", form.Base))
return
}
if err := pull_service.ChangeTargetBranch(ctx, pr, ctx.Doer, form.Base); err != nil {
@@ -992,7 +992,7 @@ func MergePullRequest(ctx *context.APIContext) {
if manuallyMerged {
if err := pull_service.MergedManually(ctx, pr, ctx.Doer, ctx.Repo.GitRepo, form.MergeCommitID); err != nil {
if pull_service.IsErrInvalidMergeStyle(err) {
ctx.APIError(http.StatusMethodNotAllowed, fmt.Errorf("%s is not allowed an allowed merge style for this repository", repo_model.MergeStyle(form.Do)).Error())
ctx.APIError(http.StatusMethodNotAllowed, fmt.Sprintf("%s is not allowed an allowed merge style for this repository", repo_model.MergeStyle(form.Do)))
return
}
if strings.Contains(err.Error(), "Wrong commit ID") {
@@ -1048,7 +1048,7 @@ func MergePullRequest(ctx *context.APIContext) {
if err := pull_service.Merge(ctx, pr, ctx.Doer, repo_model.MergeStyle(form.Do), form.HeadCommitID, message, false); err != nil {
if pull_service.IsErrInvalidMergeStyle(err) {
ctx.APIError(http.StatusMethodNotAllowed, fmt.Errorf("%s is not allowed an allowed merge style for this repository", repo_model.MergeStyle(form.Do)).Error())
ctx.APIError(http.StatusMethodNotAllowed, fmt.Sprintf("%s is not allowed an allowed merge style for this repository", repo_model.MergeStyle(form.Do)))
} else if pull_service.IsErrMergeConflicts(err) {
conflictError := err.(pull_service.ErrMergeConflicts)
ctx.JSON(http.StatusConflict, conflictError)
+6 -6
View File
@@ -641,7 +641,7 @@ func SubmitPullReview(ctx *context.APIContext) {
}
if review.Type != issues_model.ReviewTypePending {
ctx.APIError(http.StatusUnprocessableEntity, errors.New("only a pending review can be submitted").Error())
ctx.APIError(http.StatusUnprocessableEntity, "only a pending review can be submitted")
return
}
@@ -653,7 +653,7 @@ func SubmitPullReview(ctx *context.APIContext) {
// if review stay pending return
if reviewType == issues_model.ReviewTypePending {
ctx.APIError(http.StatusUnprocessableEntity, errors.New("review stay pending").Error())
ctx.APIError(http.StatusUnprocessableEntity, "review stay pending")
return
}
@@ -698,7 +698,7 @@ func preparePullReviewType(ctx *context.APIContext, pr *issues_model.PullRequest
case api.ReviewStateApproved:
// can not approve your own PR
if pr.Issue.IsPoster(ctx.Doer.ID) {
ctx.APIError(http.StatusUnprocessableEntity, errors.New("approve your own pull is not allowed").Error())
ctx.APIError(http.StatusUnprocessableEntity, "approve your own pull is not allowed")
return -1, true
}
reviewType = issues_model.ReviewTypeApprove
@@ -707,7 +707,7 @@ func preparePullReviewType(ctx *context.APIContext, pr *issues_model.PullRequest
case api.ReviewStateRequestChanges:
// can not reject your own PR
if pr.Issue.IsPoster(ctx.Doer.ID) {
ctx.APIError(http.StatusUnprocessableEntity, errors.New("reject your own pull is not allowed").Error())
ctx.APIError(http.StatusUnprocessableEntity, "reject your own pull is not allowed")
return -1, true
}
reviewType = issues_model.ReviewTypeReject
@@ -717,7 +717,7 @@ func preparePullReviewType(ctx *context.APIContext, pr *issues_model.PullRequest
needsBody = false
// if there is no body we need to ensure that there are comments
if !hasBody && !hasComments {
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("review event %s requires a body or a comment", event).Error())
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("review event %s requires a body or a comment", event))
return -1, true
}
default:
@@ -726,7 +726,7 @@ func preparePullReviewType(ctx *context.APIContext, pr *issues_model.PullRequest
// reject reviews with empty body if a body is required for this call
if needsBody && !hasBody {
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("review event %s requires a body", event).Error())
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("review event %s requires a body", event))
return -1, true
}
+2 -2
View File
@@ -243,7 +243,7 @@ func CreateRelease(ctx *context.APIContext) {
form := web.GetForm(ctx).(*api.CreateReleaseOption)
if ctx.Repo.Repository.IsEmpty {
ctx.APIError(http.StatusUnprocessableEntity, errors.New("repo is empty").Error())
ctx.APIError(http.StatusUnprocessableEntity, "repo is empty")
return
}
rel, err := repo_model.GetRelease(ctx, ctx.Repo.Repository.ID, form.TagName)
@@ -277,7 +277,7 @@ func CreateRelease(ctx *context.APIContext) {
} else if release_service.IsErrProtectedTagName(err) {
ctx.APIError(http.StatusUnprocessableEntity, err.Error())
} else if git.IsErrNotExist(err) {
ctx.APIError(http.StatusNotFound, fmt.Errorf("target \"%v\" not found: %w", rel.Target, err).Error())
ctx.APIError(http.StatusNotFound, fmt.Sprintf("target \"%v\" not found: %v", rel.Target, err))
} else {
ctx.APIErrorInternal(err)
}
+3 -3
View File
@@ -173,7 +173,7 @@ func Search(ctx *context.APIContext) {
opts.Collaborate = optional.Some(true)
case "":
default:
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("Invalid search mode: \"%s\"", mode).Error())
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid search mode: \"%s\"", mode))
return
}
@@ -234,7 +234,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre
// If the readme template does not exist, a 400 will be returned.
if opt.AutoInit && len(opt.Readme) > 0 && !slices.Contains(repo_module.Readmes, opt.Readme) {
ctx.APIError(http.StatusBadRequest, fmt.Errorf("readme template does not exist, available templates: %v", repo_module.Readmes).Error())
ctx.APIError(http.StatusBadRequest, fmt.Sprintf("readme template does not exist, available templates: %v", repo_module.Readmes))
return
}
@@ -658,7 +658,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
case db.IsErrNamePatternNotAllowed(err):
ctx.APIError(http.StatusUnprocessableEntity, err.Error())
default:
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("ChangeRepositoryName: %w", err).Error())
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("ChangeRepositoryName: %v", err))
}
return err
}
+2 -2
View File
@@ -203,7 +203,7 @@ func CreateTag(ctx *context.APIContext) {
commit, err := ctx.Repo.GitRepo.GetCommit(form.Target)
if err != nil {
ctx.APIError(http.StatusNotFound, fmt.Errorf("target not found: %w", err).Error())
ctx.APIError(http.StatusNotFound, fmt.Sprintf("target not found: %v", err))
return
}
@@ -278,7 +278,7 @@ func DeleteTag(ctx *context.APIContext) {
}
if !tag.IsTag {
ctx.APIError(http.StatusConflict, errors.New("a tag attached to a release cannot be deleted directly").Error())
ctx.APIError(http.StatusConflict, "a tag attached to a release cannot be deleted directly")
return
}
+2 -2
View File
@@ -201,13 +201,13 @@ func changeRepoTeam(ctx *context.APIContext, add bool) {
var err error
if add {
if repoHasTeam {
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("team '%s' is already added to repo", team.Name).Error())
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("team '%s' is already added to repo", team.Name))
return
}
err = repo_service.TeamAddRepository(ctx, team, ctx.Repo.Repository)
} else {
if !repoHasTeam {
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("team '%s' was not added to repo", team.Name).Error())
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("team '%s' was not added to repo", team.Name))
return
}
err = repo_service.RemoveRepositoryFromTeam(ctx, team, ctx.Repo.Repository.ID)
+2 -2
View File
@@ -87,12 +87,12 @@ func Transfer(ctx *context.APIContext) {
for _, tID := range *opts.TeamIDs {
team, err := organization.GetTeamByID(ctx, tID)
if err != nil {
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("team %d not found", tID).Error())
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("team %d not found", tID))
return
}
if team.OrgID != org.ID {
ctx.APIError(http.StatusForbidden, fmt.Errorf("team %d belongs not to org %d", tID, org.ID).Error())
ctx.APIError(http.StatusForbidden, fmt.Sprintf("team %d belongs not to org %d", tID, org.ID))
return
}
+3 -3
View File
@@ -112,13 +112,13 @@ func CreateAccessToken(ctx *context.APIContext) {
return
}
if exist {
ctx.APIError(http.StatusBadRequest, errors.New("access token name has been used already").Error())
ctx.APIError(http.StatusBadRequest, "access token name has been used already")
return
}
scope, err := auth_model.AccessTokenScope(strings.Join(form.Scopes, ",")).Normalize()
if err != nil {
ctx.APIError(http.StatusBadRequest, fmt.Errorf("invalid access token scope provided: %w", err).Error())
ctx.APIError(http.StatusBadRequest, fmt.Sprintf("invalid access token scope provided: %v", err))
return
}
if scope == "" {
@@ -188,7 +188,7 @@ func DeleteAccessToken(ctx *context.APIContext) {
case 1:
tokenID = tokens[0].ID
default:
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("multiple matches for token name '%s'", token).Error())
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("multiple matches for token name '%s'", token))
return
}
}