#2497 incorrect error handle for team name
This commit is contained in:
parent
ee814bf8d6
commit
112a7cab31
9 changed files with 65 additions and 44 deletions
|
@ -559,5 +559,26 @@ func IsErrAuthenticationNotExist(err error) bool {
|
|||
}
|
||||
|
||||
func (err ErrAuthenticationNotExist) Error() string {
|
||||
return fmt.Sprintf("Authentication does not exist [id: %d]", err.ID)
|
||||
return fmt.Sprintf("authentication does not exist [id: %d]", err.ID)
|
||||
}
|
||||
|
||||
// ___________
|
||||
// \__ ___/___ _____ _____
|
||||
// | |_/ __ \\__ \ / \
|
||||
// | |\ ___/ / __ \| Y Y \
|
||||
// |____| \___ >____ /__|_| /
|
||||
// \/ \/ \/
|
||||
|
||||
type ErrTeamAlreadyExist struct {
|
||||
OrgID int64
|
||||
Name string
|
||||
}
|
||||
|
||||
func IsErrTeamAlreadyExist(err error) bool {
|
||||
_, ok := err.(ErrTeamAlreadyExist)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrTeamAlreadyExist) Error() string {
|
||||
return fmt.Sprintf("team already exists [org_id: %d, name: %s]", err.OrgID, err.Name)
|
||||
}
|
||||
|
|
|
@ -14,10 +14,8 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
ErrOrgNotExist = errors.New("Organization does not exist")
|
||||
ErrTeamAlreadyExist = errors.New("Team already exist")
|
||||
ErrTeamNotExist = errors.New("Team does not exist")
|
||||
ErrTeamNameIllegal = errors.New("Team name contains illegal characters")
|
||||
ErrOrgNotExist = errors.New("Organization does not exist")
|
||||
ErrTeamNotExist = errors.New("Team does not exist")
|
||||
)
|
||||
|
||||
// IsOwnedBy returns true if given user is in the owner team.
|
||||
|
@ -598,9 +596,9 @@ func (t *Team) RemoveRepository(repoID int64) error {
|
|||
|
||||
// NewTeam creates a record of new team.
|
||||
// It's caller's responsibility to assign organization ID.
|
||||
func NewTeam(t *Team) (err error) {
|
||||
if err = IsUsableName(t.Name); err != nil {
|
||||
return err
|
||||
func NewTeam(t *Team) error {
|
||||
if len(t.Name) == 0 {
|
||||
return errors.New("empty team name")
|
||||
}
|
||||
|
||||
has, err := x.Id(t.OrgID).Get(new(User))
|
||||
|
@ -615,7 +613,7 @@ func NewTeam(t *Team) (err error) {
|
|||
if err != nil {
|
||||
return err
|
||||
} else if has {
|
||||
return ErrTeamAlreadyExist
|
||||
return ErrTeamAlreadyExist{t.OrgID, t.LowerName}
|
||||
}
|
||||
|
||||
sess := x.NewSession()
|
||||
|
@ -674,8 +672,8 @@ func GetTeamById(teamId int64) (*Team, error) {
|
|||
|
||||
// UpdateTeam updates information of team.
|
||||
func UpdateTeam(t *Team, authChanged bool) (err error) {
|
||||
if err = IsUsableName(t.Name); err != nil {
|
||||
return err
|
||||
if len(t.Name) == 0 {
|
||||
return errors.New("empty team name")
|
||||
}
|
||||
|
||||
if len(t.Description) > 255 {
|
||||
|
@ -689,6 +687,13 @@ func UpdateTeam(t *Team, authChanged bool) (err error) {
|
|||
}
|
||||
|
||||
t.LowerName = strings.ToLower(t.Name)
|
||||
has, err := x.Where("org_id=?", t.OrgID).And("lower_name=?", t.LowerName).And("id!=?", t.ID).Get(new(Team))
|
||||
if err != nil {
|
||||
return err
|
||||
} else if has {
|
||||
return ErrTeamAlreadyExist{t.OrgID, t.LowerName}
|
||||
}
|
||||
|
||||
if _, err = sess.Id(t.ID).AllCols().Update(t); err != nil {
|
||||
return fmt.Errorf("update: %v", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue