1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 10:18:38 +00:00

Refactor error system (#33610)

This commit is contained in:
wxiaoguang
2025-02-17 14:13:17 +08:00
committed by GitHub
parent 69de5a65c2
commit f35850f48e
184 changed files with 2100 additions and 2106 deletions

View File

@@ -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.Error(http.StatusInternalServerError, "db.FindAndCount[organization.Organization]", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
@@ -138,14 +138,14 @@ func GetUserOrgsPermissions(ctx *context.APIContext) {
op := api.OrganizationPermissions{}
if !organization.HasOrgOrUserVisible(ctx, o, ctx.ContextUser) {
ctx.NotFound("HasOrgOrUserVisible", nil)
ctx.APIErrorNotFound("HasOrgOrUserVisible", nil)
return
}
org := organization.OrgFromUser(o)
authorizeLevel, err := org.GetOrgUserMaxAuthorizeLevel(ctx, ctx.ContextUser.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetOrgUserAuthorizeLevel", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
@@ -164,7 +164,7 @@ func GetUserOrgsPermissions(ctx *context.APIContext) {
op.CanCreateRepository, err = org.CanCreateOrgRepo(ctx, ctx.ContextUser.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "CanCreateOrgRepo", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
@@ -209,7 +209,7 @@ func GetAll(ctx *context.APIContext) {
Visible: vMode,
})
if err != nil {
ctx.Error(http.StatusInternalServerError, "SearchOrganizations", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
orgs := make([]*api.Organization, len(publicOrgs))
@@ -245,7 +245,7 @@ func Create(ctx *context.APIContext) {
// "$ref": "#/responses/validationError"
form := web.GetForm(ctx).(*api.CreateOrgOption)
if !ctx.Doer.CanCreateOrganization() {
ctx.Error(http.StatusForbidden, "Create organization not allowed", nil)
ctx.APIError(http.StatusForbidden, nil)
return
}
@@ -271,9 +271,9 @@ func Create(ctx *context.APIContext) {
db.IsErrNameReserved(err) ||
db.IsErrNameCharsNotAllowed(err) ||
db.IsErrNamePatternNotAllowed(err) {
ctx.Error(http.StatusUnprocessableEntity, "", err)
ctx.APIError(http.StatusUnprocessableEntity, err)
} else {
ctx.Error(http.StatusInternalServerError, "CreateOrganization", err)
ctx.APIError(http.StatusInternalServerError, err)
}
return
}
@@ -301,7 +301,7 @@ func Get(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
if !organization.HasOrgOrUserVisible(ctx, ctx.Org.Organization.AsUser(), ctx.Doer) {
ctx.NotFound("HasOrgOrUserVisible", nil)
ctx.APIErrorNotFound("HasOrgOrUserVisible", nil)
return
}
@@ -344,9 +344,9 @@ func Rename(ctx *context.APIContext) {
orgUser := ctx.Org.Organization.AsUser()
if err := user_service.RenameUser(ctx, orgUser, form.NewName); err != nil {
if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNamePatternNotAllowed(err) || db.IsErrNameCharsNotAllowed(err) {
ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", err)
ctx.APIError(http.StatusUnprocessableEntity, err)
} else {
ctx.ServerError("RenameOrg", 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.Error(http.StatusInternalServerError, "ReplacePrimaryEmailAddress", err)
ctx.APIError(http.StatusInternalServerError, 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.Error(http.StatusInternalServerError, "UpdateUser", err)
ctx.APIError(http.StatusInternalServerError, 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.Error(http.StatusInternalServerError, "DeleteOrganization", err)
ctx.APIError(http.StatusInternalServerError, 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.Error(http.StatusInternalServerError, "IsOrgMember", err)
ctx.APIError(http.StatusInternalServerError, 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.Error(http.StatusInternalServerError, "GetFeeds", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
ctx.SetTotalCountHeader(count)