Skip to content

Commit fde7447

Browse files
committed
fix err handling
1 parent 488ef75 commit fde7447

File tree

2 files changed

+10
-29
lines changed

2 files changed

+10
-29
lines changed

routers/api/v1/admin/user.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -477,26 +477,16 @@ func RenameUser(ctx *context.APIContext) {
477477
return
478478
}
479479

480-
oldName := ctx.ContextUser.Name
481480
newName := web.GetForm(ctx).(*api.RenameUserOption).NewName
482481

483-
// Check if user name has been changed
482+
// Check if username has been changed
484483
if err := user_service.RenameUser(ctx, ctx.ContextUser, newName); err != nil {
485-
switch {
486-
case user_model.IsErrUserAlreadyExist(err):
487-
ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("form.username_been_taken"))
488-
case db.IsErrNameReserved(err):
489-
ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("user.form.name_reserved", newName))
490-
case db.IsErrNamePatternNotAllowed(err):
491-
ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("user.form.name_pattern_not_allowed", newName))
492-
case db.IsErrNameCharsNotAllowed(err):
493-
ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("user.form.name_chars_not_allowed", newName))
494-
default:
484+
if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNamePatternNotAllowed(err) || db.IsErrNameCharsNotAllowed(err) {
485+
ctx.Error(http.StatusUnprocessableEntity, "", err)
486+
} else {
495487
ctx.ServerError("ChangeUserName", err)
496488
}
497489
return
498490
}
499-
500-
log.Trace("User name changed: %s -> %s", oldName, newName)
501491
ctx.Status(http.StatusNoContent)
502492
}

routers/api/v1/org/org.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"code.gitea.io/gitea/models/organization"
1313
"code.gitea.io/gitea/models/perm"
1414
user_model "code.gitea.io/gitea/models/user"
15-
"code.gitea.io/gitea/modules/log"
1615
"code.gitea.io/gitea/modules/optional"
1716
api "code.gitea.io/gitea/modules/structs"
1817
"code.gitea.io/gitea/modules/web"
@@ -341,25 +340,17 @@ func Rename(ctx *context.APIContext) {
341340
// "422":
342341
// "$ref": "#/responses/validationError"
343342

344-
org := ctx.Org.Organization
345343
form := web.GetForm(ctx).(*api.RenameOrgOption)
346-
347-
oldName := org.AsUser().Name
348-
349-
if err := user_service.RenameUser(ctx, org.AsUser(), form.NewName); err != nil {
350-
if user_model.IsErrUserAlreadyExist(err) {
351-
ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", ctx.Tr("form.username_been_taken"))
352-
} else if db.IsErrNameReserved(err) {
353-
ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name))
354-
} else if db.IsErrNamePatternNotAllowed(err) {
355-
ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern))
344+
orgUser := ctx.Org.Organization.AsUser()
345+
if err := user_service.RenameUser(ctx, orgUser, form.NewName); err != nil {
346+
if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNamePatternNotAllowed(err) || db.IsErrNameCharsNotAllowed(err) {
347+
ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", err)
356348
} else {
357349
ctx.ServerError("RenameOrg", err)
358350
}
359-
} else {
360-
log.Info("Org name changed: %s -> %s", oldName, form.NewName)
361-
ctx.Status(http.StatusNoContent)
351+
return
362352
}
353+
ctx.Status(http.StatusNoContent)
363354
}
364355

365356
// Edit change an organization's information

0 commit comments

Comments
 (0)