fix: api error message (#38031)

Fix various abuses and mistakes
This commit is contained in:
wxiaoguang
2026-06-08 16:58:42 +08:00
committed by GitHub
parent 60f66a9bfd
commit 136f7d18aa
27 changed files with 80 additions and 256 deletions
+4 -4
View File
@@ -151,7 +151,7 @@ func repoAssignment() func(ctx *context.APIContext) {
if redirectUserID, err := user_model.LookupUserRedirect(ctx, userName); err == nil { if redirectUserID, err := user_model.LookupUserRedirect(ctx, userName); err == nil {
context.RedirectToUser(ctx.Base, ctx.Doer, userName, redirectUserID) context.RedirectToUser(ctx.Base, ctx.Doer, userName, redirectUserID)
} else if user_model.IsErrUserRedirectNotExist(err) { } else if user_model.IsErrUserRedirectNotExist(err) {
ctx.APIErrorNotFound("GetUserByName", err) ctx.APIErrorNotFound()
} else { } else {
ctx.APIErrorInternal(err) ctx.APIErrorInternal(err)
} }
@@ -626,7 +626,7 @@ func orgAssignment(args ...bool) func(ctx *context.APIContext) {
if err == nil { if err == nil {
context.RedirectToUser(ctx.Base, ctx.Doer, ctx.PathParam("org"), redirectUserID) context.RedirectToUser(ctx.Base, ctx.Doer, ctx.PathParam("org"), redirectUserID)
} else if user_model.IsErrUserRedirectNotExist(err) { } else if user_model.IsErrUserRedirectNotExist(err) {
ctx.APIErrorNotFound("GetOrgByName", err) ctx.APIErrorNotFound()
} else { } else {
ctx.APIErrorInternal(err) ctx.APIErrorInternal(err)
} }
@@ -862,12 +862,12 @@ func individualPermsChecker(ctx *context.APIContext) {
switch ctx.ContextUser.Visibility { switch ctx.ContextUser.Visibility {
case api.VisibleTypePrivate: case api.VisibleTypePrivate:
if ctx.Doer == nil || (ctx.ContextUser.ID != ctx.Doer.ID && !ctx.Doer.IsAdmin) { if ctx.Doer == nil || (ctx.ContextUser.ID != ctx.Doer.ID && !ctx.Doer.IsAdmin) {
ctx.APIErrorNotFound("Visit Project", nil) ctx.APIErrorNotFound()
return return
} }
case api.VisibleTypeLimited: case api.VisibleTypeLimited:
if ctx.Doer == nil { if ctx.Doer == nil {
ctx.APIErrorNotFound("Visit Project", nil) ctx.APIErrorNotFound()
return return
} }
} }
+2 -2
View File
@@ -146,7 +146,7 @@ func GetUserOrgsPermissions(ctx *context.APIContext) {
op := api.OrganizationPermissions{} op := api.OrganizationPermissions{}
if !organization.HasOrgOrUserVisible(ctx, o, ctx.Doer) { if !organization.HasOrgOrUserVisible(ctx, o, ctx.Doer) {
ctx.APIErrorNotFound("HasOrgOrUserVisible", nil) ctx.APIErrorNotFound()
return return
} }
@@ -312,7 +312,7 @@ func Get(ctx *context.APIContext) {
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
if !organization.HasOrgOrUserVisible(ctx, ctx.Org.Organization.AsUser(), ctx.Doer) { if !organization.HasOrgOrUserVisible(ctx, ctx.Org.Organization.AsUser(), ctx.Doer) {
ctx.APIErrorNotFound("HasOrgOrUserVisible", nil) ctx.APIErrorNotFound()
return return
} }
+7 -17
View File
@@ -1164,11 +1164,8 @@ func ActionsEnableWorkflow(ctx *context.APIContext) {
func getCurrentRepoActionRunByID(ctx *context.APIContext) *actions_model.ActionRun { func getCurrentRepoActionRunByID(ctx *context.APIContext) *actions_model.ActionRun {
runID := ctx.PathParamInt64("run") runID := ctx.PathParamInt64("run")
run, err := actions_model.GetRunByRepoAndID(ctx, ctx.Repo.Repository.ID, runID) run, err := actions_model.GetRunByRepoAndID(ctx, ctx.Repo.Repository.ID, runID)
if errors.Is(err, util.ErrNotExist) { if err != nil {
ctx.APIErrorNotFound(err) ctx.APIErrorAuto(err)
return nil
} else if err != nil {
ctx.APIErrorInternal(err)
return nil return nil
} }
run.Repo = ctx.Repo.Repository run.Repo = ctx.Repo.Repository
@@ -1198,11 +1195,8 @@ func getCurrentRepoActionRunAttemptByNumber(ctx *context.APIContext) (*actions_m
attemptNum := ctx.PathParamInt64("attempt") attemptNum := ctx.PathParamInt64("attempt")
attempt, err := actions_model.GetRunAttemptByRunIDAndAttemptNum(ctx, run.ID, attemptNum) attempt, err := actions_model.GetRunAttemptByRunIDAndAttemptNum(ctx, run.ID, attemptNum)
if errors.Is(err, util.ErrNotExist) { if err != nil {
ctx.APIErrorNotFound(err) ctx.APIErrorAuto(err)
return nil, nil
} else if err != nil {
ctx.APIErrorInternal(err)
return nil, nil return nil, nil
} }
return run, attempt return run, attempt
@@ -1454,7 +1448,7 @@ func RerunWorkflowJob(ctx *context.APIContext) {
jobID := ctx.PathParamInt64("job_id") jobID := ctx.PathParamInt64("job_id")
jobIdx := slices.IndexFunc(jobs, func(job *actions_model.ActionRunJob) bool { return job.ID == jobID }) jobIdx := slices.IndexFunc(jobs, func(job *actions_model.ActionRunJob) bool { return job.ID == jobID })
if jobIdx == -1 { if jobIdx == -1 {
ctx.APIErrorNotFound(util.NewNotExistErrorf("workflow job with id %d", jobID)) ctx.APIErrorNotFound("workflow job not found")
return return
} }
@@ -1566,11 +1560,7 @@ func ListWorkflowRunJobs(ctx *context.APIContext) {
run, err := actions_model.GetRunByRepoAndID(ctx, repoID, runID) run, err := actions_model.GetRunByRepoAndID(ctx, repoID, runID)
if err != nil { if err != nil {
if errors.Is(err, util.ErrNotExist) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
// runID is used as an additional filter next to repoID to ensure that we only list jobs for the specified repoID and runID. // runID is used as an additional filter next to repoID to ensure that we only list jobs for the specified repoID and runID.
@@ -1674,7 +1664,7 @@ func GetWorkflowJob(ctx *context.APIContext) {
} }
if !has || job.RepoID != ctx.Repo.Repository.ID { if !has || job.RepoID != ctx.Repo.Repository.ID {
ctx.APIErrorNotFound(util.ErrNotExist) ctx.APIErrorNotFound()
return return
} }
+2 -13
View File
@@ -4,10 +4,7 @@
package repo package repo
import ( import (
"errors"
actions_model "gitea.dev/models/actions" actions_model "gitea.dev/models/actions"
"gitea.dev/modules/util"
"gitea.dev/routers/common" "gitea.dev/routers/common"
"gitea.dev/services/context" "gitea.dev/services/context"
) )
@@ -45,11 +42,7 @@ func DownloadActionsRunJobLogs(ctx *context.APIContext) {
jobID := ctx.PathParamInt64("job_id") jobID := ctx.PathParamInt64("job_id")
curJob, err := actions_model.GetRunJobByRepoAndID(ctx, ctx.Repo.Repository.ID, jobID) curJob, err := actions_model.GetRunJobByRepoAndID(ctx, ctx.Repo.Repository.ID, jobID)
if err != nil { if err != nil {
if errors.Is(err, util.ErrNotExist) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
if err = curJob.LoadRepo(ctx); err != nil { if err = curJob.LoadRepo(ctx); err != nil {
@@ -59,10 +52,6 @@ func DownloadActionsRunJobLogs(ctx *context.APIContext) {
err = common.DownloadActionsRunJobLogs(ctx.Base, ctx.Repo.Repository, curJob) err = common.DownloadActionsRunJobLogs(ctx.Base, ctx.Repo.Repository, curJob)
if err != nil { if err != nil {
if errors.Is(err, util.ErrNotExist) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
} }
} }
+3 -3
View File
@@ -64,7 +64,7 @@ func GetBranch(ctx *context.APIContext) {
ctx.APIErrorInternal(err) ctx.APIErrorInternal(err)
return return
} else if !exist { } else if !exist {
ctx.APIErrorNotFound(err) ctx.APIErrorNotFound()
return return
} }
@@ -153,7 +153,7 @@ func DeleteBranch(ctx *context.APIContext) {
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil { if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
switch { switch {
case git.IsErrBranchNotExist(err): case git.IsErrBranchNotExist(err):
ctx.APIErrorNotFound(err) ctx.APIErrorNotFound()
case errors.Is(err, repo_service.ErrBranchIsDefault): case errors.Is(err, repo_service.ErrBranchIsDefault):
ctx.APIError(http.StatusForbidden, "can not delete default or pull request target branch") ctx.APIError(http.StatusForbidden, "can not delete default or pull request target branch")
case errors.Is(err, git_model.ErrBranchIsProtected): case errors.Is(err, git_model.ErrBranchIsProtected):
@@ -446,7 +446,7 @@ func UpdateBranch(ctx *context.APIContext) {
if err := repo_service.UpdateBranch(ctx, repo, ctx.Repo.GitRepo, ctx.Doer, branchName, opt.NewCommitID, opt.OldCommitID, opt.Force); err != nil { if err := repo_service.UpdateBranch(ctx, repo, ctx.Repo.GitRepo, ctx.Doer, branchName, opt.NewCommitID, opt.OldCommitID, opt.Force); err != nil {
switch { switch {
case git_model.IsErrBranchNotExist(err): case git_model.IsErrBranchNotExist(err):
ctx.APIErrorNotFound(err) ctx.APIErrorNotFound()
case errors.Is(err, util.ErrInvalidArgument): case errors.Is(err, util.ErrInvalidArgument):
ctx.APIError(http.StatusUnprocessableEntity, err.Error()) ctx.APIError(http.StatusUnprocessableEntity, err.Error())
case git.IsErrPushRejected(err): case git.IsErrPushRejected(err):
+1 -1
View File
@@ -258,7 +258,7 @@ func GetAllCommits(ctx *context.APIContext) {
ctx.APIErrorInternal(err) ctx.APIErrorInternal(err)
return return
} else if commitsCountTotal == 0 { } else if commitsCountTotal == 0 {
ctx.APIErrorNotFound("FileCommitsCount", nil) ctx.APIErrorNotFound()
return return
} }
+8 -17
View File
@@ -213,7 +213,7 @@ func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, entry *git.TreeEn
} }
if entry.IsDir() || entry.IsSubModule() { if entry.IsDir() || entry.IsSubModule() {
ctx.APIErrorNotFound("getBlobForEntry", nil) ctx.APIErrorNotFound()
return nil, nil, nil return nil, nil, nil
} }
@@ -301,18 +301,14 @@ func GetEditorconfig(ctx *context.APIContext) {
ec, _, err := ctx.Repo.GetEditorconfig(ctx.Repo.Commit) ec, _, err := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
if err != nil { if err != nil {
if git.IsErrNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
fileName := ctx.PathParam("filename") fileName := ctx.PathParam("filename")
def, err := ec.GetDefinitionForFilename(fileName) def, err := ec.GetDefinitionForFilename(fileName)
if def == nil { if err != nil {
ctx.APIErrorNotFound(err) ctx.APIErrorNotFound(err.Error())
return return
} }
ctx.JSON(http.StatusOK, def) ctx.JSON(http.StatusOK, def)
@@ -699,10 +695,8 @@ func DeleteFile(ctx *context.APIContext) {
func resolveRefCommit(ctx *context.APIContext, ref string, minCommitIDLen ...int) *utils.RefCommit { func resolveRefCommit(ctx *context.APIContext, ref string, minCommitIDLen ...int) *utils.RefCommit {
ref = util.IfZero(ref, ctx.Repo.Repository.DefaultBranch) ref = util.IfZero(ref, ctx.Repo.Repository.DefaultBranch)
refCommit, err := utils.ResolveRefCommit(ctx, ctx.Repo.Repository, ref, minCommitIDLen...) refCommit, err := utils.ResolveRefCommit(ctx, ctx.Repo.Repository, ref, minCommitIDLen...)
if errors.Is(err, util.ErrNotExist) { if err != nil {
ctx.APIErrorNotFound(err) ctx.APIErrorAuto(err)
} else if err != nil {
ctx.APIErrorInternal(err)
} }
return refCommit return refCommit
} }
@@ -828,11 +822,8 @@ func getRepoContents(ctx *context.APIContext, opts files_service.GetContentsOrLi
} }
ret, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, refCommit, opts) ret, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, refCommit, opts)
if err != nil { if err != nil {
if git.IsErrNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("GetContentsOrList", err) return nil
return nil
}
ctx.APIErrorInternal(err)
} }
return &ret return &ret
} }
+2 -12
View File
@@ -540,16 +540,10 @@ func getUserIDForFilter(ctx *context.APIContext, queryName string) int64 {
} }
user, err := user_model.GetUserByName(ctx, userName) user, err := user_model.GetUserByName(ctx, userName)
if user_model.IsErrUserNotExist(err) {
ctx.APIErrorNotFound(err)
return 0
}
if err != nil { if err != nil {
ctx.APIErrorInternal(err) ctx.APIErrorAuto(err)
return 0 return 0
} }
return user.ID return user.ID
} }
@@ -969,11 +963,7 @@ func DeleteIssue(ctx *context.APIContext) {
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
+3 -15
View File
@@ -447,11 +447,7 @@ func GetIssueComment(ctx *context.APIContext) {
comment, err := issues_model.GetCommentWithRepoID(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("id")) comment, err := issues_model.GetCommentWithRepoID(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("id"))
if err != nil { if err != nil {
if issues_model.IsErrCommentNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
@@ -572,11 +568,7 @@ func EditIssueCommentDeprecated(ctx *context.APIContext) {
func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) { func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) {
comment, err := issues_model.GetCommentWithRepoID(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("id")) comment, err := issues_model.GetCommentWithRepoID(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("id"))
if err != nil { if err != nil {
if issues_model.IsErrCommentNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
@@ -681,11 +673,7 @@ func DeleteIssueCommentDeprecated(ctx *context.APIContext) {
func deleteIssueComment(ctx *context.APIContext) { func deleteIssueComment(ctx *context.APIContext) {
comment, err := issues_model.GetCommentWithRepoID(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("id")) comment, err := issues_model.GetCommentWithRepoID(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("id"))
if err != nil { if err != nil {
if issues_model.IsErrCommentNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
+4 -20
View File
@@ -63,11 +63,7 @@ func GetIssueDependencies(ctx *context.APIContext) {
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("IsErrIssueNotExist", err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
@@ -487,11 +483,7 @@ func RemoveIssueBlocking(ctx *context.APIContext) {
func getParamsIssue(ctx *context.APIContext) *issues_model.Issue { func getParamsIssue(ctx *context.APIContext) *issues_model.Issue {
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("IsErrIssueNotExist", err)
} else {
ctx.APIErrorInternal(err)
}
return nil return nil
} }
issue.Repo = ctx.Repo.Repository issue.Repo = ctx.Repo.Repository
@@ -508,11 +500,7 @@ func getFormIssue(ctx *context.APIContext, form *api.IssueMeta) *issues_model.Is
var err error var err error
repo, err = repo_model.GetRepositoryByOwnerAndName(ctx, form.Owner, form.Name) repo, err = repo_model.GetRepositoryByOwnerAndName(ctx, form.Owner, form.Name)
if err != nil { if err != nil {
if repo_model.IsErrRepoNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("IsErrRepoNotExist", err)
} else {
ctx.APIErrorInternal(err)
}
return nil return nil
} }
} else { } else {
@@ -521,11 +509,7 @@ func getFormIssue(ctx *context.APIContext, form *api.IssueMeta) *issues_model.Is
issue, err := issues_model.GetIssueByIndex(ctx, repo.ID, form.Index) issue, err := issues_model.GetIssueByIndex(ctx, repo.ID, form.Index)
if err != nil { if err != nil {
if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("IsErrIssueNotExist", err)
} else {
ctx.APIErrorInternal(err)
}
return nil return nil
} }
issue.Repo = repo issue.Repo = repo
+2 -10
View File
@@ -53,11 +53,7 @@ func LockIssue(ctx *context.APIContext) {
reason := web.GetForm(ctx).(*api.LockIssueOption).Reason reason := web.GetForm(ctx).(*api.LockIssueOption).Reason
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
@@ -120,11 +116,7 @@ func UnlockIssue(ctx *context.APIContext) {
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
+2 -10
View File
@@ -53,11 +53,7 @@ func GetIssueCommentReactions(ctx *context.APIContext) {
comment, err := issues_model.GetCommentByID(ctx, ctx.PathParamInt64("id")) comment, err := issues_model.GetCommentByID(ctx, ctx.PathParamInt64("id"))
if err != nil { if err != nil {
if issues_model.IsErrCommentNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
@@ -190,11 +186,7 @@ func DeleteIssueCommentReaction(ctx *context.APIContext) {
func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOption, isCreateType bool) { func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOption, isCreateType bool) {
comment, err := issues_model.GetCommentByID(ctx, ctx.PathParamInt64("id")) comment, err := issues_model.GetCommentByID(ctx, ctx.PathParamInt64("id"))
if err != nil { if err != nil {
if issues_model.IsErrCommentNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
+7 -31
View File
@@ -71,16 +71,12 @@ func ListTrackedTimes(ctx *context.APIContext) {
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
if !ctx.Repo.Repository.IsTimetrackerEnabled(ctx) { if !ctx.Repo.Repository.IsTimetrackerEnabled(ctx) {
ctx.APIErrorNotFound("Timetracker is disabled") ctx.APIErrorNotFound("timetracker is disabled")
return return
} }
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
@@ -182,11 +178,7 @@ func AddTime(ctx *context.APIContext) {
form := web.GetForm(ctx).(*api.AddTimeOption) form := web.GetForm(ctx).(*api.AddTimeOption)
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
@@ -265,11 +257,7 @@ func ResetIssueTime(ctx *context.APIContext) {
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
@@ -338,11 +326,7 @@ func DeleteTime(ctx *context.APIContext) {
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
@@ -357,11 +341,7 @@ func DeleteTime(ctx *context.APIContext) {
time, err := issues_model.GetTrackedTimeByID(ctx, issue.ID, ctx.PathParamInt64("id")) time, err := issues_model.GetTrackedTimeByID(ctx, issue.ID, ctx.PathParamInt64("id"))
if err != nil { if err != nil {
if db.IsErrNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
return
}
ctx.APIErrorInternal(err)
return return
} }
if time.Deleted { if time.Deleted {
@@ -423,11 +403,7 @@ func ListTrackedTimesByUser(ctx *context.APIContext) {
} }
user, err := user_model.GetUserByName(ctx, ctx.PathParam("timetrackingusername")) user, err := user_model.GetUserByName(ctx, ctx.PathParam("timetrackingusername"))
if err != nil { if err != nil {
if user_model.IsErrUserNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
if user == nil { if user == nil {
+1 -5
View File
@@ -68,11 +68,7 @@ func getNote(ctx *context.APIContext, identifier string) {
commitID, err := ctx.Repo.GitRepo.ConvertToGitID(identifier) commitID, err := ctx.Repo.GitRepo.ConvertToGitID(identifier)
if err != nil { if err != nil {
if git.IsErrNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
+1 -5
View File
@@ -927,11 +927,7 @@ func MergePullRequest(ctx *context.APIContext) {
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("GetPullRequestByIndex", err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
+10 -34
View File
@@ -63,11 +63,7 @@ func ListPullReviews(ctx *context.APIContext) {
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("GetPullRequestByIndex", err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
@@ -389,11 +385,7 @@ func updatePullReviewCommentResolve(ctx *context.APIContext, isResolve bool) {
func getPullReviewCommentToResolve(ctx *context.APIContext) *issues_model.Comment { func getPullReviewCommentToResolve(ctx *context.APIContext) *issues_model.Comment {
comment, err := issues_model.GetCommentWithRepoID(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("id")) comment, err := issues_model.GetCommentWithRepoID(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("id"))
if err != nil { if err != nil {
if issues_model.IsErrCommentNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("GetCommentByID", err)
} else {
ctx.APIErrorInternal(err)
}
return nil return nil
} }
@@ -510,11 +502,7 @@ func CreatePullReview(ctx *context.APIContext) {
opts := web.GetForm(ctx).(*api.CreatePullReviewOptions) opts := web.GetForm(ctx).(*api.CreatePullReviewOptions)
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("GetPullRequestByIndex", err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
@@ -737,33 +725,25 @@ func preparePullReviewType(ctx *context.APIContext, pr *issues_model.PullRequest
func prepareSingleReview(ctx *context.APIContext) (*issues_model.Review, *issues_model.PullRequest, bool) { func prepareSingleReview(ctx *context.APIContext) (*issues_model.Review, *issues_model.PullRequest, bool) {
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("GetPullRequestByIndex", err)
} else {
ctx.APIErrorInternal(err)
}
return nil, nil, true return nil, nil, true
} }
review, err := issues_model.GetReviewByID(ctx, ctx.PathParamInt64("id")) review, err := issues_model.GetReviewByID(ctx, ctx.PathParamInt64("id"))
if err != nil { if err != nil {
if issues_model.IsErrReviewNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("GetReviewByID", err)
} else {
ctx.APIErrorInternal(err)
}
return nil, nil, true return nil, nil, true
} }
// validate the review is for the given PR // validate the review is for the given PR
if review.IssueID != pr.IssueID { if review.IssueID != pr.IssueID {
ctx.APIErrorNotFound("ReviewNotInPR") ctx.APIErrorNotFound()
return nil, nil, true return nil, nil, true
} }
// make sure that the user has access to this review if it is pending // make sure that the user has access to this review if it is pending
if review.Type == issues_model.ReviewTypePending && review.ReviewerID != ctx.Doer.ID && !ctx.Doer.IsAdmin { if review.Type == issues_model.ReviewTypePending && review.ReviewerID != ctx.Doer.ID && !ctx.Doer.IsAdmin {
ctx.APIErrorNotFound("GetReviewByID") ctx.APIErrorNotFound()
return nil, nil, true return nil, nil, true
} }
@@ -870,7 +850,7 @@ func parseReviewersByNames(ctx *context.APIContext, reviewerNames, teamReviewerN
if err != nil { if err != nil {
if user_model.IsErrUserNotExist(err) { if user_model.IsErrUserNotExist(err) {
ctx.APIErrorNotFound("UserNotExist", fmt.Sprintf("User '%s' not exist", r)) ctx.APIErrorNotFound("user doesn't exist: " + r)
return nil, nil return nil, nil
} }
ctx.APIErrorInternal(err) ctx.APIErrorInternal(err)
@@ -886,7 +866,7 @@ func parseReviewersByNames(ctx *context.APIContext, reviewerNames, teamReviewerN
teamReviewer, err = organization.GetTeam(ctx, ctx.Repo.Owner.ID, t) teamReviewer, err = organization.GetTeam(ctx, ctx.Repo.Owner.ID, t)
if err != nil { if err != nil {
if organization.IsErrTeamNotExist(err) { if organization.IsErrTeamNotExist(err) {
ctx.APIErrorNotFound("TeamNotExist", fmt.Sprintf("Team '%s' not exist", t)) ctx.APIErrorNotFound("team doesn't exist: " + t)
return nil, nil return nil, nil
} }
ctx.APIErrorInternal(err) ctx.APIErrorInternal(err)
@@ -902,11 +882,7 @@ func parseReviewersByNames(ctx *context.APIContext, reviewerNames, teamReviewerN
func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions, isAdd bool) { func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions, isAdd bool) {
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil { if err != nil {
if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("GetPullRequestByIndex", err)
} else {
ctx.APIErrorInternal(err)
}
return return
} }
+1 -1
View File
@@ -205,7 +205,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
// Check if attachments are enabled // Check if attachments are enabled
if !setting.Attachment.Enabled { if !setting.Attachment.Enabled {
ctx.APIErrorNotFound("Attachment is not enabled") ctx.APIErrorNotFound("attachment is not enabled")
return return
} }
+3 -15
View File
@@ -245,11 +245,7 @@ func DeleteWikiPage(ctx *context.APIContext) {
wikiName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("pageName")) wikiName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("pageName"))
if err := wiki_service.DeleteWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, wikiName); err != nil { if err := wiki_service.DeleteWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, wikiName); err != nil {
if err.Error() == "file does not exist" { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
return
}
ctx.APIErrorInternal(err)
return return
} }
@@ -474,21 +470,13 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) { func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) {
wikiRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo()) wikiRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
if err != nil { if err != nil {
if git.IsErrNotExist(err) || err.Error() == "no such file or directory" { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return nil, nil return nil, nil
} }
commit, err := wikiRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch) commit, err := wikiRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch)
if err != nil { if err != nil {
if git.IsErrNotExist(err) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound(err)
} else {
ctx.APIErrorInternal(err)
}
return wikiRepo, nil return wikiRepo, nil
} }
return wikiRepo, commit return wikiRepo, commit
+3 -3
View File
@@ -45,7 +45,7 @@ func ListBlocks(ctx *context.APIContext, blocker *user_model.User) {
func CheckUserBlock(ctx *context.APIContext, blocker *user_model.User) { func CheckUserBlock(ctx *context.APIContext, blocker *user_model.User) {
blockee, err := user_model.GetUserByName(ctx, ctx.PathParam("username")) blockee, err := user_model.GetUserByName(ctx, ctx.PathParam("username"))
if err != nil { if err != nil {
ctx.APIErrorNotFound("GetUserByName", err) ctx.APIErrorAuto(err)
return return
} }
@@ -62,7 +62,7 @@ func CheckUserBlock(ctx *context.APIContext, blocker *user_model.User) {
func BlockUser(ctx *context.APIContext, blocker *user_model.User) { func BlockUser(ctx *context.APIContext, blocker *user_model.User) {
blockee, err := user_model.GetUserByName(ctx, ctx.PathParam("username")) blockee, err := user_model.GetUserByName(ctx, ctx.PathParam("username"))
if err != nil { if err != nil {
ctx.APIErrorNotFound("GetUserByName", err) ctx.APIErrorAuto(err)
return return
} }
@@ -81,7 +81,7 @@ func BlockUser(ctx *context.APIContext, blocker *user_model.User) {
func UnblockUser(ctx *context.APIContext, doer, blocker *user_model.User) { func UnblockUser(ctx *context.APIContext, doer, blocker *user_model.User) {
blockee, err := user_model.GetUserByName(ctx, ctx.PathParam("username")) blockee, err := user_model.GetUserByName(ctx, ctx.PathParam("username"))
if err != nil { if err != nil {
ctx.APIErrorNotFound("GetUserByName", err) ctx.APIErrorAuto(err)
return return
} }
+1 -5
View File
@@ -77,11 +77,7 @@ func getRunnerByID(ctx *context.APIContext, ownerID, repoID, runnerID int64) (*a
runner, err := actions_model.GetRunnerByID(ctx, runnerID) runner, err := actions_model.GetRunnerByID(ctx, runnerID)
if err != nil { if err != nil {
if errors.Is(err, util.ErrNotExist) { ctx.APIErrorAuto(err)
ctx.APIErrorNotFound("Runner not found")
} else {
ctx.APIErrorInternal(err)
}
return nil, false return nil, false
} }
+2 -3
View File
@@ -4,7 +4,6 @@
package user package user
import ( import (
"errors"
"net/http" "net/http"
"strings" "strings"
@@ -135,7 +134,7 @@ func GetGPGKey(ctx *context.APIContext) {
// CreateUserGPGKey creates new GPG key to given user by ID. // CreateUserGPGKey creates new GPG key to given user by ID.
func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) { func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) {
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageGPGKeys) { if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageGPGKeys) {
ctx.APIErrorNotFound("Not Found", errors.New("gpg keys setting is not allowed to be visited")) ctx.APIErrorNotFound("gpg keys setting is not allowed to be changed")
return return
} }
@@ -276,7 +275,7 @@ func DeleteGPGKey(ctx *context.APIContext) {
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageGPGKeys) { if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageGPGKeys) {
ctx.APIErrorNotFound("Not Found", errors.New("gpg keys setting is not allowed to be visited")) ctx.APIErrorNotFound("gpg keys setting is not allowed to be changed")
return return
} }
+1 -1
View File
@@ -18,7 +18,7 @@ func GetUserByPathParam(ctx *context.APIContext, name string) *user_model.User {
if redirectUserID, err2 := user_model.LookupUserRedirect(ctx, username); err2 == nil { if redirectUserID, err2 := user_model.LookupUserRedirect(ctx, username); err2 == nil {
context.RedirectToUser(ctx.Base, ctx.Doer, username, redirectUserID) context.RedirectToUser(ctx.Base, ctx.Doer, username, redirectUserID)
} else { } else {
ctx.APIErrorNotFound("GetUserByName", err) ctx.APIErrorNotFound()
} }
} else { } else {
ctx.APIErrorInternal(err) ctx.APIErrorInternal(err)
+2 -3
View File
@@ -6,7 +6,6 @@ package user
import ( import (
std_ctx "context" std_ctx "context"
"errors"
"net/http" "net/http"
asymkey_model "gitea.dev/models/asymkey" asymkey_model "gitea.dev/models/asymkey"
@@ -201,7 +200,7 @@ func GetPublicKey(ctx *context.APIContext) {
// CreateUserPublicKey creates new public key to given user by ID. // CreateUserPublicKey creates new public key to given user by ID.
func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) { func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) {
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageSSHKeys) { if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageSSHKeys) {
ctx.APIErrorNotFound("Not Found", errors.New("ssh keys setting is not allowed to be visited")) ctx.APIErrorNotFound("ssh keys setting is not allowed to be changed")
return return
} }
@@ -271,7 +270,7 @@ func DeletePublicKey(ctx *context.APIContext) {
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageSSHKeys) { if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageSSHKeys) {
ctx.APIErrorNotFound("Not Found", errors.New("ssh keys setting is not allowed to be visited")) ctx.APIErrorNotFound("ssh keys setting is not allowed to be changed")
return return
} }
+1 -1
View File
@@ -117,7 +117,7 @@ func GetInfo(ctx *context.APIContext) {
if !user_model.IsUserVisibleToViewer(ctx, ctx.ContextUser, ctx.Doer) { if !user_model.IsUserVisibleToViewer(ctx, ctx.ContextUser, ctx.Doer) {
// fake ErrUserNotExist error message to not leak information about existence // fake ErrUserNotExist error message to not leak information about existence
ctx.APIErrorNotFound("GetUserByName", user_model.ErrUserNotExist{Name: ctx.PathParam("username")}) ctx.APIErrorNotFound()
return return
} }
ctx.JSON(http.StatusOK, convert.ToUser(ctx, ctx.ContextUser, ctx.Doer)) ctx.JSON(http.StatusOK, convert.ToUser(ctx, ctx.ContextUser, ctx.Doer))
+4 -20
View File
@@ -138,26 +138,10 @@ func (ctx *APIContext) apiErrorInternal(skip int, err error) {
} }
// APIErrorNotFound handles 404s for APIContext // APIErrorNotFound handles 404s for APIContext
// String will replace message, errors will be added to a slice func (ctx *APIContext) APIErrorNotFound(msg ...string) {
func (ctx *APIContext) APIErrorNotFound(objs ...any) { ctx.JSON(http.StatusNotFound, APIError{
var message string Message: util.OptionalArg(msg, "not found"),
var errs []string URL: setting.API.SwaggerURL,
for _, obj := range objs {
// Ignore nil
if obj == nil {
continue
}
if err, ok := obj.(error); ok {
errs = append(errs, err.Error())
} else {
message = obj.(string)
}
}
ctx.JSON(http.StatusNotFound, map[string]any{
"message": util.IfZero(message, "not found"), // do not use locale in API
"url": setting.API.SwaggerURL,
"errors": errs,
}) })
} }
+2 -2
View File
@@ -7,7 +7,6 @@ package wiki
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"gitea.dev/models/db" "gitea.dev/models/db"
repo_model "gitea.dev/models/repo" repo_model "gitea.dev/models/repo"
@@ -21,6 +20,7 @@ import (
"gitea.dev/modules/graceful" "gitea.dev/modules/graceful"
"gitea.dev/modules/log" "gitea.dev/modules/log"
repo_module "gitea.dev/modules/repository" repo_module "gitea.dev/modules/repository"
"gitea.dev/modules/util"
asymkey_service "gitea.dev/services/asymkey" asymkey_service "gitea.dev/services/asymkey"
repo_service "gitea.dev/services/repository" repo_service "gitea.dev/services/repository"
) )
@@ -304,7 +304,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
return err return err
} }
} else { } else {
return os.ErrNotExist return util.ErrNotExist
} }
// FIXME: The wiki doesn't have lfs support at present - if this changes need to check attributes here // FIXME: The wiki doesn't have lfs support at present - if this changes need to check attributes here
+1 -3
View File
@@ -151,9 +151,7 @@ func testUnknownOrganization(t *testing.T) {
req := NewRequest(t, "GET", "/api/v1/users/user1/orgs/unknown/permissions"). req := NewRequest(t, "GET", "/api/v1/users/user1/orgs/unknown/permissions").
AddTokenAuth(token) AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusNotFound) MakeRequest(t, req, http.StatusNotFound)
apiError := DecodeJSON(t, resp, &api.APIError{})
assert.Equal(t, "GetUserByName", apiError.Message)
} }
func testHiddenMemberPermissionsForbidden(t *testing.T) { func testHiddenMemberPermissionsForbidden(t *testing.T) {