From 15e020eec85d7ec2ef0f1e9ad10a1ffde2ca0f34 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 18 Feb 2025 04:41:03 +0800 Subject: [PATCH] Refactor error system (#33626) --- modules/util/error.go | 15 ++- routers/api/v1/admin/email.go | 2 +- routers/api/v1/admin/hooks.go | 10 +- routers/api/v1/admin/org.go | 4 +- routers/api/v1/admin/user.go | 16 +-- routers/api/v1/admin/user_badge.go | 6 +- routers/api/v1/api.go | 42 ++++--- routers/api/v1/misc/signing.go | 5 +- routers/api/v1/org/action.go | 18 +-- routers/api/v1/org/avatar.go | 4 +- routers/api/v1/org/label.go | 12 +- routers/api/v1/org/member.go | 14 +-- routers/api/v1/org/org.go | 20 +-- routers/api/v1/org/team.go | 44 +++---- routers/api/v1/packages/package.go | 10 +- routers/api/v1/repo/action.go | 34 ++--- routers/api/v1/repo/avatar.go | 4 +- routers/api/v1/repo/branch.go | 118 +++++++++--------- routers/api/v1/repo/collaborators.go | 24 ++-- routers/api/v1/repo/commits.go | 28 ++--- routers/api/v1/repo/compare.go | 2 +- routers/api/v1/repo/download.go | 2 +- routers/api/v1/repo/file.go | 18 +-- routers/api/v1/repo/fork.go | 14 +-- routers/api/v1/repo/git_hook.go | 12 +- routers/api/v1/repo/git_ref.go | 2 +- routers/api/v1/repo/hook.go | 4 +- routers/api/v1/repo/issue.go | 68 +++++----- routers/api/v1/repo/issue_attachment.go | 16 +-- routers/api/v1/repo/issue_comment.go | 44 +++---- .../api/v1/repo/issue_comment_attachment.go | 18 +-- routers/api/v1/repo/issue_dependency.go | 18 +-- routers/api/v1/repo/issue_label.go | 30 ++--- routers/api/v1/repo/issue_pin.go | 32 ++--- routers/api/v1/repo/issue_reaction.go | 28 ++--- routers/api/v1/repo/issue_stopwatch.go | 12 +- routers/api/v1/repo/issue_subscription.go | 18 +-- routers/api/v1/repo/issue_tracked_time.go | 42 +++---- routers/api/v1/repo/key.go | 10 +- routers/api/v1/repo/label.go | 12 +- routers/api/v1/repo/migrate.go | 12 +- routers/api/v1/repo/milestone.go | 12 +- routers/api/v1/repo/mirror.go | 6 +- routers/api/v1/repo/notes.go | 6 +- routers/api/v1/repo/patch.go | 4 +- routers/api/v1/repo/pull.go | 110 ++++++++-------- routers/api/v1/repo/pull_review.go | 62 ++++----- routers/api/v1/repo/release.go | 30 ++--- routers/api/v1/repo/release_attachment.go | 20 +-- routers/api/v1/repo/release_tags.go | 8 +- routers/api/v1/repo/repo.go | 42 +++---- routers/api/v1/repo/star.go | 2 +- routers/api/v1/repo/status.go | 6 +- routers/api/v1/repo/subscriber.go | 2 +- routers/api/v1/repo/tag.go | 38 +++--- routers/api/v1/repo/transfer.go | 4 +- routers/api/v1/repo/wiki.go | 20 +-- routers/api/v1/shared/block.go | 10 +- routers/api/v1/user/action.go | 18 +-- routers/api/v1/user/app.go | 16 +-- routers/api/v1/user/avatar.go | 4 +- routers/api/v1/user/email.go | 8 +- routers/api/v1/user/follower.go | 8 +- routers/api/v1/user/gpg_key.go | 16 +-- routers/api/v1/user/helper.go | 4 +- routers/api/v1/user/key.go | 8 +- routers/api/v1/user/repo.go | 12 +- routers/api/v1/user/star.go | 8 +- routers/api/v1/user/user.go | 4 +- routers/api/v1/user/watch.go | 8 +- routers/api/v1/utils/git.go | 2 +- routers/api/v1/utils/hook.go | 34 ++--- services/actions/workflow.go | 2 +- services/context/api.go | 16 ++- services/context/user.go | 2 +- 75 files changed, 703 insertions(+), 693 deletions(-) diff --git a/modules/util/error.go b/modules/util/error.go index 07fadf3cab..13b592a9f8 100644 --- a/modules/util/error.go +++ b/modules/util/error.go @@ -10,13 +10,16 @@ import ( // Common Errors forming the base of our error system // -// Many Errors returned by Gitea can be tested against these errors -// using errors.Is. +// Many Errors returned by Gitea can be tested against these errors using "errors.Is". var ( - ErrInvalidArgument = errors.New("invalid argument") - ErrPermissionDenied = errors.New("permission denied") - ErrAlreadyExist = errors.New("resource already exists") - ErrNotExist = errors.New("resource does not exist") + ErrInvalidArgument = errors.New("invalid argument") // also implies HTTP 400 + ErrPermissionDenied = errors.New("permission denied") // also implies HTTP 403 + ErrNotExist = errors.New("resource does not exist") // also implies HTTP 404 + ErrAlreadyExist = errors.New("resource already exists") // also implies HTTP 409 + + // ErrUnprocessableContent implies HTTP 422, syntax of the request content was correct, + // but server was unable to process the contained instructions + ErrUnprocessableContent = errors.New("unprocessable content") ) // SilentWrap provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message diff --git a/routers/api/v1/admin/email.go b/routers/api/v1/admin/email.go index dc8d27be0f..ad078347a4 100644 --- a/routers/api/v1/admin/email.go +++ b/routers/api/v1/admin/email.go @@ -42,7 +42,7 @@ func GetAllEmails(ctx *context.APIContext) { ListOptions: listOptions, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/admin/hooks.go b/routers/api/v1/admin/hooks.go index e914baf278..fb1ea4eab6 100644 --- a/routers/api/v1/admin/hooks.go +++ b/routers/api/v1/admin/hooks.go @@ -59,14 +59,14 @@ func ListHooks(ctx *context.APIContext) { sysHooks, err := webhook.GetSystemOrDefaultWebhooks(ctx, isSystemWebhook) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } hooks := make([]*api.Hook, len(sysHooks)) for i, hook := range sysHooks { h, err := webhook_service.ToHook(setting.AppURL+"/-/admin", hook) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } hooks[i] = h @@ -98,13 +98,13 @@ func GetHook(ctx *context.APIContext) { if errors.Is(err, util.ErrNotExist) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } h, err := webhook_service.ToHook("/-/admin/", hook) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, h) @@ -188,7 +188,7 @@ func DeleteHook(ctx *context.APIContext) { if errors.Is(err, util.ErrNotExist) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/routers/api/v1/admin/org.go b/routers/api/v1/admin/org.go index c5962afbc1..8808a1587d 100644 --- a/routers/api/v1/admin/org.go +++ b/routers/api/v1/admin/org.go @@ -69,7 +69,7 @@ func CreateOrg(ctx *context.APIContext) { db.IsErrNamePatternNotAllowed(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -109,7 +109,7 @@ func GetAllOrgs(ctx *context.APIContext) { Visible: []api.VisibleType{api.VisibleTypePublic, api.VisibleTypeLimited, api.VisibleTypePrivate}, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } orgs := make([]*api.Organization, len(users)) diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index c653794ffc..c4bb85de55 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -42,7 +42,7 @@ func parseAuthSource(ctx *context.APIContext, u *user_model.User, sourceID int64 if auth.IsErrSourceNotExist(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -145,7 +145,7 @@ func CreateUser(ctx *context.APIContext) { db.IsErrNamePatternNotAllowed(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -210,7 +210,7 @@ func EditUser(ctx *context.APIContext) { case errors.Is(err, password.ErrIsPwned), password.IsErrIsPwnedRequest(err): ctx.APIError(http.StatusBadRequest, err) default: - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -223,7 +223,7 @@ func EditUser(ctx *context.APIContext) { case user_model.IsErrEmailAlreadyUsed(err): ctx.APIError(http.StatusBadRequest, err) default: - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -252,7 +252,7 @@ func EditUser(ctx *context.APIContext) { if user_model.IsErrDeleteLastAdminUser(err) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -307,7 +307,7 @@ func DeleteUser(ctx *context.APIContext) { user_model.IsErrDeleteLastAdminUser(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -381,7 +381,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) { } else if asymkey_model.IsErrKeyAccessDenied(err) { ctx.APIError(http.StatusForbidden, "You do not have access to this key") } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -432,7 +432,7 @@ func SearchUsers(ctx *context.APIContext) { ListOptions: listOptions, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/admin/user_badge.go b/routers/api/v1/admin/user_badge.go index 80c246b43d..6d9665a72b 100644 --- a/routers/api/v1/admin/user_badge.go +++ b/routers/api/v1/admin/user_badge.go @@ -33,7 +33,7 @@ func ListUserBadges(ctx *context.APIContext) { badges, maxResults, err := user_model.GetUserBadges(ctx, ctx.ContextUser) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -70,7 +70,7 @@ func AddUserBadges(ctx *context.APIContext) { badges := prepareBadgesForReplaceOrAdd(*form) if err := user_model.AddUserBadges(ctx, ctx.ContextUser, badges); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -106,7 +106,7 @@ func DeleteUserBadges(ctx *context.APIContext) { badges := prepareBadgesForReplaceOrAdd(*form) if err := user_model.RemoveUserBadges(ctx, ctx.ContextUser, badges); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index ad9c71dfbe..907a2f08fe 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -66,6 +66,7 @@ package v1 import ( + "errors" "fmt" "net/http" "strings" @@ -118,7 +119,7 @@ func sudo() func(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -156,10 +157,10 @@ func repoAssignment() func(ctx *context.APIContext) { } else if user_model.IsErrUserRedirectNotExist(err) { ctx.APIErrorNotFound("GetUserByName", err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -177,10 +178,10 @@ func repoAssignment() func(ctx *context.APIContext) { } else if repo_model.IsErrRedirectNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -192,7 +193,7 @@ func repoAssignment() func(ctx *context.APIContext) { taskID := ctx.Data["ActionsTaskID"].(int64) task, err := actions_model.GetTaskByID(ctx, taskID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if task.RepoID != repo.ID { @@ -207,14 +208,14 @@ func repoAssignment() func(ctx *context.APIContext) { } if err := ctx.Repo.Repository.LoadUnits(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Repo.Permission.SetUnitsWithDefaultAccessMode(ctx.Repo.Repository.Units, ctx.Repo.Permission.AccessMode) } else { ctx.Repo.Permission, err = access_model.GetUserRepoPermission(ctx, repo, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -474,13 +475,14 @@ func reqOrgOwnership() func(ctx *context.APIContext) { } else if ctx.Org.Team != nil { orgID = ctx.Org.Team.OrgID } else { - ctx.APIError(http.StatusInternalServerError, "reqOrgOwnership: unprepared context") + setting.PanicInDevOrTesting("reqOrgOwnership: unprepared context") + ctx.APIErrorInternal(errors.New("reqOrgOwnership: unprepared context")) return } isOwner, err := organization.IsOrganizationOwner(ctx, orgID, ctx.Doer.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if !isOwner { if ctx.Org.Organization != nil { @@ -500,26 +502,27 @@ func reqTeamMembership() func(ctx *context.APIContext) { return } if ctx.Org.Team == nil { - ctx.APIError(http.StatusInternalServerError, "reqTeamMembership: unprepared context") + setting.PanicInDevOrTesting("reqTeamMembership: unprepared context") + ctx.APIErrorInternal(errors.New("reqTeamMembership: unprepared context")) return } orgID := ctx.Org.Team.OrgID isOwner, err := organization.IsOrganizationOwner(ctx, orgID, ctx.Doer.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if isOwner { return } if isTeamMember, err := organization.IsTeamMember(ctx, orgID, ctx.Org.Team.ID, ctx.Doer.ID); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if !isTeamMember { isOrgMember, err := organization.IsOrganizationMember(ctx, orgID, ctx.Doer.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } else if isOrgMember { ctx.APIError(http.StatusForbidden, "Must be a team member") } else { @@ -543,12 +546,13 @@ func reqOrgMembership() func(ctx *context.APIContext) { } else if ctx.Org.Team != nil { orgID = ctx.Org.Team.OrgID } else { - ctx.APIError(http.StatusInternalServerError, "reqOrgMembership: unprepared context") + setting.PanicInDevOrTesting("reqOrgMembership: unprepared context") + ctx.APIErrorInternal(errors.New("reqOrgMembership: unprepared context")) return } if isMember, err := organization.IsOrganizationMember(ctx, orgID, ctx.Doer.ID); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if !isMember { if ctx.Org.Organization != nil { @@ -615,10 +619,10 @@ func orgAssignment(args ...bool) func(ctx *context.APIContext) { } else if user_model.IsErrUserRedirectNotExist(err) { ctx.APIErrorNotFound("GetOrgByName", err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -631,7 +635,7 @@ func orgAssignment(args ...bool) func(ctx *context.APIContext) { if organization.IsErrTeamNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/routers/api/v1/misc/signing.go b/routers/api/v1/misc/signing.go index 7a9c0af7e9..667396e39c 100644 --- a/routers/api/v1/misc/signing.go +++ b/routers/api/v1/misc/signing.go @@ -5,7 +5,6 @@ package misc import ( "fmt" - "net/http" asymkey_service "code.gitea.io/gitea/services/asymkey" "code.gitea.io/gitea/services/context" @@ -53,11 +52,11 @@ func SigningKey(ctx *context.APIContext) { content, err := asymkey_service.PublicSigningKey(ctx, path) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } _, err = ctx.Write([]byte(content)) if err != nil { - ctx.APIError(http.StatusInternalServerError, fmt.Errorf("Error writing key content %w", err)) + ctx.APIErrorInternal(fmt.Errorf("Error writing key content %w", err)) } } diff --git a/routers/api/v1/org/action.go b/routers/api/v1/org/action.go index 425e514f2b..d9bdb3ab48 100644 --- a/routers/api/v1/org/action.go +++ b/routers/api/v1/org/action.go @@ -113,7 +113,7 @@ func (Action) CreateOrUpdateSecret(ctx *context.APIContext) { } else if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -160,7 +160,7 @@ func (Action) DeleteSecret(ctx *context.APIContext) { } else if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -223,7 +223,7 @@ func (Action) ListVariables(ctx *context.APIContext) { ListOptions: utils.GetListOptions(ctx), }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -275,7 +275,7 @@ func (Action) GetVariable(ctx *context.APIContext) { if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -326,7 +326,7 @@ func (Action) DeleteVariable(ctx *context.APIContext) { } else if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -378,7 +378,7 @@ func (Action) CreateVariable(ctx *context.APIContext) { Name: variableName, }) if err != nil && !errors.Is(err, util.ErrNotExist) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if v != nil && v.ID > 0 { @@ -390,7 +390,7 @@ func (Action) CreateVariable(ctx *context.APIContext) { if errors.Is(err, util.ErrInvalidArgument) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -442,7 +442,7 @@ func (Action) UpdateVariable(ctx *context.APIContext) { if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -458,7 +458,7 @@ func (Action) UpdateVariable(ctx *context.APIContext) { if errors.Is(err, util.ErrInvalidArgument) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/routers/api/v1/org/avatar.go b/routers/api/v1/org/avatar.go index 7dadd0dd9e..0eb771b2cd 100644 --- a/routers/api/v1/org/avatar.go +++ b/routers/api/v1/org/avatar.go @@ -45,7 +45,7 @@ func UpdateAvatar(ctx *context.APIContext) { err = user_service.UploadAvatar(ctx, ctx.Org.Organization.AsUser(), content) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -72,7 +72,7 @@ func DeleteAvatar(ctx *context.APIContext) { // "$ref": "#/responses/notFound" err := user_service.DeleteAvatar(ctx, ctx.Org.Organization.AsUser()) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/org/label.go b/routers/api/v1/org/label.go index e5407ec82f..b5b70bdc7d 100644 --- a/routers/api/v1/org/label.go +++ b/routers/api/v1/org/label.go @@ -46,7 +46,7 @@ func ListLabels(ctx *context.APIContext) { labels, err := issues_model.GetLabelsByOrgID(ctx, ctx.Org.Organization.ID, ctx.FormString("sort"), utils.GetListOptions(ctx)) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -103,7 +103,7 @@ func CreateLabel(ctx *context.APIContext) { Description: form.Description, } if err := issues_model.NewLabel(ctx, label); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -149,7 +149,7 @@ func GetLabel(ctx *context.APIContext) { if issues_model.IsErrOrgLabelNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -195,7 +195,7 @@ func EditLabel(ctx *context.APIContext) { if issues_model.IsErrOrgLabelNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -219,7 +219,7 @@ func EditLabel(ctx *context.APIContext) { } l.SetArchived(form.IsArchived != nil && *form.IsArchived) if err := issues_model.UpdateLabel(ctx, l); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -250,7 +250,7 @@ func DeleteLabel(ctx *context.APIContext) { // "$ref": "#/responses/notFound" if err := issues_model.DeleteLabel(ctx, ctx.Org.Organization.ID, ctx.PathParamInt64("id")); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/org/member.go b/routers/api/v1/org/member.go index 8ed029895a..2663d78b73 100644 --- a/routers/api/v1/org/member.go +++ b/routers/api/v1/org/member.go @@ -82,7 +82,7 @@ func ListMembers(ctx *context.APIContext) { if ctx.Doer != nil { isMember, err = ctx.Org.Organization.IsOrgMember(ctx, ctx.Doer.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -150,12 +150,12 @@ func IsMember(ctx *context.APIContext) { if ctx.Doer != nil { userIsMember, err := ctx.Org.Organization.IsOrgMember(ctx, ctx.Doer.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if userIsMember || ctx.Doer.IsAdmin { userToCheckIsMember, err := ctx.Org.Organization.IsOrgMember(ctx, userToCheck.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } else if userToCheckIsMember { ctx.Status(http.StatusNoContent) } else { @@ -200,7 +200,7 @@ func IsPublicMember(ctx *context.APIContext) { } is, err := organization.IsPublicMembership(ctx, ctx.Org.Organization.ID, userToCheck.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if is { @@ -246,7 +246,7 @@ func PublicizeMember(ctx *context.APIContext) { } err := organization.ChangeOrgUserStatus(ctx, ctx.Org.Organization.ID, userToPublicize.ID, true) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) @@ -288,7 +288,7 @@ func ConcealMember(ctx *context.APIContext) { } err := organization.ChangeOrgUserStatus(ctx, ctx.Org.Organization.ID, userToConceal.ID, false) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) @@ -323,7 +323,7 @@ func DeleteMember(ctx *context.APIContext) { return } if err := org_service.RemoveOrgUser(ctx, ctx.Org.Organization, member); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } ctx.Status(http.StatusNoContent) } diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index 57d6aabdad..c9208f4757 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -35,7 +35,7 @@ func listUserOrgs(ctx *context.APIContext, u *user_model.User) { } orgs, maxResults, err := db.FindAndCount[organization.Organization](ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -145,7 +145,7 @@ func GetUserOrgsPermissions(ctx *context.APIContext) { org := organization.OrgFromUser(o) authorizeLevel, err := org.GetOrgUserMaxAuthorizeLevel(ctx, ctx.ContextUser.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -164,7 +164,7 @@ func GetUserOrgsPermissions(ctx *context.APIContext) { op.CanCreateRepository, err = org.CanCreateOrgRepo(ctx, ctx.ContextUser.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -209,7 +209,7 @@ func GetAll(ctx *context.APIContext) { Visible: vMode, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } orgs := make([]*api.Organization, len(publicOrgs)) @@ -273,7 +273,7 @@ func Create(ctx *context.APIContext) { db.IsErrNamePatternNotAllowed(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -383,7 +383,7 @@ func Edit(ctx *context.APIContext) { if form.Email != "" { if err := user_service.ReplacePrimaryEmailAddress(ctx, ctx.Org.Organization.AsUser(), form.Email); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -397,7 +397,7 @@ func Edit(ctx *context.APIContext) { RepoAdminChangeTeamAccess: optional.FromPtr(form.RepoAdminChangeTeamAccess), } if err := user_service.UpdateUser(ctx, ctx.Org.Organization.AsUser(), opts); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -424,7 +424,7 @@ func Delete(ctx *context.APIContext) { // "$ref": "#/responses/notFound" if err := org.DeleteOrganization(ctx, ctx.Org.Organization, false); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) @@ -469,7 +469,7 @@ func ListOrgActivityFeeds(ctx *context.APIContext) { org := organization.OrgFromUser(ctx.ContextUser) isMember, err := org.IsOrgMember(ctx, ctx.Doer.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } includePrivate = isMember @@ -488,7 +488,7 @@ func ListOrgActivityFeeds(ctx *context.APIContext) { feeds, count, err := feed_service.GetFeeds(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.SetTotalCountHeader(count) diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index 1518d1410c..f70e5dd235 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -59,13 +59,13 @@ func ListTeams(ctx *context.APIContext) { OrgID: ctx.Org.Organization.ID, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiTeams, err := convert.ToTeams(ctx, teams, false) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -98,13 +98,13 @@ func ListUserTeams(ctx *context.APIContext) { UserID: ctx.Doer.ID, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiTeams, err := convert.ToTeams(ctx, teams, true) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -233,7 +233,7 @@ func CreateTeam(ctx *context.APIContext) { } else if len(form.Units) > 0 { attachTeamUnits(team, form.Units) } else { - ctx.APIError(http.StatusInternalServerError, errors.New("units permission should not be empty")) + ctx.APIErrorInternal(errors.New("units permission should not be empty")) return } } else { @@ -244,7 +244,7 @@ func CreateTeam(ctx *context.APIContext) { if organization.IsErrTeamAlreadyExist(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -332,7 +332,7 @@ func EditTeam(ctx *context.APIContext) { } if err := org_service.UpdateTeam(ctx, team, isAuthChanged, isIncludeAllChanged); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -363,7 +363,7 @@ func DeleteTeam(ctx *context.APIContext) { // "$ref": "#/responses/notFound" if err := org_service.DeleteTeam(ctx, ctx.Org.Team); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) @@ -399,7 +399,7 @@ func GetTeamMembers(ctx *context.APIContext) { isMember, err := organization.IsOrganizationMember(ctx, ctx.Org.Team.OrgID, ctx.Doer.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if !isMember && !ctx.Doer.IsAdmin { ctx.APIErrorNotFound() @@ -411,7 +411,7 @@ func GetTeamMembers(ctx *context.APIContext) { TeamID: ctx.Org.Team.ID, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -456,7 +456,7 @@ func GetTeamMember(ctx *context.APIContext) { teamID := ctx.PathParamInt64("teamid") isTeamMember, err := organization.IsUserInTeams(ctx, u.ID, []int64{teamID}) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if !isTeamMember { ctx.APIErrorNotFound() @@ -500,7 +500,7 @@ func AddTeamMember(ctx *context.APIContext) { if errors.Is(err, user_model.ErrBlockedUser) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -538,7 +538,7 @@ func RemoveTeamMember(ctx *context.APIContext) { } if err := org_service.RemoveTeamMember(ctx, ctx.Org.Team, u); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) @@ -578,14 +578,14 @@ func GetTeamRepos(ctx *context.APIContext) { TeamID: team.ID, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } repos := make([]*api.Repository, len(teamRepos)) for i, repo := range teamRepos { permission, err := access_model.GetUserRepoPermission(ctx, repo, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } repos[i] = convert.ToRepo(ctx, repo, permission) @@ -636,7 +636,7 @@ func GetTeamRepo(ctx *context.APIContext) { permission, err := access_model.GetUserRepoPermission(ctx, repo, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -650,7 +650,7 @@ func getRepositoryByParams(ctx *context.APIContext) *repo_model.Repository { if repo_model.IsErrRepoNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil } @@ -694,14 +694,14 @@ func AddTeamRepository(ctx *context.APIContext) { return } if access, err := access_model.AccessLevel(ctx, ctx.Doer, repo); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if access < perm.AccessModeAdmin { ctx.APIError(http.StatusForbidden, "Must have admin-level access to the repository") return } if err := repo_service.TeamAddRepository(ctx, ctx.Org.Team, repo); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) @@ -746,14 +746,14 @@ func RemoveTeamRepository(ctx *context.APIContext) { return } if access, err := access_model.AccessLevel(ctx, ctx.Doer, repo); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if access < perm.AccessModeAdmin { ctx.APIError(http.StatusForbidden, "Must have admin-level access to the repository") return } if err := repo_service.RemoveRepositoryFromTeam(ctx, ctx.Org.Team, repo.ID); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) @@ -885,7 +885,7 @@ func ListTeamActivityFeeds(ctx *context.APIContext) { feeds, count, err := feed_service.GetFeeds(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.SetTotalCountHeader(count) diff --git a/routers/api/v1/packages/package.go b/routers/api/v1/packages/package.go index 3da456c3fb..f869519344 100644 --- a/routers/api/v1/packages/package.go +++ b/routers/api/v1/packages/package.go @@ -67,13 +67,13 @@ func ListPackages(ctx *context.APIContext) { Paginator: &listOptions, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } pds, err := packages.GetPackageDescriptors(ctx, pvs) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -81,7 +81,7 @@ func ListPackages(ctx *context.APIContext) { for _, pd := range pds { apiPackage, err := convert.ToPackage(ctx, pd, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiPackages = append(apiPackages, apiPackage) @@ -128,7 +128,7 @@ func GetPackage(ctx *context.APIContext) { apiPackage, err := convert.ToPackage(ctx, ctx.Package.Descriptor, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -169,7 +169,7 @@ func DeletePackage(ctx *context.APIContext) { err := packages_service.RemovePackageVersion(ctx, ctx.Doer, ctx.Package.Descriptor.Version) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) diff --git a/routers/api/v1/repo/action.go b/routers/api/v1/repo/action.go index 676f3f505a..6b4ce37fcf 100644 --- a/routers/api/v1/repo/action.go +++ b/routers/api/v1/repo/action.go @@ -143,7 +143,7 @@ func (Action) CreateOrUpdateSecret(ctx *context.APIContext) { } else if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -197,7 +197,7 @@ func (Action) DeleteSecret(ctx *context.APIContext) { } else if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -243,7 +243,7 @@ func (Action) GetVariable(ctx *context.APIContext) { if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -299,7 +299,7 @@ func (Action) DeleteVariable(ctx *context.APIContext) { } else if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -354,7 +354,7 @@ func (Action) CreateVariable(ctx *context.APIContext) { Name: variableName, }) if err != nil && !errors.Is(err, util.ErrNotExist) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if v != nil && v.ID > 0 { @@ -366,7 +366,7 @@ func (Action) CreateVariable(ctx *context.APIContext) { if errors.Is(err, util.ErrInvalidArgument) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -421,7 +421,7 @@ func (Action) UpdateVariable(ctx *context.APIContext) { if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -437,7 +437,7 @@ func (Action) UpdateVariable(ctx *context.APIContext) { if errors.Is(err, util.ErrInvalidArgument) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -484,7 +484,7 @@ func (Action) ListVariables(ctx *context.APIContext) { ListOptions: utils.GetListOptions(ctx), }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -581,7 +581,7 @@ func ListActionTasks(ctx *context.APIContext) { RepoID: ctx.Repo.Repository.ID, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -592,7 +592,7 @@ func ListActionTasks(ctx *context.APIContext) { for i := range tasks { convertedTask, err := convert.ToActionTask(ctx, tasks[i]) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } res.Entries[i] = convertedTask @@ -634,7 +634,7 @@ func ActionsListRepositoryWorkflows(ctx *context.APIContext) { workflows, err := actions_service.ListActionWorkflows(ctx) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -683,7 +683,7 @@ func ActionsGetWorkflow(ctx *context.APIContext) { if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -731,7 +731,7 @@ func ActionsDisableWorkflow(ctx *context.APIContext) { if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -810,7 +810,7 @@ func ActionsDispatchWorkflow(ctx *context.APIContext) { } else if errors.Is(err, util.ErrPermissionDenied) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -860,7 +860,7 @@ func ActionsEnableWorkflow(ctx *context.APIContext) { if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -1218,7 +1218,7 @@ func DownloadArtifactRaw(ctx *context.APIContext) { if actions.IsArtifactV4(art) { err := actions.DownloadArtifactV4(ctx.Base, art) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } return diff --git a/routers/api/v1/repo/avatar.go b/routers/api/v1/repo/avatar.go index abfaace300..593460586f 100644 --- a/routers/api/v1/repo/avatar.go +++ b/routers/api/v1/repo/avatar.go @@ -50,7 +50,7 @@ func UpdateAvatar(ctx *context.APIContext) { err = repo_service.UploadAvatar(ctx, ctx.Repo.Repository, content) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } ctx.Status(http.StatusNoContent) @@ -81,7 +81,7 @@ func DeleteAvatar(ctx *context.APIContext) { // "$ref": "#/responses/notFound" err := repo_service.DeleteAvatar(ctx, ctx.Repo.Repository) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } ctx.Status(http.StatusNoContent) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index a76ffc578f..5430c1a266 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -65,26 +65,26 @@ func GetBranch(ctx *context.APIContext) { if git.IsErrBranchNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } c, err := branch.GetCommit() if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } branchProtection, err := git_model.GetFirstMatchProtectedBranchRule(ctx, ctx.Repo.Repository.ID, branchName) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } br, err := convert.ToBranch(ctx, ctx.Repo.Repository, branch.Name, c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin()) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -141,7 +141,7 @@ func DeleteBranch(ctx *context.APIContext) { IsDeletedBranch: optional.Some(false), }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if totalNumOfBranches == 0 { // sync branches immediately because non-empty repository should have at least 1 branch @@ -161,7 +161,7 @@ func DeleteBranch(ctx *context.APIContext) { case errors.Is(err, git_model.ErrBranchIsProtected): ctx.APIError(http.StatusForbidden, fmt.Errorf("branch protected")) default: - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -223,14 +223,14 @@ func CreateBranch(ctx *context.APIContext) { if len(opt.OldRefName) > 0 { oldCommit, err = ctx.Repo.GitRepo.GetCommit(opt.OldRefName) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else if len(opt.OldBranchName) > 0 { //nolint if ctx.Repo.GitRepo.IsBranchExist(opt.OldBranchName) { //nolint oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldBranchName) //nolint if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -240,7 +240,7 @@ func CreateBranch(ctx *context.APIContext) { } else { oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -256,32 +256,32 @@ func CreateBranch(ctx *context.APIContext) { } else if git_model.IsErrBranchNameConflict(err) { ctx.APIError(http.StatusConflict, "The branch with the same name already exists.") } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } branch, err := ctx.Repo.GitRepo.GetBranch(opt.BranchName) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } commit, err := branch.GetCommit() if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } branchProtection, err := git_model.GetFirstMatchProtectedBranchRule(ctx, ctx.Repo.Repository.ID, branch.Name) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } br, err := convert.ToBranch(ctx, ctx.Repo.Repository, branch.Name, commit, branchProtection, ctx.Doer, ctx.Repo.IsAdmin()) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -325,7 +325,7 @@ func ListBranches(ctx *context.APIContext) { if !ctx.Repo.Repository.IsEmpty { if ctx.Repo.GitRepo == nil { - ctx.APIError(http.StatusInternalServerError, nil) + ctx.APIErrorInternal(nil) return } @@ -337,7 +337,7 @@ func ListBranches(ctx *context.APIContext) { var err error totalNumOfBranches, err = db.Count[git_model.Branch](ctx, branchOpts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if totalNumOfBranches == 0 { // sync branches immediately because non-empty repository should have at least 1 branch @@ -350,13 +350,13 @@ func ListBranches(ctx *context.APIContext) { rules, err := git_model.FindRepoProtectedBranchRules(ctx, ctx.Repo.Repository.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } branches, err := db.Find[git_model.Branch](ctx, branchOpts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -369,14 +369,14 @@ func ListBranches(ctx *context.APIContext) { totalNumOfBranches-- continue } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } branchProtection := rules.GetFirstMatched(branches[i].Name) apiBranch, err := convert.ToBranch(ctx, ctx.Repo.Repository, branches[i].Name, c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin()) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiBranches = append(apiBranches, apiBranch) @@ -450,7 +450,7 @@ func UpdateBranch(ctx *context.APIContext) { case errors.Is(err, git_model.ErrBranchIsProtected): ctx.APIError(http.StatusForbidden, "Branch is protected by glob-based protection rules.") default: - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -499,7 +499,7 @@ func GetBranchProtection(ctx *context.APIContext) { bpName := ctx.PathParam("name") bp, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if bp == nil || bp.RepoID != repo.ID { @@ -535,7 +535,7 @@ func ListBranchProtections(ctx *context.APIContext) { repo := ctx.Repo.Repository bps, err := git_model.FindRepoProtectedBranchRules(ctx, repo.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiBps := make([]*api.BranchProtection, len(bps)) @@ -602,7 +602,7 @@ func CreateBranchProtection(ctx *context.APIContext) { protectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, ruleName) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if protectBranch != nil { ctx.APIError(http.StatusForbidden, "Branch protection already exist") @@ -620,7 +620,7 @@ func CreateBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } forcePushAllowlistUsers, err := user_model.GetUserIDsByNames(ctx, form.ForcePushAllowlistUsernames, false) @@ -629,7 +629,7 @@ func CreateBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } mergeWhitelistUsers, err := user_model.GetUserIDsByNames(ctx, form.MergeWhitelistUsernames, false) @@ -638,7 +638,7 @@ func CreateBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } approvalsWhitelistUsers, err := user_model.GetUserIDsByNames(ctx, form.ApprovalsWhitelistUsernames, false) @@ -647,7 +647,7 @@ func CreateBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } var whitelistTeams, forcePushAllowlistTeams, mergeWhitelistTeams, approvalsWhitelistTeams []int64 @@ -658,7 +658,7 @@ func CreateBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } forcePushAllowlistTeams, err = organization.GetTeamIDsByNames(ctx, repo.OwnerID, form.ForcePushAllowlistTeams, false) @@ -667,7 +667,7 @@ func CreateBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } mergeWhitelistTeams, err = organization.GetTeamIDsByNames(ctx, repo.OwnerID, form.MergeWhitelistTeams, false) @@ -676,7 +676,7 @@ func CreateBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } approvalsWhitelistTeams, err = organization.GetTeamIDsByNames(ctx, repo.OwnerID, form.ApprovalsWhitelistTeams, false) @@ -685,7 +685,7 @@ func CreateBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -726,13 +726,13 @@ func CreateBranchProtection(ctx *context.APIContext) { ApprovalsUserIDs: approvalsWhitelistUsers, ApprovalsTeamIDs: approvalsWhitelistTeams, }); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if isBranchExist { if err := pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, ruleName); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -740,20 +740,20 @@ func CreateBranchProtection(ctx *context.APIContext) { if ctx.Repo.GitRepo == nil { ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } // FIXME: since we only need to recheck files protected rules, we could improve this matchedBranches, err := git_model.FindAllMatchedBranches(ctx, ctx.Repo.Repository.ID, ruleName) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } for _, branchName := range matchedBranches { if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, branchName); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -763,11 +763,11 @@ func CreateBranchProtection(ctx *context.APIContext) { // Reload from db to get all whitelists bp, err := git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, ruleName) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if bp == nil || bp.RepoID != ctx.Repo.Repository.ID { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -817,7 +817,7 @@ func EditBranchProtection(ctx *context.APIContext) { bpName := ctx.PathParam("name") protectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if protectBranch == nil || protectBranch.RepoID != repo.ID { @@ -935,7 +935,7 @@ func EditBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -948,7 +948,7 @@ func EditBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -961,7 +961,7 @@ func EditBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -974,7 +974,7 @@ func EditBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -990,7 +990,7 @@ func EditBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -1003,7 +1003,7 @@ func EditBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -1016,7 +1016,7 @@ func EditBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -1029,7 +1029,7 @@ func EditBranchProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -1048,7 +1048,7 @@ func EditBranchProtection(ctx *context.APIContext) { ApprovalsTeamIDs: approvalsWhitelistTeams, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -1060,7 +1060,7 @@ func EditBranchProtection(ctx *context.APIContext) { if isBranchExist { if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, bpName); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -1068,7 +1068,7 @@ func EditBranchProtection(ctx *context.APIContext) { if ctx.Repo.GitRepo == nil { ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -1076,13 +1076,13 @@ func EditBranchProtection(ctx *context.APIContext) { // FIXME: since we only need to recheck files protected rules, we could improve this matchedBranches, err := git_model.FindAllMatchedBranches(ctx, ctx.Repo.Repository.ID, protectBranch.RuleName) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } for _, branchName := range matchedBranches { if err = pull_service.CheckPRsForBaseBranch(ctx, ctx.Repo.Repository, branchName); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -1092,11 +1092,11 @@ func EditBranchProtection(ctx *context.APIContext) { // Reload from db to ensure get all whitelists bp, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if bp == nil || bp.RepoID != ctx.Repo.Repository.ID { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -1136,7 +1136,7 @@ func DeleteBranchProtection(ctx *context.APIContext) { bpName := ctx.PathParam("name") bp, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if bp == nil || bp.RepoID != repo.ID { @@ -1145,7 +1145,7 @@ func DeleteBranchProtection(ctx *context.APIContext) { } if err := git_model.DeleteProtectedBranch(ctx, ctx.Repo.Repository, bp.ID); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -1189,7 +1189,7 @@ func UpdateBranchProtectionPriories(ctx *context.APIContext) { repo := ctx.Repo.Repository if err := git_model.UpdateProtectBranchPriorities(ctx, repo, form.IDs); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -1234,7 +1234,7 @@ func MergeUpstream(ctx *context.APIContext) { ctx.APIError(http.StatusNotFound, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, &api.MergeUpstreamResponse{MergeStyle: mergeStyle}) diff --git a/routers/api/v1/repo/collaborators.go b/routers/api/v1/repo/collaborators.go index 2e18987888..c397d7972b 100644 --- a/routers/api/v1/repo/collaborators.go +++ b/routers/api/v1/repo/collaborators.go @@ -59,7 +59,7 @@ func ListCollaborators(ctx *context.APIContext) { RepoID: ctx.Repo.Repository.ID, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -108,13 +108,13 @@ func IsCollaborator(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } isColab, err := repo_model.IsCollaborator(ctx, ctx.Repo.Repository.ID, user.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if isColab { @@ -168,13 +168,13 @@ func AddOrUpdateCollaborator(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if !collaborator.IsActive { - ctx.APIError(http.StatusInternalServerError, errors.New("collaborator's account is inactive")) + ctx.APIErrorInternal(errors.New("collaborator's account is inactive")) return } @@ -187,7 +187,7 @@ func AddOrUpdateCollaborator(ctx *context.APIContext) { if errors.Is(err, user_model.ErrBlockedUser) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -231,13 +231,13 @@ func DeleteCollaborator(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err := repo_service.DeleteCollaboration(ctx, ctx.Repo.Repository, collaborator); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) @@ -284,14 +284,14 @@ func GetRepoPermissions(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } permission, err := access_model.GetUserRepoPermission(ctx, ctx.Repo.Repository, collaborator) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -330,7 +330,7 @@ func GetReviewers(ctx *context.APIContext) { reviewers, err := pull_service.GetReviewers(ctx, ctx.Repo.Repository, ctx.Doer.ID, 0) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToUsers(ctx, ctx.Doer, reviewers)) @@ -362,7 +362,7 @@ func GetAssignees(ctx *context.APIContext) { assignees, err := repo_model.GetRepoAssignees(ctx, ctx.Repo.Repository) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToUsers(ctx, ctx.Doer, assignees)) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index be21cca159..03489d777b 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -79,13 +79,13 @@ func getCommit(ctx *context.APIContext, identifier string, toCommitOpts convert. ctx.APIErrorNotFound(identifier) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } json, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil, toCommitOpts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, json) @@ -182,20 +182,20 @@ func GetAllCommits(ctx *context.APIContext) { // no sha supplied - use default branch head, err := ctx.Repo.GitRepo.GetHEADBranch() if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } baseCommit, err = ctx.Repo.GitRepo.GetBranchCommit(head.Name) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { // get commit specified by sha baseCommit, err = ctx.Repo.GitRepo.GetCommit(sha) if err != nil { - ctx.NotFoundOrServerError("GetCommit", git.IsErrNotExist, err) + ctx.NotFoundOrServerError(err) return } } @@ -207,14 +207,14 @@ func GetAllCommits(ctx *context.APIContext) { Revision: []string{baseCommit.ID.String()}, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } // Query commits commits, err = baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize, not) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -231,7 +231,7 @@ func GetAllCommits(ctx *context.APIContext) { }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if commitsCountTotal == 0 { ctx.APIErrorNotFound("FileCommitsCount", nil) @@ -246,7 +246,7 @@ func GetAllCommits(ctx *context.APIContext) { Page: listOptions.Page, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -259,7 +259,7 @@ func GetAllCommits(ctx *context.APIContext) { // Create json struct apiCommits[i], err = convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, userCache, convert.ParseCommitOptions(ctx)) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -320,7 +320,7 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) { ctx.APIErrorNotFound(sha) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -359,17 +359,17 @@ func GetCommitPullRequest(ctx *context.APIContext) { if issues_model.IsErrPullRequestNotExist(err) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err = pr.LoadBaseRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err = pr.LoadHeadRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToAPIPullRequest(ctx, pr, ctx.Doer)) diff --git a/routers/api/v1/repo/compare.go b/routers/api/v1/repo/compare.go index 588f5f7919..6d427c8073 100644 --- a/routers/api/v1/repo/compare.go +++ b/routers/api/v1/repo/compare.go @@ -47,7 +47,7 @@ func CompareDiff(ctx *context.APIContext) { var err error ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } diff --git a/routers/api/v1/repo/download.go b/routers/api/v1/repo/download.go index d5d78ea52f..20901badfb 100644 --- a/routers/api/v1/repo/download.go +++ b/routers/api/v1/repo/download.go @@ -31,7 +31,7 @@ func DownloadArchive(ctx *context.APIContext) { var err error ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 5fd4c27d67..7e6a7ef087 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -84,7 +84,7 @@ func GetRawFile(ctx *context.APIContext) { ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry))) if err := common.ServeBlob(ctx.Base, ctx.Repo.TreePath, blob, lastModified); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } } @@ -231,7 +231,7 @@ func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, entry *git.TreeEn if git.IsErrNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil, nil, nil } @@ -243,7 +243,7 @@ func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, entry *git.TreeEn latestCommit, err := ctx.Repo.GitRepo.GetTreePathLatestCommit(ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, nil, nil } when := &latestCommit.Committer.When @@ -284,7 +284,7 @@ func GetArchive(ctx *context.APIContext) { var err error ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -389,7 +389,7 @@ func GetEditorconfig(ctx *context.APIContext) { if git.IsErrNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -736,7 +736,7 @@ func handleCreateOrUpdateFileError(ctx *context.APIContext, err error) { return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } // Called from both CreateFile or UpdateFile to handle both @@ -887,7 +887,7 @@ func DeleteFile(ctx *context.APIContext) { ctx.APIError(http.StatusForbidden, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } else { fileResponse := files_service.GetFileResponseFromFilesResponse(filesResponse, 0) ctx.JSON(http.StatusOK, fileResponse) // FIXME on APIv2: return http.StatusNoContent @@ -929,7 +929,7 @@ func GetContents(ctx *context.APIContext) { // "$ref": "#/responses/notFound" if !canReadFiles(ctx.Repo) { - ctx.APIError(http.StatusInternalServerError, repo_model.ErrUserDoesNotHaveAccessToRepo{ + ctx.APIErrorInternal(repo_model.ErrUserDoesNotHaveAccessToRepo{ UserID: ctx.Doer.ID, RepoName: ctx.Repo.Repository.LowerName, }) @@ -944,7 +944,7 @@ func GetContents(ctx *context.APIContext) { ctx.APIErrorNotFound("GetContentsOrList", err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } else { ctx.JSON(http.StatusOK, fileList) } diff --git a/routers/api/v1/repo/fork.go b/routers/api/v1/repo/fork.go index ce236d2a04..58f66954e1 100644 --- a/routers/api/v1/repo/fork.go +++ b/routers/api/v1/repo/fork.go @@ -57,15 +57,15 @@ func ListForks(ctx *context.APIContext) { forks, total, err := repo_service.FindForks(ctx, ctx.Repo.Repository, ctx.Doer, utils.GetListOptions(ctx)) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err := repo_model.RepositoryList(forks).LoadOwners(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err := repo_model.RepositoryList(forks).LoadUnits(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -73,7 +73,7 @@ func ListForks(ctx *context.APIContext) { for i, fork := range forks { permission, err := access_model.GetUserRepoPermission(ctx, fork, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiForks[i] = convert.ToRepo(ctx, fork, permission) @@ -128,14 +128,14 @@ func CreateFork(ctx *context.APIContext) { if organization.IsErrOrgNotExist(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if !ctx.Doer.IsAdmin { isMember, err := org.IsOrgMember(ctx, ctx.Doer.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if !isMember { ctx.APIError(http.StatusForbidden, fmt.Sprintf("User is no Member of Organisation '%s'", org.Name)) @@ -163,7 +163,7 @@ func CreateFork(ctx *context.APIContext) { } else if errors.Is(err, user_model.ErrBlockedUser) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/routers/api/v1/repo/git_hook.go b/routers/api/v1/repo/git_hook.go index 10e8aa6474..487c74e183 100644 --- a/routers/api/v1/repo/git_hook.go +++ b/routers/api/v1/repo/git_hook.go @@ -40,7 +40,7 @@ func ListGitHooks(ctx *context.APIContext) { hooks, err := ctx.Repo.GitRepo.Hooks() if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -86,7 +86,7 @@ func GetGitHook(ctx *context.APIContext) { if errors.Is(err, git.ErrNotValidHook) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -133,14 +133,14 @@ func EditGitHook(ctx *context.APIContext) { if errors.Is(err, git.ErrNotValidHook) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } hook.Content = form.Content if err = hook.Update(); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -182,14 +182,14 @@ func DeleteGitHook(ctx *context.APIContext) { if errors.Is(err, git.ErrNotValidHook) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } hook.Content = "" if err = hook.Update(); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/repo/git_ref.go b/routers/api/v1/repo/git_ref.go index e2030ac296..f042e9e344 100644 --- a/routers/api/v1/repo/git_ref.go +++ b/routers/api/v1/repo/git_ref.go @@ -78,7 +78,7 @@ func GetGitRefs(ctx *context.APIContext) { func getGitRefsInternal(ctx *context.APIContext, filter string) { refs, lastMethodName, err := utils.GetGitRefs(ctx, filter) if err != nil { - ctx.APIError(http.StatusInternalServerError, fmt.Errorf("%s: %w", lastMethodName, err)) + ctx.APIErrorInternal(fmt.Errorf("%s: %w", lastMethodName, err)) return } diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index 241ca6a732..ac47e15d64 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -189,7 +189,7 @@ func TestHook(ctx *context.APIContext) { Pusher: convert.ToUserWithAccessMode(ctx, ctx.Doer, perm.AccessModeNone), Sender: convert.ToUserWithAccessMode(ctx, ctx.Doer, perm.AccessModeNone), }); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -300,7 +300,7 @@ func DeleteHook(ctx *context.APIContext) { if webhook.IsErrWebhookNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 04da46a1a3..c9575ff98a 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -172,7 +172,7 @@ func SearchIssues(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -191,7 +191,7 @@ func SearchIssues(ctx *context.APIContext) { if organization.IsErrTeamNotExist(err) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -204,7 +204,7 @@ func SearchIssues(ctx *context.APIContext) { } repoIDs, _, err = repo_model.SearchRepositoryIDs(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if len(repoIDs) == 0 { @@ -237,7 +237,7 @@ func SearchIssues(ctx *context.APIContext) { } includedAnyLabels, err = issues_model.GetLabelIDsByNames(ctx, includedLabelNames) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -251,7 +251,7 @@ func SearchIssues(ctx *context.APIContext) { } includedMilestones, err = issues_model.GetMilestoneIDsByNames(ctx, includedMilestoneNames) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -312,12 +312,12 @@ func SearchIssues(ctx *context.APIContext) { ids, total, err := issue_indexer.SearchIssues(ctx, searchOpt) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } issues, err := issues_model.GetIssuesByIDs(ctx, ids, true) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -428,7 +428,7 @@ func ListIssues(ctx *context.APIContext) { if splitted := strings.Split(ctx.FormString("labels"), ","); len(splitted) > 0 { labelIDs, err = issues_model.GetLabelIDsInRepoByNames(ctx, ctx.Repo.Repository.ID, splitted) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -444,7 +444,7 @@ func ListIssues(ctx *context.APIContext) { continue } if !issues_model.IsErrMilestoneNotExist(err) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } id, err := strconv.ParseInt(part[i], 10, 64) @@ -459,7 +459,7 @@ func ListIssues(ctx *context.APIContext) { if issues_model.IsErrMilestoneNotExist(err) { continue } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } } @@ -549,12 +549,12 @@ func ListIssues(ctx *context.APIContext) { ids, total, err := issue_indexer.SearchIssues(ctx, searchOpt) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } issues, err := issues_model.GetIssuesByIDs(ctx, ids, true) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -618,7 +618,7 @@ func GetIssue(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -693,7 +693,7 @@ func CreateIssue(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("Assignee does not exist: [name: %s]", err)) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -702,13 +702,13 @@ func CreateIssue(ctx *context.APIContext) { for _, aID := range assigneeIDs { assignee, err := user_model.GetUserByID(ctx, aID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } valid, err := access_model.CanBeAssigned(ctx, assignee, ctx.Repo.Repository, false) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if !valid { @@ -727,7 +727,7 @@ func CreateIssue(ctx *context.APIContext) { } else if errors.Is(err, user_model.ErrBlockedUser) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -738,7 +738,7 @@ func CreateIssue(ctx *context.APIContext) { ctx.APIError(http.StatusPreconditionFailed, "cannot close this issue because it still has open dependencies") return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -746,7 +746,7 @@ func CreateIssue(ctx *context.APIContext) { // Refetch from database to assign some automatic values issue, err = issues_model.GetIssueByID(ctx, issue.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusCreated, convert.ToAPIIssue(ctx, ctx.Doer, issue)) @@ -798,7 +798,7 @@ func EditIssue(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -807,7 +807,7 @@ func EditIssue(ctx *context.APIContext) { err = issue.LoadAttributes(ctx) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -819,7 +819,7 @@ func EditIssue(ctx *context.APIContext) { if len(form.Title) > 0 { err = issue_service.ChangeTitle(ctx, issue, ctx.Doer, form.Title) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -831,14 +831,14 @@ func EditIssue(ctx *context.APIContext) { return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } if form.Ref != nil { err = issue_service.ChangeIssueRef(ctx, issue, ctx.Doer, *form.Ref) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -860,7 +860,7 @@ func EditIssue(ctx *context.APIContext) { } if err := issues_model.UpdateIssueDeadline(ctx, issue, deadlineUnix, ctx.Doer); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } issue.DeadlineUnix = deadlineUnix @@ -885,7 +885,7 @@ func EditIssue(ctx *context.APIContext) { if errors.Is(err, user_model.ErrBlockedUser) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -896,14 +896,14 @@ func EditIssue(ctx *context.APIContext) { oldMilestoneID := issue.MilestoneID issue.MilestoneID = *form.Milestone if err = issue_service.ChangeMilestoneAssign(ctx, issue, ctx.Doer, oldMilestoneID); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } if form.State != nil { if issue.IsPull { if err := issue.LoadPullRequest(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if issue.PullRequest.HasMerged { @@ -965,13 +965,13 @@ func DeleteIssue(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err = issue_service.DeleteIssue(ctx, ctx.Doer, ctx.Repo.GitRepo, issue); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -1021,7 +1021,7 @@ func UpdateIssueDeadline(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -1033,7 +1033,7 @@ func UpdateIssueDeadline(ctx *context.APIContext) { deadlineUnix, _ := common.ParseAPIDeadlineToEndOfDay(form.Deadline) if err := issues_model.UpdateIssueDeadline(ctx, issue, deadlineUnix, ctx.Doer); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -1052,12 +1052,12 @@ func closeOrReopenIssue(ctx *context.APIContext, issue *issues_model.Issue, stat ctx.APIError(http.StatusPreconditionFailed, "cannot close this issue or pull request because it still has open dependencies") return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else if state == api.StateOpen && issue.IsClosed { if err := issue_service.ReopenIssue(ctx, issue, ctx.Doer, ""); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } diff --git a/routers/api/v1/repo/issue_attachment.go b/routers/api/v1/repo/issue_attachment.go index b063054424..3f751a295c 100644 --- a/routers/api/v1/repo/issue_attachment.go +++ b/routers/api/v1/repo/issue_attachment.go @@ -104,7 +104,7 @@ func ListIssueAttachments(ctx *context.APIContext) { } if err := issue.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -171,7 +171,7 @@ func CreateIssueAttachment(ctx *context.APIContext) { // Get uploaded file from request file, header, err := ctx.Req.FormFile("attachment") if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } defer file.Close() @@ -191,7 +191,7 @@ func CreateIssueAttachment(ctx *context.APIContext) { if upload.IsErrFileTypeForbidden(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -199,7 +199,7 @@ func CreateIssueAttachment(ctx *context.APIContext) { issue.Attachments = append(issue.Attachments, attachment) if err := issue_service.ChangeContent(ctx, issue, ctx.Doer, issue.Content, issue.ContentVersion); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -268,7 +268,7 @@ func EditIssueAttachment(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -319,7 +319,7 @@ func DeleteIssueAttachment(ctx *context.APIContext) { } if err := repo_model.DeleteAttachment(ctx, attachment, true); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -329,7 +329,7 @@ func DeleteIssueAttachment(ctx *context.APIContext) { func getIssueFromContext(ctx *context.APIContext) *issues_model.Issue { issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) if err != nil { - ctx.NotFoundOrServerError("GetIssueByIndex", issues_model.IsErrIssueNotExist, err) + ctx.NotFoundOrServerError(err) return nil } @@ -354,7 +354,7 @@ func getIssueAttachmentSafeWrite(ctx *context.APIContext) *repo_model.Attachment func getIssueAttachmentSafeRead(ctx *context.APIContext, issue *issues_model.Issue) *repo_model.Attachment { attachment, err := repo_model.GetAttachmentByID(ctx, ctx.PathParamInt64("attachment_id")) if err != nil { - ctx.NotFoundOrServerError("GetAttachmentByID", repo_model.IsErrAttachmentNotExist, err) + ctx.NotFoundOrServerError(err) return nil } if !attachmentBelongsToRepoOrIssue(ctx, attachment, issue) { diff --git a/routers/api/v1/repo/issue_comment.go b/routers/api/v1/repo/issue_comment.go index 6efa4ee6b5..0c572a06a8 100644 --- a/routers/api/v1/repo/issue_comment.go +++ b/routers/api/v1/repo/issue_comment.go @@ -70,7 +70,7 @@ func ListIssueComments(ctx *context.APIContext) { } issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if !ctx.Repo.CanReadIssuesOrPulls(issue.IsPull) { @@ -89,7 +89,7 @@ func ListIssueComments(ctx *context.APIContext) { comments, err := issues_model.FindComments(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -100,12 +100,12 @@ func ListIssueComments(ctx *context.APIContext) { } if err := comments.LoadPosters(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err := comments.LoadAttachments(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -174,7 +174,7 @@ func ListIssueCommentsAndTimeline(ctx *context.APIContext) { } issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } issue.Repo = ctx.Repo.Repository @@ -189,12 +189,12 @@ func ListIssueCommentsAndTimeline(ctx *context.APIContext) { comments, err := issues_model.FindComments(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err := comments.LoadPosters(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -303,7 +303,7 @@ func ListRepoIssueComments(ctx *context.APIContext) { comments, err := issues_model.FindComments(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -314,21 +314,21 @@ func ListRepoIssueComments(ctx *context.APIContext) { } if err = comments.LoadPosters(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiComments := make([]*api.Comment, len(comments)) if err := comments.LoadIssues(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err := comments.LoadAttachments(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if _, err := comments.Issues().LoadRepositories(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } for i := range comments { @@ -382,7 +382,7 @@ func CreateIssueComment(ctx *context.APIContext) { form := web.GetForm(ctx).(*api.CreateIssueCommentOption) issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -401,7 +401,7 @@ func CreateIssueComment(ctx *context.APIContext) { if errors.Is(err, user_model.ErrBlockedUser) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -450,7 +450,7 @@ func GetIssueComment(ctx *context.APIContext) { if issues_model.IsErrCommentNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -475,7 +475,7 @@ func GetIssueComment(ctx *context.APIContext) { } if err := comment.LoadPoster(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -584,13 +584,13 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) if issues_model.IsErrCommentNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err := comment.LoadIssue(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -615,7 +615,7 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) if errors.Is(err, user_model.ErrBlockedUser) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -701,13 +701,13 @@ func deleteIssueComment(ctx *context.APIContext) { if issues_model.IsErrCommentNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err := comment.LoadIssue(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -725,7 +725,7 @@ func deleteIssueComment(ctx *context.APIContext) { } if err = issue_service.DeleteComment(ctx, ctx.Doer, comment); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/repo/issue_comment_attachment.go b/routers/api/v1/repo/issue_comment_attachment.go index 3dbfa8177a..5f660c5750 100644 --- a/routers/api/v1/repo/issue_comment_attachment.go +++ b/routers/api/v1/repo/issue_comment_attachment.go @@ -109,7 +109,7 @@ func ListIssueCommentAttachments(ctx *context.APIContext) { } if err := comment.LoadAttachments(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -179,7 +179,7 @@ func CreateIssueCommentAttachment(ctx *context.APIContext) { // Get uploaded file from request file, header, err := ctx.Req.FormFile("attachment") if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } defer file.Close() @@ -200,13 +200,13 @@ func CreateIssueCommentAttachment(ctx *context.APIContext) { if upload.IsErrFileTypeForbidden(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err := comment.LoadAttachments(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -282,7 +282,7 @@ func EditIssueCommentAttachment(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, attach) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attach)) @@ -331,7 +331,7 @@ func DeleteIssueCommentAttachment(ctx *context.APIContext) { } if err := repo_model.DeleteAttachment(ctx, attach, true); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) @@ -340,11 +340,11 @@ func DeleteIssueCommentAttachment(ctx *context.APIContext) { func getIssueCommentSafe(ctx *context.APIContext) *issues_model.Comment { comment, err := issues_model.GetCommentByID(ctx, ctx.PathParamInt64("id")) if err != nil { - ctx.NotFoundOrServerError("GetCommentByID", issues_model.IsErrCommentNotExist, err) + ctx.NotFoundOrServerError(err) return nil } if err := comment.LoadIssue(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil } if comment.Issue == nil || comment.Issue.RepoID != ctx.Repo.Repository.ID { @@ -385,7 +385,7 @@ func canUserWriteIssueCommentAttachment(ctx *context.APIContext, comment *issues func getIssueCommentAttachmentSafeRead(ctx *context.APIContext, comment *issues_model.Comment) *repo_model.Attachment { attachment, err := repo_model.GetAttachmentByID(ctx, ctx.PathParamInt64("attachment_id")) if err != nil { - ctx.NotFoundOrServerError("GetAttachmentByID", repo_model.IsErrAttachmentNotExist, err) + ctx.NotFoundOrServerError(err) return nil } if !attachmentBelongsToRepoOrComment(ctx, attachment, comment) { diff --git a/routers/api/v1/repo/issue_dependency.go b/routers/api/v1/repo/issue_dependency.go index ac177100b9..2048c76ea0 100644 --- a/routers/api/v1/repo/issue_dependency.go +++ b/routers/api/v1/repo/issue_dependency.go @@ -66,7 +66,7 @@ func GetIssueDependencies(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound("IsErrIssueNotExist", err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -98,7 +98,7 @@ func GetIssueDependencies(ctx *context.APIContext) { PageSize: limit, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -342,7 +342,7 @@ func GetIssueBlocks(ctx *context.APIContext) { deps, err := issue.BlockingDependencies(ctx) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -504,7 +504,7 @@ func getParamsIssue(ctx *context.APIContext) *issues_model.Issue { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound("IsErrIssueNotExist", err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil } @@ -525,7 +525,7 @@ func getFormIssue(ctx *context.APIContext, form *api.IssueMeta) *issues_model.Is if repo_model.IsErrRepoNotExist(err) { ctx.APIErrorNotFound("IsErrRepoNotExist", err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil } @@ -538,7 +538,7 @@ func getFormIssue(ctx *context.APIContext, form *api.IssueMeta) *issues_model.Is if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound("IsErrIssueNotExist", err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil } @@ -553,7 +553,7 @@ func getPermissionForRepo(ctx *context.APIContext, repo *repo_model.Repository) perm, err := access_model.GetUserRepoPermission(ctx, repo, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil } @@ -581,7 +581,7 @@ func createIssueDependency(ctx *context.APIContext, target, dependency *issues_m err := issues_model.CreateIssueDependency(ctx, ctx.Doer, target, dependency) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -607,7 +607,7 @@ func removeIssueDependency(ctx *context.APIContext, target, dependency *issues_m err := issues_model.RemoveIssueDependency(ctx, ctx.Doer, target, dependency, issues_model.DependencyTypeBlockedBy) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } diff --git a/routers/api/v1/repo/issue_label.go b/routers/api/v1/repo/issue_label.go index 3715a8e952..f8e14e0490 100644 --- a/routers/api/v1/repo/issue_label.go +++ b/routers/api/v1/repo/issue_label.go @@ -52,13 +52,13 @@ func ListIssueLabels(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err := issue.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -110,13 +110,13 @@ func AddIssueLabels(ctx *context.APIContext) { } if err = issue_service.AddLabels(ctx, issue, ctx.Doer, labels); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } labels, err = issues_model.GetLabelsByIssueID(ctx, issue.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -168,7 +168,7 @@ func DeleteIssueLabel(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -183,13 +183,13 @@ func DeleteIssueLabel(ctx *context.APIContext) { if issues_model.IsErrLabelNotExist(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err := issue_service.RemoveLabel(ctx, issue, ctx.Doer, label); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -240,13 +240,13 @@ func ReplaceIssueLabels(ctx *context.APIContext) { } if err := issue_service.ReplaceLabels(ctx, issue, ctx.Doer, labels); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } labels, err = issues_model.GetLabelsByIssueID(ctx, issue.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -290,7 +290,7 @@ func ClearIssueLabels(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -301,7 +301,7 @@ func ClearIssueLabels(ctx *context.APIContext) { } if err := issue_service.ClearLabels(ctx, issue, ctx.Doer); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -314,7 +314,7 @@ func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption) if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil, nil, err } @@ -347,14 +347,14 @@ func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption) if len(labelNames) > 0 { repoLabelIDs, err := issues_model.GetLabelIDsInRepoByNames(ctx, ctx.Repo.Repository.ID, labelNames) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, nil, err } labelIDs = append(labelIDs, repoLabelIDs...) if ctx.Repo.Owner.IsOrganization() { orgLabelIDs, err := issues_model.GetLabelIDsInOrgByNames(ctx, ctx.Repo.Owner.ID, labelNames) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, nil, err } labelIDs = append(labelIDs, orgLabelIDs...) @@ -363,7 +363,7 @@ func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption) labels, err := issues_model.GetLabelsByIDs(ctx, labelIDs, "id", "repo_id", "org_id", "name", "exclusive") if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, nil, err } diff --git a/routers/api/v1/repo/issue_pin.go b/routers/api/v1/repo/issue_pin.go index 5e55b7c2d6..71985ac765 100644 --- a/routers/api/v1/repo/issue_pin.go +++ b/routers/api/v1/repo/issue_pin.go @@ -48,7 +48,7 @@ func PinIssue(ctx *context.APIContext) { } else if issues_model.IsErrIssueMaxPinReached(err) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -56,13 +56,13 @@ func PinIssue(ctx *context.APIContext) { // If we don't do this, it will crash when trying to add the pin event to the comment history err = issue.LoadRepo(ctx) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } err = issues_model.PinIssue(ctx, issue, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -103,7 +103,7 @@ func UnpinIssue(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -111,13 +111,13 @@ func UnpinIssue(ctx *context.APIContext) { // If we don't do this, it will crash when trying to add the unpin event to the comment history err = issue.LoadRepo(ctx) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } err = issues_model.UnpinIssue(ctx, issue, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -164,14 +164,14 @@ func MoveIssuePin(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } err = issues_model.MovePin(ctx, issue, int(ctx.PathParamInt64("position"))) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -203,7 +203,7 @@ func ListPinnedIssues(ctx *context.APIContext) { // "$ref": "#/responses/notFound" issues, err := issues_model.GetPinnedIssues(ctx, ctx.Repo.Repository.ID, false) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -235,29 +235,29 @@ func ListPinnedPullRequests(ctx *context.APIContext) { // "$ref": "#/responses/notFound" issues, err := issues_model.GetPinnedIssues(ctx, ctx.Repo.Repository.ID, true) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiPrs := make([]*api.PullRequest, len(issues)) if err := issues.LoadPullRequests(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } for i, currentIssue := range issues { pr := currentIssue.PullRequest if err = pr.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err = pr.LoadBaseRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err = pr.LoadHeadRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -295,13 +295,13 @@ func AreNewIssuePinsAllowed(ctx *context.APIContext) { pinsAllowed.Issues, err = issues_model.IsNewPinAllowed(ctx, ctx.Repo.Repository.ID, false) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } pinsAllowed.PullRequests, err = issues_model.IsNewPinAllowed(ctx, ctx.Repo.Repository.ID, true) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/repo/issue_reaction.go b/routers/api/v1/repo/issue_reaction.go index ce669e410a..e535b5e009 100644 --- a/routers/api/v1/repo/issue_reaction.go +++ b/routers/api/v1/repo/issue_reaction.go @@ -56,13 +56,13 @@ func GetIssueCommentReactions(ctx *context.APIContext) { if issues_model.IsErrCommentNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err := comment.LoadIssue(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -78,12 +78,12 @@ func GetIssueCommentReactions(ctx *context.APIContext) { reactions, _, err := issues_model.FindCommentReactions(ctx, comment.IssueID, comment.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } _, err = reactions.LoadUsers(ctx, ctx.Repo.Repository) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -193,13 +193,13 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp if issues_model.IsErrCommentNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err = comment.LoadIssue(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -231,7 +231,7 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp Created: reaction.CreatedUnix.AsTime(), }) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -245,7 +245,7 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp // DeleteIssueCommentReaction part err = issues_model.DeleteCommentReaction(ctx, ctx.Doer.ID, comment.Issue.ID, comment.ID, form.Reaction) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } // ToDo respond 204 @@ -300,7 +300,7 @@ func GetIssueReactions(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -312,12 +312,12 @@ func GetIssueReactions(ctx *context.APIContext) { reactions, count, err := issues_model.FindIssueReactions(ctx, issue.ID, utils.GetListOptions(ctx)) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } _, err = reactions.LoadUsers(ctx, ctx.Repo.Repository) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -424,7 +424,7 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -447,7 +447,7 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i Created: reaction.CreatedUnix.AsTime(), }) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -461,7 +461,7 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i // DeleteIssueReaction part err = issues_model.DeleteIssueReaction(ctx, ctx.Doer.ID, issue.ID, form.Reaction) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } // ToDo respond 204 diff --git a/routers/api/v1/repo/issue_stopwatch.go b/routers/api/v1/repo/issue_stopwatch.go index 0a29dd2aa1..b18e172b37 100644 --- a/routers/api/v1/repo/issue_stopwatch.go +++ b/routers/api/v1/repo/issue_stopwatch.go @@ -55,7 +55,7 @@ func StartIssueStopwatch(ctx *context.APIContext) { } if err := issues_model.CreateIssueStopwatch(ctx, ctx.Doer, issue); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -104,7 +104,7 @@ func StopIssueStopwatch(ctx *context.APIContext) { } if err := issues_model.FinishIssueStopwatch(ctx, ctx.Doer, issue); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -153,7 +153,7 @@ func DeleteIssueStopwatch(ctx *context.APIContext) { } if err := issues_model.CancelStopwatch(ctx, ctx.Doer, issue); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -166,7 +166,7 @@ func prepareIssueStopwatch(ctx *context.APIContext, shouldExist bool) (*issues_m if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil, err @@ -220,7 +220,7 @@ func GetStopwatches(ctx *context.APIContext) { sws, err := issues_model.GetUserStopwatches(ctx, ctx.Doer.ID, utils.GetListOptions(ctx)) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -232,7 +232,7 @@ func GetStopwatches(ctx *context.APIContext) { apiSWs, err := convert.ToStopWatches(ctx, sws) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/repo/issue_subscription.go b/routers/api/v1/repo/issue_subscription.go index acf72c64cd..21e549496d 100644 --- a/routers/api/v1/repo/issue_subscription.go +++ b/routers/api/v1/repo/issue_subscription.go @@ -109,7 +109,7 @@ func setIssueSubscription(ctx *context.APIContext, watch bool) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return @@ -120,7 +120,7 @@ func setIssueSubscription(ctx *context.APIContext, watch bool) { if user_model.IsErrUserNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return @@ -134,7 +134,7 @@ func setIssueSubscription(ctx *context.APIContext, watch bool) { current, err := issues_model.CheckIssueWatch(ctx, user, issue) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -146,7 +146,7 @@ func setIssueSubscription(ctx *context.APIContext, watch bool) { // Update watch state if err := issues_model.CreateOrUpdateIssueWatch(ctx, user.ID, issue.ID, watch); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -190,7 +190,7 @@ func CheckIssueSubscription(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return @@ -256,7 +256,7 @@ func GetIssueSubscribers(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return @@ -264,7 +264,7 @@ func GetIssueSubscribers(ctx *context.APIContext) { iwl, err := issues_model.GetIssueWatchers(ctx, issue.ID, utils.GetListOptions(ctx)) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -275,7 +275,7 @@ func GetIssueSubscribers(ctx *context.APIContext) { users, err := user_model.GetUsersByIDs(ctx, userIDs) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiUsers := make([]*api.User, 0, len(users)) @@ -285,7 +285,7 @@ func GetIssueSubscribers(ctx *context.APIContext) { count, err := issues_model.CountIssueWatchers(ctx, issue.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/repo/issue_tracked_time.go b/routers/api/v1/repo/issue_tracked_time.go index 8cead42267..dbb2afa920 100644 --- a/routers/api/v1/repo/issue_tracked_time.go +++ b/routers/api/v1/repo/issue_tracked_time.go @@ -80,7 +80,7 @@ func ListTrackedTimes(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -97,7 +97,7 @@ func ListTrackedTimes(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusNotFound, err) } else if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } opts.UserID = user.ID @@ -129,11 +129,11 @@ func ListTrackedTimes(ctx *context.APIContext) { trackedTimes, err := issues_model.GetTrackedTimes(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err = trackedTimes.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -186,7 +186,7 @@ func AddTime(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -206,7 +206,7 @@ func AddTime(ctx *context.APIContext) { // allow only RepoAdmin, Admin and User to add time user, err = user_model.GetUserByName(ctx, form.User) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } } } @@ -218,11 +218,11 @@ func AddTime(ctx *context.APIContext) { trackedTime, err := issues_model.AddTime(ctx, user, issue, form.Time, created) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err = trackedTime.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToTrackedTime(ctx, user, trackedTime)) @@ -269,7 +269,7 @@ func ResetIssueTime(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -288,7 +288,7 @@ func ResetIssueTime(ctx *context.APIContext) { if db.IsErrNotExist(err) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -342,7 +342,7 @@ func DeleteTime(ctx *context.APIContext) { if issues_model.IsErrIssueNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -362,7 +362,7 @@ func DeleteTime(ctx *context.APIContext) { ctx.APIErrorNotFound(err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if time.Deleted { @@ -378,7 +378,7 @@ func DeleteTime(ctx *context.APIContext) { err = issues_model.DeleteTime(ctx, time) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) @@ -427,7 +427,7 @@ func ListTrackedTimesByUser(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -448,11 +448,11 @@ func ListTrackedTimesByUser(ctx *context.APIContext) { trackedTimes, err := issues_model.GetTrackedTimes(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err = trackedTimes.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToTrackedTimeList(ctx, ctx.Doer, trackedTimes)) @@ -525,7 +525,7 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusNotFound, err) } else if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } opts.UserID = user.ID @@ -558,11 +558,11 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) { trackedTimes, err := issues_model.GetTrackedTimes(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err = trackedTimes.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -619,12 +619,12 @@ func ListMyTrackedTimes(ctx *context.APIContext) { trackedTimes, err := issues_model.GetTrackedTimes(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err = trackedTimes.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/repo/key.go b/routers/api/v1/repo/key.go index f6f7b01585..8cb61e9e0c 100644 --- a/routers/api/v1/repo/key.go +++ b/routers/api/v1/repo/key.go @@ -100,7 +100,7 @@ func ListDeployKeys(ctx *context.APIContext) { apiKeys := make([]*api.DeployKey, len(keys)) for i := range keys { if err := keys[i].GetContent(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiKeys[i] = convert.ToDeployKey(apiLink, keys[i]) @@ -148,7 +148,7 @@ func GetDeployKey(ctx *context.APIContext) { if asymkey_model.IsErrDeployKeyNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -160,7 +160,7 @@ func GetDeployKey(ctx *context.APIContext) { } if err = key.GetContent(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -195,7 +195,7 @@ func HandleAddKeyError(ctx *context.APIContext, err error) { case asymkey_model.IsErrDeployKeyNameAlreadyUsed(err): ctx.APIError(http.StatusUnprocessableEntity, "A key with the same name already exists") default: - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } } @@ -283,7 +283,7 @@ func DeleteDeploykey(ctx *context.APIContext) { if asymkey_model.IsErrKeyAccessDenied(err) { ctx.APIError(http.StatusForbidden, "You do not have access to this key") } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go index 357718628f..4f79d42595 100644 --- a/routers/api/v1/repo/label.go +++ b/routers/api/v1/repo/label.go @@ -51,7 +51,7 @@ func ListLabels(ctx *context.APIContext) { labels, err := issues_model.GetLabelsByRepoID(ctx, ctx.Repo.Repository.ID, ctx.FormString("sort"), utils.GetListOptions(ctx)) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -109,7 +109,7 @@ func GetLabel(ctx *context.APIContext) { if issues_model.IsErrRepoLabelNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -166,7 +166,7 @@ func CreateLabel(ctx *context.APIContext) { } l.SetArchived(form.IsArchived) if err := issues_model.NewLabel(ctx, l); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -217,7 +217,7 @@ func EditLabel(ctx *context.APIContext) { if issues_model.IsErrRepoLabelNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -241,7 +241,7 @@ func EditLabel(ctx *context.APIContext) { } l.SetArchived(form.IsArchived != nil && *form.IsArchived) if err := issues_model.UpdateLabel(ctx, l); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -277,7 +277,7 @@ func DeleteLabel(ctx *context.APIContext) { // "$ref": "#/responses/notFound" if err := issues_model.DeleteLabel(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("id")); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/repo/migrate.go b/routers/api/v1/repo/migrate.go index 46563b8f22..d7508684a1 100644 --- a/routers/api/v1/repo/migrate.go +++ b/routers/api/v1/repo/migrate.go @@ -74,7 +74,7 @@ func Migrate(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -94,7 +94,7 @@ func Migrate(ctx *context.APIContext) { // Check ownership of organization. isOwner, err := organization.OrgFromUser(repoOwner).IsOwnedBy(ctx, ctx.Doer.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if !isOwner { ctx.APIError(http.StatusForbidden, "Given user is not owner of organization.") @@ -129,7 +129,7 @@ func Migrate(ctx *context.APIContext) { if form.LFS && len(form.LFSEndpoint) > 0 { ep := lfs.DetermineEndpoint("", form.LFSEndpoint) if ep == nil { - ctx.APIError(http.StatusInternalServerError, ctx.Tr("repo.migrate.invalid_lfs_endpoint")) + ctx.APIErrorInternal(errors.New("the LFS endpoint is not valid")) return } err = migrations.IsMigrateURLAllowed(ep.String(), ctx.Doer) @@ -249,7 +249,7 @@ func handleMigrateError(ctx *context.APIContext, repoOwner *user_model.User, err } else if strings.Contains(err.Error(), "fatal:") { ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("Migration failed: %v.", err)) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } } } @@ -269,9 +269,9 @@ func handleRemoteAddrError(ctx *context.APIContext, err error) { case addrErr.IsInvalidPath: ctx.APIError(http.StatusUnprocessableEntity, "Invalid local path, it does not exist or not a directory.") default: - ctx.APIError(http.StatusInternalServerError, "Unknown error type (ErrInvalidCloneAddr): "+err.Error()) + ctx.APIErrorInternal(fmt.Errorf("unknown error type (ErrInvalidCloneAddr): %w", err)) } } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } } diff --git a/routers/api/v1/repo/milestone.go b/routers/api/v1/repo/milestone.go index 727f7a1a1b..33fa7c4b16 100644 --- a/routers/api/v1/repo/milestone.go +++ b/routers/api/v1/repo/milestone.go @@ -74,7 +74,7 @@ func ListMilestones(ctx *context.APIContext) { Name: ctx.FormString("name"), }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -173,7 +173,7 @@ func CreateMilestone(ctx *context.APIContext) { } if err := issues_model.NewMilestone(ctx, milestone); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusCreated, convert.ToAPIMilestone(milestone)) @@ -233,7 +233,7 @@ func EditMilestone(ctx *context.APIContext) { } if err := issues_model.UpdateMilestone(ctx, milestone, oldIsClosed); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToAPIMilestone(milestone)) @@ -272,7 +272,7 @@ func DeleteMilestone(ctx *context.APIContext) { } if err := issues_model.DeleteMilestoneByRepoID(ctx, ctx.Repo.Repository.ID, m.ID); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) @@ -288,7 +288,7 @@ func getMilestoneByIDOrName(ctx *context.APIContext) *issues_model.Milestone { if err == nil { return milestone } else if !issues_model.IsErrMilestoneNotExist(err) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil } } @@ -299,7 +299,7 @@ func getMilestoneByIDOrName(ctx *context.APIContext) *issues_model.Milestone { ctx.APIErrorNotFound() return nil } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil } diff --git a/routers/api/v1/repo/mirror.go b/routers/api/v1/repo/mirror.go index 59a4a3199a..b5f4c12c50 100644 --- a/routers/api/v1/repo/mirror.go +++ b/routers/api/v1/repo/mirror.go @@ -66,7 +66,7 @@ func MirrorSync(ctx *context.APIContext) { ctx.APIError(http.StatusBadRequest, "Repository is not a mirror") return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -116,7 +116,7 @@ func PushMirrorSync(ctx *context.APIContext) { for _, mirror := range pushMirrors { ok := mirror_service.SyncPushMirror(ctx, mirror.ID) if !ok { - ctx.APIError(http.StatusInternalServerError, "error occurred when syncing push mirror "+mirror.RemoteName) + ctx.APIErrorInternal(errors.New("error occurred when syncing push mirror " + mirror.RemoteName)) return } } @@ -230,7 +230,7 @@ func GetPushMirrorByName(ctx *context.APIContext) { RemoteName: mirrorName, }.ToConds()) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if !exist { ctx.APIError(http.StatusNotFound, nil) diff --git a/routers/api/v1/repo/notes.go b/routers/api/v1/repo/notes.go index 084879ba00..dcb512256c 100644 --- a/routers/api/v1/repo/notes.go +++ b/routers/api/v1/repo/notes.go @@ -71,7 +71,7 @@ func getNote(ctx *context.APIContext, identifier string) { if git.IsErrNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -82,7 +82,7 @@ func getNote(ctx *context.APIContext, identifier string) { ctx.APIErrorNotFound(identifier) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -96,7 +96,7 @@ func getNote(ctx *context.APIContext, identifier string) { Files: files, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiNote := api.Note{Message: string(note.Message), Commit: cmt} diff --git a/routers/api/v1/repo/patch.go b/routers/api/v1/repo/patch.go index b03dd1219d..bcf498bf7e 100644 --- a/routers/api/v1/repo/patch.go +++ b/routers/api/v1/repo/patch.go @@ -83,7 +83,7 @@ func ApplyDiffPatch(ctx *context.APIContext) { } if !canWriteFiles(ctx, apiOpts.BranchName) { - ctx.APIError(http.StatusInternalServerError, repo_model.ErrUserDoesNotHaveAccessToRepo{ + ctx.APIErrorInternal(repo_model.ErrUserDoesNotHaveAccessToRepo{ UserID: ctx.Doer.ID, RepoName: ctx.Repo.Repository.LowerName, }) @@ -105,7 +105,7 @@ func ApplyDiffPatch(ctx *context.APIContext) { ctx.APIError(http.StatusNotFound, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } else { ctx.JSON(http.StatusCreated, fileResponse) } diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 8809c7ed2d..1f61ac031a 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -108,7 +108,7 @@ func ListPullRequests(ctx *context.APIContext) { labelIDs, err := base.StringsToInt64s(ctx.FormStrings("labels")) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } var posterID int64 @@ -118,7 +118,7 @@ func ListPullRequests(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -134,13 +134,13 @@ func ListPullRequests(ctx *context.APIContext) { PosterID: posterID, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiPrs, err := convert.ToAPIPullRequests(ctx, ctx.Repo.Repository, prs, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -184,17 +184,17 @@ func GetPullRequest(ctx *context.APIContext) { if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err = pr.LoadBaseRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err = pr.LoadHeadRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToAPIPullRequest(ctx, pr, ctx.Doer)) @@ -254,7 +254,7 @@ func GetPullRequestByBaseHead(ctx *context.APIContext) { if repo_model.IsErrRepoNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -269,17 +269,17 @@ func GetPullRequestByBaseHead(ctx *context.APIContext) { if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err = pr.LoadBaseRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err = pr.LoadHeadRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToAPIPullRequest(ctx, pr, ctx.Doer)) @@ -417,7 +417,7 @@ func CreatePullRequest(ctx *context.APIContext) { ) if err != nil { if !issues_model.IsErrPullRequestNotExist(err) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } else { @@ -436,7 +436,7 @@ func CreatePullRequest(ctx *context.APIContext) { if len(form.Labels) > 0 { labels, err := issues_model.GetLabelsInRepoByIDs(ctx, ctx.Repo.Repository.ID, form.Labels) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -448,7 +448,7 @@ func CreatePullRequest(ctx *context.APIContext) { if ctx.Repo.Owner.IsOrganization() { orgLabels, err := issues_model.GetLabelsInOrgByIDs(ctx, ctx.Repo.Owner.ID, form.Labels) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -466,7 +466,7 @@ func CreatePullRequest(ctx *context.APIContext) { if issues_model.IsErrMilestoneNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, fmt.Errorf("GetMilestoneByRepoID: %w", err)) + ctx.APIErrorInternal(fmt.Errorf("GetMilestoneByRepoID: %w", err)) } return } @@ -506,7 +506,7 @@ func CreatePullRequest(ctx *context.APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("Assignee does not exist: [name: %s]", err)) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -514,13 +514,13 @@ func CreatePullRequest(ctx *context.APIContext) { for _, aID := range assigneeIDs { assignee, err := user_model.GetUserByID(ctx, aID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } valid, err := access_model.CanBeAssigned(ctx, assignee, repo, true) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if !valid { @@ -549,7 +549,7 @@ func CreatePullRequest(ctx *context.APIContext) { } else if errors.Is(err, issues_model.ErrMustCollaborator) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -608,21 +608,21 @@ func EditPullRequest(ctx *context.APIContext) { if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } err = pr.LoadIssue(ctx) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } issue := pr.Issue issue.Repo = ctx.Repo.Repository if err := issue.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -634,7 +634,7 @@ func EditPullRequest(ctx *context.APIContext) { if len(form.Title) > 0 { err = issue_service.ChangeTitle(ctx, issue, ctx.Doer, form.Title) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -646,7 +646,7 @@ func EditPullRequest(ctx *context.APIContext) { return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -661,7 +661,7 @@ func EditPullRequest(ctx *context.APIContext) { } if err := issues_model.UpdateIssueDeadline(ctx, issue, deadlineUnix, ctx.Doer); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } issue.DeadlineUnix = deadlineUnix @@ -683,7 +683,7 @@ func EditPullRequest(ctx *context.APIContext) { } else if errors.Is(err, user_model.ErrBlockedUser) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -694,7 +694,7 @@ func EditPullRequest(ctx *context.APIContext) { oldMilestoneID := issue.MilestoneID issue.MilestoneID = form.Milestone if err = issue_service.ChangeMilestoneAssign(ctx, issue, ctx.Doer, oldMilestoneID); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -702,14 +702,14 @@ func EditPullRequest(ctx *context.APIContext) { if ctx.Repo.CanWrite(unit.TypePullRequests) && form.Labels != nil { labels, err := issues_model.GetLabelsInRepoByIDs(ctx, ctx.Repo.Repository.ID, form.Labels) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if ctx.Repo.Owner.IsOrganization() { orgLabels, err := issues_model.GetLabelsInOrgByIDs(ctx, ctx.Repo.Owner.ID, form.Labels) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -717,7 +717,7 @@ func EditPullRequest(ctx *context.APIContext) { } if err = issues_model.ReplaceIssueLabels(ctx, issue, labels, ctx.Doer); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -776,7 +776,7 @@ func EditPullRequest(ctx *context.APIContext) { if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -820,7 +820,7 @@ func IsPullRequestMerged(ctx *context.APIContext) { if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -878,18 +878,18 @@ func MergePullRequest(ctx *context.APIContext) { if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound("GetPullRequestByIndex", err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err := pr.LoadHeadRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err := pr.LoadIssue(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } pr.Issue.Repo = ctx.Repo.Repository @@ -897,7 +897,7 @@ func MergePullRequest(ctx *context.APIContext) { if ctx.IsSigned { // Update issue-user. if err = activities_model.SetIssueReadBy(ctx, pr.Issue.ID, ctx.Doer.ID); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -945,7 +945,7 @@ func MergePullRequest(ctx *context.APIContext) { ctx.JSON(http.StatusConflict, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusOK) @@ -960,7 +960,7 @@ func MergePullRequest(ctx *context.APIContext) { if len(message) == 0 { message, _, err = pull_service.GetDefaultMergeMessage(ctx, ctx.Repo.GitRepo, pr, repo_model.MergeStyle(form.Do)) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -977,7 +977,7 @@ func MergePullRequest(ctx *context.APIContext) { ctx.APIError(http.StatusConflict, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if scheduled { // nothing more to do ... @@ -1010,7 +1010,7 @@ func MergePullRequest(ctx *context.APIContext) { ctx.APIError(http.StatusConflict, "PushRejected with remote message: "+errPushRej.Message) } } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -1053,7 +1053,7 @@ func MergePullRequest(ctx *context.APIContext) { case errors.Is(err, git_model.ErrBranchIsProtected): ctx.APIError(http.StatusForbidden, fmt.Errorf("branch protected")) default: - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -1094,7 +1094,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) if user_model.IsErrUserNotExist(err) { ctx.APIErrorNotFound("GetUserByName") } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil, nil } @@ -1110,7 +1110,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) if headRepo == nil && !isSameRepo { err = baseRepo.GetBaseRepo(ctx) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, nil } @@ -1132,7 +1132,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) } else { headGitRepo, err = gitrepo.OpenRepository(ctx, headRepo) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, nil } closer = func() { _ = headGitRepo.Close() } @@ -1146,7 +1146,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) // user should have permission to read baseRepo's codes and pulls, NOT headRepo's permBase, err := access_model.GetUserRepoPermission(ctx, baseRepo, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, nil } @@ -1160,7 +1160,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) // TODO: could the logic be simplified if the headRepo is the same as the baseRepo? Need to think more about it. permHead, err := access_model.GetUserRepoPermission(ctx, headRepo, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, nil } if !permHead.CanRead(unit.TypeCode) { @@ -1184,7 +1184,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) compareInfo, err := headGitRepo.GetCompareInfo(repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseRef.ShortName(), headRef.ShortName(), false, false) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, nil } @@ -1238,7 +1238,7 @@ func UpdatePullRequest(ctx *context.APIContext) { if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -1249,7 +1249,7 @@ func UpdatePullRequest(ctx *context.APIContext) { } if err = pr.LoadIssue(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -1259,11 +1259,11 @@ func UpdatePullRequest(ctx *context.APIContext) { } if err = pr.LoadBaseRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err = pr.LoadHeadRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -1271,7 +1271,7 @@ func UpdatePullRequest(ctx *context.APIContext) { allowedUpdateByMerge, allowedUpdateByRebase, err := pull_service.IsUserAllowedToUpdate(ctx, pr, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -1291,7 +1291,7 @@ func UpdatePullRequest(ctx *context.APIContext) { ctx.APIError(http.StatusConflict, "rebase failed because of conflict") return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -1423,7 +1423,7 @@ func GetPullRequestCommits(ctx *context.APIContext) { if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -1546,7 +1546,7 @@ func GetPullRequestFiles(ctx *context.APIContext) { if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index a351343f10..fb35126a99 100644 --- a/routers/api/v1/repo/pull_review.go +++ b/routers/api/v1/repo/pull_review.go @@ -66,18 +66,18 @@ func ListPullReviews(ctx *context.APIContext) { if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound("GetPullRequestByIndex", err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err = pr.LoadIssue(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err = pr.Issue.LoadRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -100,7 +100,7 @@ func ListPullReviews(ctx *context.APIContext) { apiReviews, err := convert.ToPullReviewList(ctx, allReviews, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -151,7 +151,7 @@ func GetPullReview(ctx *context.APIContext) { apiReview, err := convert.ToPullReview(ctx, review, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -201,7 +201,7 @@ func GetPullReviewComments(ctx *context.APIContext) { apiComments, err := convert.ToPullReviewCommentList(ctx, review, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -261,7 +261,7 @@ func DeletePullReview(ctx *context.APIContext) { } if err := issues_model.DeleteReview(ctx, review); err != nil { - ctx.APIError(http.StatusInternalServerError, fmt.Errorf("can not delete ReviewID: %d", review.ID)) + ctx.APIErrorInternal(fmt.Errorf("can not delete ReviewID: %d", review.ID)) return } @@ -311,7 +311,7 @@ func CreatePullReview(ctx *context.APIContext) { if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound("GetPullRequestByIndex", err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -323,7 +323,7 @@ func CreatePullReview(ctx *context.APIContext) { } if err := pr.Issue.LoadRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -331,14 +331,14 @@ func CreatePullReview(ctx *context.APIContext) { if opts.CommitID == "" { gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.Issue.Repo) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } defer closer.Close() headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName()) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -364,7 +364,7 @@ func CreatePullReview(ctx *context.APIContext) { opts.CommitID, nil, ); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -375,7 +375,7 @@ func CreatePullReview(ctx *context.APIContext) { if errors.Is(err, pull_service.ErrSubmitReviewOnClosedPR) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -383,7 +383,7 @@ func CreatePullReview(ctx *context.APIContext) { // convert response apiReview, err := convert.ToPullReview(ctx, review, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, apiReview) @@ -457,7 +457,7 @@ func SubmitPullReview(ctx *context.APIContext) { headCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(pr.GetGitRefName()) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -467,7 +467,7 @@ func SubmitPullReview(ctx *context.APIContext) { if errors.Is(err, pull_service.ErrSubmitReviewOnClosedPR) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -475,7 +475,7 @@ func SubmitPullReview(ctx *context.APIContext) { // convert response apiReview, err := convert.ToPullReview(ctx, review, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, apiReview) @@ -484,7 +484,7 @@ func SubmitPullReview(ctx *context.APIContext) { // preparePullReviewType return ReviewType and false or nil and true if an error happen func preparePullReviewType(ctx *context.APIContext, pr *issues_model.PullRequest, event api.ReviewStateType, body string, hasComments bool) (issues_model.ReviewType, bool) { if err := pr.LoadIssue(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return -1, true } @@ -538,7 +538,7 @@ func prepareSingleReview(ctx *context.APIContext) (*issues_model.Review, *issues if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound("GetPullRequestByIndex", err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil, nil, true } @@ -548,7 +548,7 @@ func prepareSingleReview(ctx *context.APIContext) (*issues_model.Review, *issues if issues_model.IsErrReviewNotExist(err) { ctx.APIErrorNotFound("GetReviewByID", err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil, nil, true } @@ -566,7 +566,7 @@ func prepareSingleReview(ctx *context.APIContext) (*issues_model.Review, *issues } if err := review.LoadAttributes(ctx); err != nil && !user_model.IsErrUserNotExist(err) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, nil, true } @@ -671,7 +671,7 @@ func parseReviewersByNames(ctx *context.APIContext, reviewerNames, teamReviewerN ctx.APIErrorNotFound("UserNotExist", fmt.Sprintf("User '%s' not exist", r)) return nil, nil } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, nil } @@ -687,7 +687,7 @@ func parseReviewersByNames(ctx *context.APIContext, reviewerNames, teamReviewerN ctx.APIErrorNotFound("TeamNotExist", fmt.Sprintf("Team '%s' not exist", t)) return nil, nil } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, nil } @@ -703,19 +703,19 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions if issues_model.IsErrPullRequestNotExist(err) { ctx.APIErrorNotFound("GetPullRequestByIndex", err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err := pr.Issue.LoadRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } permDoer, err := access_model.GetUserRepoPermission(ctx, pr.Issue.Repo, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -740,7 +740,7 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -782,7 +782,7 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions if isAdd { apiReviews, err := convert.ToPullReviewList(ctx, reviews, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusCreated, apiReviews) @@ -903,19 +903,19 @@ func dismissReview(ctx *context.APIContext, msg string, isDismiss, dismissPriors ctx.APIError(http.StatusForbidden, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if review, err = issues_model.GetReviewByID(ctx, review.ID); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } // convert response apiReview, err := convert.ToPullReview(ctx, review, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, apiReview) diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index 3d5e51a2c9..36fff126e1 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -53,7 +53,7 @@ func GetRelease(ctx *context.APIContext) { id := ctx.PathParamInt64("id") release, err := repo_model.GetReleaseForRepoByID(ctx, ctx.Repo.Repository.ID, id) if err != nil && !repo_model.IsErrReleaseNotExist(err) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err != nil && repo_model.IsErrReleaseNotExist(err) || release.IsTag { @@ -62,7 +62,7 @@ func GetRelease(ctx *context.APIContext) { } if err := release.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, release)) @@ -93,7 +93,7 @@ func GetLatestRelease(ctx *context.APIContext) { // "$ref": "#/responses/notFound" release, err := repo_model.GetLatestReleaseByRepoID(ctx, ctx.Repo.Repository.ID) if err != nil && !repo_model.IsErrReleaseNotExist(err) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err != nil && repo_model.IsErrReleaseNotExist(err) || @@ -103,7 +103,7 @@ func GetLatestRelease(ctx *context.APIContext) { } if err := release.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, release)) @@ -161,13 +161,13 @@ func ListReleases(ctx *context.APIContext) { releases, err := db.Find[repo_model.Release](ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } rels := make([]*api.Release, len(releases)) for i, release := range releases { if err := release.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } rels[i] = convert.ToAPIRelease(ctx, ctx.Repo.Repository, release) @@ -226,7 +226,7 @@ func CreateRelease(ctx *context.APIContext) { rel, err := repo_model.GetRelease(ctx, ctx.Repo.Repository.ID, form.TagName) if err != nil { if !repo_model.IsErrReleaseNotExist(err) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } // If target is not provided use default branch @@ -254,7 +254,7 @@ func CreateRelease(ctx *context.APIContext) { } else if git.IsErrNotExist(err) { ctx.APIError(http.StatusNotFound, fmt.Errorf("target \"%v\" not found: %w", rel.Target, err)) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -275,7 +275,7 @@ func CreateRelease(ctx *context.APIContext) { rel.Target = form.Target if err = release_service.UpdateRelease(ctx, ctx.Doer, ctx.Repo.GitRepo, rel, nil, nil, nil); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -322,7 +322,7 @@ func EditRelease(ctx *context.APIContext) { id := ctx.PathParamInt64("id") rel, err := repo_model.GetReleaseForRepoByID(ctx, ctx.Repo.Repository.ID, id) if err != nil && !repo_model.IsErrReleaseNotExist(err) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err != nil && repo_model.IsErrReleaseNotExist(err) || rel.IsTag { @@ -349,18 +349,18 @@ func EditRelease(ctx *context.APIContext) { rel.IsPrerelease = *form.IsPrerelease } if err := release_service.UpdateRelease(ctx, ctx.Doer, ctx.Repo.GitRepo, rel, nil, nil, nil); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } // reload data from database rel, err = repo_model.GetReleaseByID(ctx, id) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err := rel.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, rel)) @@ -399,7 +399,7 @@ func DeleteRelease(ctx *context.APIContext) { id := ctx.PathParamInt64("id") rel, err := repo_model.GetReleaseForRepoByID(ctx, ctx.Repo.Repository.ID, id) if err != nil && !repo_model.IsErrReleaseNotExist(err) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err != nil && repo_model.IsErrReleaseNotExist(err) || rel.IsTag { @@ -411,7 +411,7 @@ func DeleteRelease(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, "user not allowed to delete protected tag") return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) diff --git a/routers/api/v1/repo/release_attachment.go b/routers/api/v1/repo/release_attachment.go index 7dba703cfa..defde81a1d 100644 --- a/routers/api/v1/repo/release_attachment.go +++ b/routers/api/v1/repo/release_attachment.go @@ -26,7 +26,7 @@ func checkReleaseMatchRepo(ctx *context.APIContext, releaseID int64) bool { ctx.APIErrorNotFound() return false } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return false } if release.RepoID != ctx.Repo.Repository.ID { @@ -84,7 +84,7 @@ func GetReleaseAttachment(ctx *context.APIContext) { ctx.APIErrorNotFound() return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if attach.ReleaseID != releaseID { @@ -133,7 +133,7 @@ func ListReleaseAttachments(ctx *context.APIContext) { ctx.APIErrorNotFound() return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if release.RepoID != ctx.Repo.Repository.ID { @@ -141,7 +141,7 @@ func ListReleaseAttachments(ctx *context.APIContext) { return } if err := release.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, release).Attachments) @@ -212,7 +212,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) { if strings.HasPrefix(strings.ToLower(ctx.Req.Header.Get("Content-Type")), "multipart/form-data") { file, header, err := ctx.Req.FormFile("attachment") if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } defer file.Close() @@ -245,7 +245,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) { ctx.APIError(http.StatusBadRequest, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -311,7 +311,7 @@ func EditReleaseAttachment(ctx *context.APIContext) { ctx.APIErrorNotFound() return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if attach.ReleaseID != releaseID { @@ -329,7 +329,7 @@ func EditReleaseAttachment(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, attach) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attach)) @@ -384,7 +384,7 @@ func DeleteReleaseAttachment(ctx *context.APIContext) { ctx.APIErrorNotFound() return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if attach.ReleaseID != releaseID { @@ -395,7 +395,7 @@ func DeleteReleaseAttachment(ctx *context.APIContext) { // FIXME Should prove the existence of the given repo, but results in unnecessary database requests if err := repo_model.DeleteAttachment(ctx, attach, true); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) diff --git a/routers/api/v1/repo/release_tags.go b/routers/api/v1/repo/release_tags.go index 19f8bac9e8..b5e7d83b2a 100644 --- a/routers/api/v1/repo/release_tags.go +++ b/routers/api/v1/repo/release_tags.go @@ -49,7 +49,7 @@ func GetReleaseByTag(ctx *context.APIContext) { ctx.APIErrorNotFound() return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -59,7 +59,7 @@ func GetReleaseByTag(ctx *context.APIContext) { } if err = release.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, release)) @@ -102,7 +102,7 @@ func DeleteReleaseByTag(ctx *context.APIContext) { ctx.APIErrorNotFound() return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -116,7 +116,7 @@ func DeleteReleaseByTag(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, "user not allowed to delete protected tag") return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 72b841ff6b..d418b8e4b5 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -271,7 +271,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre label.IsErrTemplateLoad(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -279,7 +279,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre // reload repo from db to get a real state after creation repo, err = repo_model.GetRepositoryByID(ctx, repo.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } ctx.JSON(http.StatusCreated, convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm.AccessModeOwner})) @@ -395,7 +395,7 @@ func Generate(ctx *context.APIContext) { return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -424,7 +424,7 @@ func Generate(ctx *context.APIContext) { db.IsErrNamePatternNotAllowed(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -500,7 +500,7 @@ func CreateOrgRepo(ctx *context.APIContext) { if organization.IsErrOrgNotExist(err) { ctx.APIError(http.StatusUnprocessableEntity, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -513,7 +513,7 @@ func CreateOrgRepo(ctx *context.APIContext) { if !ctx.Doer.IsAdmin { canCreate, err := org.CanCreateOrgRepo(ctx, ctx.Doer.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if !canCreate { ctx.APIError(http.StatusForbidden, "Given user is not allowed to create repository in organization.") @@ -548,7 +548,7 @@ func Get(ctx *context.APIContext) { // "$ref": "#/responses/notFound" if err := ctx.Repo.Repository.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -580,14 +580,14 @@ func GetByID(ctx *context.APIContext) { if repo_model.IsErrRepoNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } permission, err := access_model.GetUserRepoPermission(ctx, repo, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if !permission.HasAnyUnitAccess() { ctx.APIErrorNotFound() @@ -703,7 +703,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err // Visibility of forked repository is forced sync with base repository. if repo.IsFork { if err := repo.GetBaseRepo(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return err } *opts.Private = repo.BaseRepo.IsPrivate @@ -728,7 +728,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err var err error ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, repo) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return err } } @@ -738,7 +738,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch)) { if !repo.IsEmpty { if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, *opts.DefaultBranch); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return err } updateRepoLicense = true @@ -747,7 +747,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err } if err := repo_service.UpdateRepository(ctx, repo, visibilityChanged); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return err } @@ -755,7 +755,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err if err := repo_service.AddRepoToLicenseUpdaterQueue(&repo_service.LicenseUpdaterOptions{ RepoID: ctx.Repo.Repository.ID, }); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return err } } @@ -1024,7 +1024,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { if len(units)+len(deleteUnitTypes) > 0 { if err := repo_service.UpdateRepositoryUnits(ctx, repo, units, deleteUnitTypes); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return err } } @@ -1046,7 +1046,7 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e if *opts.Archived { if err := repo_model.SetArchiveRepoState(ctx, repo, *opts.Archived); err != nil { log.Error("Tried to archive a repo: %s", err) - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return err } if err := actions_model.CleanRepoScheduleTasks(ctx, repo); err != nil { @@ -1056,7 +1056,7 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e } else { if err := repo_model.SetArchiveRepoState(ctx, repo, *opts.Archived); err != nil { log.Error("Tried to un-archive a repo: %s", err) - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return err } if ctx.Repo.Repository.UnitEnabled(ctx, unit_model.TypeActions) { @@ -1084,7 +1084,7 @@ func updateMirror(ctx *context.APIContext, opts api.EditRepoOption) error { mirror, err := repo_model.GetMirrorByRepoID(ctx, repo.ID) if err != nil { log.Error("Failed to get mirror: %s", err) - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return err } @@ -1158,7 +1158,7 @@ func Delete(ctx *context.APIContext) { canDelete, err := repo_module.CanUserDelete(ctx, repo, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if !canDelete { ctx.APIError(http.StatusForbidden, "Given user is not owner of organization.") @@ -1170,7 +1170,7 @@ func Delete(ctx *context.APIContext) { } if err := repo_service.DeleteRepository(ctx, ctx.Doer, repo, true); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -1315,7 +1315,7 @@ func ListRepoActivityFeeds(ctx *context.APIContext) { feeds, count, err := feed_service.GetFeeds(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.SetTotalCountHeader(count) diff --git a/routers/api/v1/repo/star.go b/routers/api/v1/repo/star.go index 1e1c416e42..46218e0e28 100644 --- a/routers/api/v1/repo/star.go +++ b/routers/api/v1/repo/star.go @@ -49,7 +49,7 @@ func ListStargazers(ctx *context.APIContext) { stargazers, err := repo_model.GetStargazers(ctx, ctx.Repo.Repository, utils.GetListOptions(ctx)) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } users := make([]*api.User, len(stargazers)) diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index e6c7c71782..e1dbb25865 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -65,7 +65,7 @@ func NewCommitStatus(ctx *context.APIContext) { Context: form.Context, } if err := commitstatus_service.CreateCommitStatus(ctx, ctx.Repo.Repository, ctx.Doer, sha, status); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -203,7 +203,7 @@ func getCommitStatuses(ctx *context.APIContext, sha string) { State: ctx.FormTrim("state"), }) if err != nil { - ctx.APIError(http.StatusInternalServerError, fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %w", repo.FullName(), sha, ctx.FormInt("page"), err)) + ctx.APIErrorInternal(fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %w", repo.FullName(), sha, ctx.FormInt("page"), err)) return } @@ -266,7 +266,7 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) { statuses, count, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, utils.GetListOptions(ctx)) if err != nil { - ctx.APIError(http.StatusInternalServerError, fmt.Errorf("GetLatestCommitStatus[%s, %s]: %w", repo.FullName(), sha, err)) + ctx.APIErrorInternal(fmt.Errorf("GetLatestCommitStatus[%s, %s]: %w", repo.FullName(), sha, err)) return } diff --git a/routers/api/v1/repo/subscriber.go b/routers/api/v1/repo/subscriber.go index e02b0c2ab7..14f296a83d 100644 --- a/routers/api/v1/repo/subscriber.go +++ b/routers/api/v1/repo/subscriber.go @@ -47,7 +47,7 @@ func ListSubscribers(ctx *context.APIContext) { subscribers, err := repo_model.GetRepoWatchers(ctx, ctx.Repo.Repository.ID, utils.GetListOptions(ctx)) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } users := make([]*api.User, len(subscribers)) diff --git a/routers/api/v1/repo/tag.go b/routers/api/v1/repo/tag.go index c0b70b92b2..2e6c1c1023 100644 --- a/routers/api/v1/repo/tag.go +++ b/routers/api/v1/repo/tag.go @@ -57,7 +57,7 @@ func ListTags(ctx *context.APIContext) { tags, total, err := ctx.Repo.GitRepo.GetTagInfos(listOpts.Page, listOpts.PageSize) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -270,7 +270,7 @@ func DeleteTag(ctx *context.APIContext) { ctx.APIErrorNotFound() return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -284,7 +284,7 @@ func DeleteTag(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, "user not allowed to delete protected tag") return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -316,7 +316,7 @@ func ListTagProtection(ctx *context.APIContext) { repo := ctx.Repo.Repository pts, err := git_model.GetProtectedTags(ctx, repo.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiPts := make([]*api.TagProtection, len(pts)) @@ -360,7 +360,7 @@ func GetTagProtection(ctx *context.APIContext) { id := ctx.PathParamInt64("id") pt, err := git_model.GetProtectedTagByID(ctx, id) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -424,7 +424,7 @@ func CreateTagProtection(ctx *context.APIContext) { pt, err := git_model.GetProtectedTagByNamePattern(ctx, repo.ID, namePattern) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } else if pt != nil { ctx.APIError(http.StatusForbidden, "Tag protection already exist") @@ -438,7 +438,7 @@ func CreateTagProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -449,7 +449,7 @@ func CreateTagProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -461,18 +461,18 @@ func CreateTagProtection(ctx *context.APIContext) { AllowlistTeamIDs: whitelistTeams, } if err := git_model.InsertProtectedTag(ctx, protectTag); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } pt, err = git_model.GetProtectedTagByID(ctx, protectTag.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if pt == nil || pt.RepoID != repo.ID { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -524,7 +524,7 @@ func EditTagProtection(ctx *context.APIContext) { id := ctx.PathParamInt64("id") pt, err := git_model.GetProtectedTagByID(ctx, id) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -546,7 +546,7 @@ func EditTagProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -560,7 +560,7 @@ func EditTagProtection(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } pt.AllowlistUserIDs = whitelistUsers @@ -568,18 +568,18 @@ func EditTagProtection(ctx *context.APIContext) { err = git_model.UpdateProtectedTag(ctx, pt) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } pt, err = git_model.GetProtectedTagByID(ctx, id) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if pt == nil || pt.RepoID != repo.ID { - ctx.APIError(http.StatusInternalServerError, "New tag protection not found") + ctx.APIErrorInternal(errors.New("new tag protection not found")) return } @@ -619,7 +619,7 @@ func DeleteTagProtection(ctx *context.APIContext) { id := ctx.PathParamInt64("id") pt, err := git_model.GetProtectedTagByID(ctx, id) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -630,7 +630,7 @@ func DeleteTagProtection(ctx *context.APIContext) { err = git_model.DeleteProtectedTag(ctx, pt) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/repo/transfer.go b/routers/api/v1/repo/transfer.go index 0e50f29e15..7b890c9e5c 100644 --- a/routers/api/v1/repo/transfer.go +++ b/routers/api/v1/repo/transfer.go @@ -170,7 +170,7 @@ func AcceptTransfer(ctx *context.APIContext) { case errors.Is(err, util.ErrPermissionDenied): ctx.APIError(http.StatusForbidden, err) default: - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -212,7 +212,7 @@ func RejectTransfer(ctx *context.APIContext) { case errors.Is(err, util.ErrPermissionDenied): ctx.APIError(http.StatusForbidden, err) default: - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index a913225bb6..8d73383f76 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -82,7 +82,7 @@ func NewWikiPage(ctx *context.APIContext) { } else if repo_model.IsErrWikiAlreadyExist(err) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -155,7 +155,7 @@ func EditWikiPage(ctx *context.APIContext) { form.ContentBase64 = string(content) if err := wiki_service.EditWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, oldWikiName, newWikiName, form.ContentBase64, form.Message); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -198,7 +198,7 @@ func getWikiPage(ctx *context.APIContext, wikiName wiki_service.WebPath) *api.Wi // Get last change information. lastCommit, err := wikiRepo.GetCommitByPath(pageFilename) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil } @@ -249,7 +249,7 @@ func DeleteWikiPage(ctx *context.APIContext) { ctx.APIErrorNotFound(err) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -322,7 +322,7 @@ func ListWikiPages(ctx *context.APIContext) { } c, err := wikiRepo.GetCommitByPath(entry.Name()) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } wikiName, err := wiki_service.GitPathToWebPath(entry.Name()) @@ -330,7 +330,7 @@ func ListWikiPages(ctx *context.APIContext) { if repo_model.IsErrWikiInvalidFileName(err) { continue } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } pages = append(pages, wiki_service.ToWikiPageMetaData(wikiName, c, ctx.Repo.Repository)) @@ -447,7 +447,7 @@ func ListPageRevisions(ctx *context.APIContext) { Page: page, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -481,7 +481,7 @@ func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) if git.IsErrNotExist(err) || err.Error() == "no such file or directory" { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil, nil } @@ -491,7 +491,7 @@ func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) if git.IsErrNotExist(err) { ctx.APIErrorNotFound(err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return wikiRepo, nil } @@ -507,7 +507,7 @@ func wikiContentsByEntry(ctx *context.APIContext, entry *git.TreeEntry) string { } content, err := blob.GetBlobContentBase64() if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return "" } return content diff --git a/routers/api/v1/shared/block.go b/routers/api/v1/shared/block.go index 17be2dde4a..b22f8a74fd 100644 --- a/routers/api/v1/shared/block.go +++ b/routers/api/v1/shared/block.go @@ -21,12 +21,12 @@ func ListBlocks(ctx *context.APIContext, blocker *user_model.User) { BlockerID: blocker.ID, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err := user_model.BlockingList(blocks).LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -49,7 +49,7 @@ func CheckUserBlock(ctx *context.APIContext, blocker *user_model.User) { status := http.StatusNotFound blocking, err := user_model.GetBlocking(ctx, blocker.ID, blockee.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if blocking != nil { @@ -70,7 +70,7 @@ func BlockUser(ctx *context.APIContext, blocker *user_model.User) { if errors.Is(err, user_model.ErrCanNotBlock) || errors.Is(err, user_model.ErrBlockOrganization) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -89,7 +89,7 @@ func UnblockUser(ctx *context.APIContext, doer, blocker *user_model.User) { if errors.Is(err, user_model.ErrCanNotUnblock) || errors.Is(err, user_model.ErrBlockOrganization) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/routers/api/v1/user/action.go b/routers/api/v1/user/action.go index cfc206cac7..1c8e3b6e71 100644 --- a/routers/api/v1/user/action.go +++ b/routers/api/v1/user/action.go @@ -56,7 +56,7 @@ func CreateOrUpdateSecret(ctx *context.APIContext) { } else if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -98,7 +98,7 @@ func DeleteSecret(ctx *context.APIContext) { } else if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -145,7 +145,7 @@ func CreateVariable(ctx *context.APIContext) { Name: variableName, }) if err != nil && !errors.Is(err, util.ErrNotExist) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if v != nil && v.ID > 0 { @@ -157,7 +157,7 @@ func CreateVariable(ctx *context.APIContext) { if errors.Is(err, util.ErrInvalidArgument) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -204,7 +204,7 @@ func UpdateVariable(ctx *context.APIContext) { if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -220,7 +220,7 @@ func UpdateVariable(ctx *context.APIContext) { if errors.Is(err, util.ErrInvalidArgument) { ctx.APIError(http.StatusBadRequest, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -257,7 +257,7 @@ func DeleteVariable(ctx *context.APIContext) { } else if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -294,7 +294,7 @@ func GetVariable(ctx *context.APIContext) { if errors.Is(err, util.ErrNotExist) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -338,7 +338,7 @@ func ListVariables(ctx *context.APIContext) { ListOptions: utils.GetListOptions(ctx), }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/user/app.go b/routers/api/v1/user/app.go index cc1ed8b95f..4ca06ca923 100644 --- a/routers/api/v1/user/app.go +++ b/routers/api/v1/user/app.go @@ -125,7 +125,7 @@ func CreateAccessToken(ctx *context.APIContext) { t.Scope = scope if err := auth_model.NewAccessToken(ctx, t); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusCreated, &api.AccessToken{ @@ -174,7 +174,7 @@ func DeleteAccessToken(ctx *context.APIContext) { UserID: ctx.ContextUser.ID, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -190,7 +190,7 @@ func DeleteAccessToken(ctx *context.APIContext) { } } if tokenID == 0 { - ctx.APIError(http.StatusInternalServerError, nil) + ctx.APIErrorInternal(nil) return } @@ -198,7 +198,7 @@ func DeleteAccessToken(ctx *context.APIContext) { if auth_model.IsErrAccessTokenNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -273,7 +273,7 @@ func ListOauth2Applications(ctx *context.APIContext) { OwnerID: ctx.Doer.ID, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -311,7 +311,7 @@ func DeleteOauth2Application(ctx *context.APIContext) { if auth_model.IsErrOAuthApplicationNotFound(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -344,7 +344,7 @@ func GetOauth2Application(ctx *context.APIContext) { if auth_model.IsErrOauthClientIDInvalid(err) || auth_model.IsErrOAuthApplicationNotFound(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -398,7 +398,7 @@ func UpdateOauth2Application(ctx *context.APIContext) { if auth_model.IsErrOauthClientIDInvalid(err) || auth_model.IsErrOAuthApplicationNotFound(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/routers/api/v1/user/avatar.go b/routers/api/v1/user/avatar.go index 028b5e8288..9c7bd57bc0 100644 --- a/routers/api/v1/user/avatar.go +++ b/routers/api/v1/user/avatar.go @@ -38,7 +38,7 @@ func UpdateAvatar(ctx *context.APIContext) { err = user_service.UploadAvatar(ctx, ctx.Doer, content) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -57,7 +57,7 @@ func DeleteAvatar(ctx *context.APIContext) { // "$ref": "#/responses/empty" err := user_service.DeleteAvatar(ctx, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } diff --git a/routers/api/v1/user/email.go b/routers/api/v1/user/email.go index b488e457a1..055e5ea419 100644 --- a/routers/api/v1/user/email.go +++ b/routers/api/v1/user/email.go @@ -29,7 +29,7 @@ func ListEmails(ctx *context.APIContext) { emails, err := user_model.GetEmailAddresses(ctx, ctx.Doer.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } apiEmails := make([]*api.Email, len(emails)) @@ -78,14 +78,14 @@ func AddEmail(ctx *context.APIContext) { errMsg := fmt.Sprintf("Email address %q invalid", email) ctx.APIError(http.StatusUnprocessableEntity, errMsg) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } emails, err := user_model.GetEmailAddresses(ctx, ctx.Doer.ID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -124,7 +124,7 @@ func DeleteEmail(ctx *context.APIContext) { if user_model.IsErrEmailAddressNotExist(err) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/routers/api/v1/user/follower.go b/routers/api/v1/user/follower.go index 83a852062b..0d0c0be7e0 100644 --- a/routers/api/v1/user/follower.go +++ b/routers/api/v1/user/follower.go @@ -26,7 +26,7 @@ func responseAPIUsers(ctx *context.APIContext, users []*user_model.User) { func listUserFollowers(ctx *context.APIContext, u *user_model.User) { users, count, err := user_model.GetUserFollowers(ctx, u, ctx.Doer, utils.GetListOptions(ctx)) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -90,7 +90,7 @@ func ListFollowers(ctx *context.APIContext) { func listUserFollowing(ctx *context.APIContext, u *user_model.User) { users, count, err := user_model.GetUserFollowing(ctx, u, ctx.Doer, utils.GetListOptions(ctx)) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -231,7 +231,7 @@ func Follow(ctx *context.APIContext) { if errors.Is(err, user_model.ErrBlockedUser) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -256,7 +256,7 @@ func Unfollow(ctx *context.APIContext) { // "$ref": "#/responses/notFound" if err := user_model.UnfollowUser(ctx, ctx.Doer.ID, ctx.ContextUser.ID); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) diff --git a/routers/api/v1/user/gpg_key.go b/routers/api/v1/user/gpg_key.go index f8b5cf08a3..504e74ae10 100644 --- a/routers/api/v1/user/gpg_key.go +++ b/routers/api/v1/user/gpg_key.go @@ -25,12 +25,12 @@ func listGPGKeys(ctx *context.APIContext, uid int64, listOptions db.ListOptions) OwnerID: uid, }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err := asymkey_model.GPGKeyList(keys).LoadSubKeys(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -121,12 +121,12 @@ func GetGPGKey(ctx *context.APIContext) { if asymkey_model.IsErrGPGKeyNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } if err := key.LoadSubKeys(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, convert.ToGPGKey(key)) @@ -208,7 +208,7 @@ func VerifyUserGPGKey(ctx *context.APIContext) { ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The provided GPG key, signature and token do not match or token is out of date. Provide a valid signature for the token: %s", token)) return } - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } keys, err := db.Find[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{ @@ -219,7 +219,7 @@ func VerifyUserGPGKey(ctx *context.APIContext) { if asymkey_model.IsErrGPGKeyNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -284,7 +284,7 @@ func DeleteGPGKey(ctx *context.APIContext) { if asymkey_model.IsErrGPGKeyAccessDenied(err) { ctx.APIError(http.StatusForbidden, "You do not have access to this key") } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -306,6 +306,6 @@ func HandleAddGPGKeyError(ctx *context.APIContext, err error, token string) { case asymkey_model.IsErrGPGInvalidTokenSignature(err): ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The provided GPG key, signature and token do not match or token is out of date. Provide a valid signature for the token: %s", token)) default: - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } } diff --git a/routers/api/v1/user/helper.go b/routers/api/v1/user/helper.go index 9ceb0c255b..f49bbbd6db 100644 --- a/routers/api/v1/user/helper.go +++ b/routers/api/v1/user/helper.go @@ -4,8 +4,6 @@ package user import ( - "net/http" - user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/services/context" ) @@ -23,7 +21,7 @@ func GetUserByPathParam(ctx *context.APIContext, name string) *user_model.User { ctx.APIErrorNotFound("GetUserByName", err) } } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil } diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go index dafd696954..6295f4753b 100644 --- a/routers/api/v1/user/key.go +++ b/routers/api/v1/user/key.go @@ -81,7 +81,7 @@ func listPublicKeys(ctx *context.APIContext, user *user_model.User) { } if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -184,7 +184,7 @@ func GetPublicKey(ctx *context.APIContext) { if asymkey_model.IsErrKeyNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -280,7 +280,7 @@ func DeletePublicKey(ctx *context.APIContext) { if asymkey_model.IsErrKeyNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -294,7 +294,7 @@ func DeletePublicKey(ctx *context.APIContext) { if asymkey_model.IsErrKeyAccessDenied(err) { ctx.APIError(http.StatusForbidden, "You do not have access to this key") } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index f57e23d679..6aabc4fb90 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -26,12 +26,12 @@ func listUserRepos(ctx *context.APIContext, u *user_model.User, private bool) { OrderBy: "id ASC", }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if err := repos.LoadAttributes(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -39,7 +39,7 @@ func listUserRepos(ctx *context.APIContext, u *user_model.User, private bool) { for i := range repos { permission, err := access_model.GetUserRepoPermission(ctx, repos[i], ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if ctx.IsSigned && ctx.Doer.IsAdmin || permission.HasAnyUnitAccess() { @@ -113,19 +113,19 @@ func ListMyRepos(ctx *context.APIContext) { repos, count, err := repo_model.SearchRepository(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } results := make([]*api.Repository, len(repos)) for i, repo := range repos { if err = repo.LoadOwner(ctx); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } permission, err := access_model.GetUserRepoPermission(ctx, repo, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } results[i] = convert.ToRepo(ctx, repo, permission) } diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go index 1fab8a09bb..4b0cb45d67 100644 --- a/routers/api/v1/user/star.go +++ b/routers/api/v1/user/star.go @@ -72,7 +72,7 @@ func GetStarredRepos(ctx *context.APIContext) { private := ctx.ContextUser.ID == ctx.Doer.ID repos, err := getStarredRepos(ctx, ctx.ContextUser, private) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } @@ -104,7 +104,7 @@ func GetMyStarredRepos(ctx *context.APIContext) { repos, err := getStarredRepos(ctx, ctx.Doer, true) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } ctx.SetTotalCountHeader(int64(ctx.Doer.NumStars)) @@ -171,7 +171,7 @@ func Star(ctx *context.APIContext) { if errors.Is(err, user_model.ErrBlockedUser) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -204,7 +204,7 @@ func Unstar(ctx *context.APIContext) { err := repo_model.StarRepo(ctx, ctx.Doer, ctx.Repo.Repository, false) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index 0542bf21cf..757a548518 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -162,7 +162,7 @@ func GetUserHeatmapData(ctx *context.APIContext) { heatmap, err := activities_model.GetUserHeatmapDataByUser(ctx, ctx.ContextUser, ctx.Doer) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, heatmap) @@ -217,7 +217,7 @@ func ListUserActivityFeeds(ctx *context.APIContext) { feeds, count, err := feed_service.GetFeeds(ctx, opts) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.SetTotalCountHeader(count) diff --git a/routers/api/v1/user/watch.go b/routers/api/v1/user/watch.go index d803efc387..76d7c81793 100644 --- a/routers/api/v1/user/watch.go +++ b/routers/api/v1/user/watch.go @@ -68,7 +68,7 @@ func GetWatchedRepos(ctx *context.APIContext) { private := ctx.ContextUser.ID == ctx.Doer.ID repos, total, err := getWatchedRepos(ctx, ctx.ContextUser, private) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } ctx.SetTotalCountHeader(total) @@ -97,7 +97,7 @@ func GetMyWatchedRepos(ctx *context.APIContext) { repos, total, err := getWatchedRepos(ctx, ctx.Doer, true) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } ctx.SetTotalCountHeader(total) @@ -170,7 +170,7 @@ func Watch(ctx *context.APIContext) { if errors.Is(err, user_model.ErrBlockedUser) { ctx.APIError(http.StatusForbidden, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } @@ -208,7 +208,7 @@ func Unwatch(ctx *context.APIContext) { err := repo_model.WatchRepo(ctx, ctx.Doer, ctx.Repo.Repository, false) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.Status(http.StatusNoContent) diff --git a/routers/api/v1/utils/git.go b/routers/api/v1/utils/git.go index 33aa3b81d6..af672ba147 100644 --- a/routers/api/v1/utils/git.go +++ b/routers/api/v1/utils/git.go @@ -26,7 +26,7 @@ func ResolveRefOrSha(ctx *context.APIContext, ref string) string { for _, refType := range []string{"heads", "tags"} { refSHA, lastMethodName, err := searchRefCommitByType(ctx, refType, ref) if err != nil { - ctx.APIError(http.StatusInternalServerError, fmt.Errorf("%s: %w", lastMethodName, err)) + ctx.APIErrorInternal(fmt.Errorf("%s: %w", lastMethodName, err)) return "" } if refSHA != "" { diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index c6154dd133..9c49819970 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -54,7 +54,7 @@ func GetOwnerHook(ctx *context.APIContext, ownerID, hookID int64) (*webhook.Webh if webhook.IsErrWebhookNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil, err } @@ -69,7 +69,7 @@ func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*webhook.Webhoo if webhook.IsErrWebhookNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return nil, err } @@ -102,7 +102,7 @@ func AddSystemHook(ctx *context.APIContext, form *api.CreateHookOption) { if ok { h, err := webhook_service.ToHook(setting.AppSubURL+"/-/admin", hook) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusCreated, h) @@ -141,7 +141,7 @@ func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) { func toAPIHook(ctx *context.APIContext, repoLink string, hook *webhook.Webhook) (*api.Hook, bool) { apiHook, err := webhook_service.ToHook(repoLink, hook) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, false } return apiHook, true @@ -215,7 +215,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI } err := w.SetHeaderAuthorization(form.AuthorizationHeader) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, false } if w.Type == webhook_module.SLACK { @@ -238,17 +238,17 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI Color: form.Config["color"], }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, false } w.Meta = string(meta) } if err := w.UpdateEvent(); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, false } else if err := webhook.CreateWebhook(ctx, w); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return nil, false } return w, true @@ -258,21 +258,21 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI func EditSystemHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) { hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } if !editHook(ctx, form, hook) { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } updated, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } h, err := webhook_service.ToHook(setting.AppURL+"/-/admin", updated) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } ctx.JSON(http.StatusOK, h) @@ -343,7 +343,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh Color: form.Config["color"], }) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return false } w.Meta = string(meta) @@ -369,7 +369,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh err := w.SetHeaderAuthorization(form.AuthorizationHeader) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return false } @@ -391,7 +391,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh w.HookEvents[webhook_module.HookEventPullRequestSync] = pullHook(form.Events, string(webhook_module.HookEventPullRequestSync)) if err := w.UpdateEvent(); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return false } @@ -400,7 +400,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh } if err := webhook.UpdateWebhook(ctx, w); err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return false } return true @@ -412,7 +412,7 @@ func DeleteOwnerHook(ctx *context.APIContext, owner *user_model.User, hookID int if webhook.IsErrWebhookNotExist(err) { ctx.APIErrorNotFound() } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } return } diff --git a/services/actions/workflow.go b/services/actions/workflow.go index ab320aa453..9aecad171b 100644 --- a/services/actions/workflow.go +++ b/services/actions/workflow.go @@ -104,7 +104,7 @@ func EnableOrDisableWorkflow(ctx *context.APIContext, workflowID string, isEnabl func ListActionWorkflows(ctx *context.APIContext) ([]*api.ActionWorkflow, error) { defaultBranchCommit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch) if err != nil { - ctx.APIError(http.StatusInternalServerError, err.Error()) + ctx.APIErrorInternal(err) return nil, err } diff --git a/services/context/api.go b/services/context/api.go index 9c3ee2a4ac..230c3456d1 100644 --- a/services/context/api.go +++ b/services/context/api.go @@ -5,6 +5,7 @@ package context import ( + "errors" "fmt" "net/http" "net/url" @@ -17,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" web_types "code.gitea.io/gitea/modules/web/types" ) @@ -108,7 +110,11 @@ type APIRepoArchivedError struct { // APIErrorInternal responds with error message, status is 500 func (ctx *APIContext) APIErrorInternal(err error) { - log.ErrorWithSkip(1, "InternalServerError: %v", err) + ctx.apiErrorInternal(1, err) +} + +func (ctx *APIContext) apiErrorInternal(skip int, err error) { + log.ErrorWithSkip(skip+1, "InternalServerError: %v", err) var message string if !setting.IsProd || (ctx.Doer != nil && ctx.Doer.IsAdmin) { @@ -273,7 +279,7 @@ func ReferencesGitRepo(allowEmpty ...bool) func(ctx *APIContext) { var err error ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -344,12 +350,12 @@ func (ctx *APIContext) GetErrMsg() string { // NotFoundOrServerError use error check function to determine if the error // is about not found. It responds with 404 status code for not found error, // or error context description for logging purpose of 500 server error. -func (ctx *APIContext) NotFoundOrServerError(logMsg string, errCheck func(error) bool, logErr error) { - if errCheck(logErr) { +func (ctx *APIContext) NotFoundOrServerError(err error) { + if errors.Is(err, util.ErrNotExist) { ctx.JSON(http.StatusNotFound, nil) return } - ctx.APIError(http.StatusInternalServerError, logMsg) + ctx.APIErrorInternal(err) } // IsUserSiteAdmin returns true if current user is a site admin diff --git a/services/context/user.go b/services/context/user.go index 4037354955..c09ded8339 100644 --- a/services/context/user.go +++ b/services/context/user.go @@ -44,7 +44,7 @@ func UserIDAssignmentAPI() func(ctx *APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } } }