mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-21 12:44:49 -05:00
Improve org error handling (#2117)
* Improve ErrOrgNotExist type Return new error type Use good error check Use new method to check error Update tests * Fix unchanged method name report
This commit is contained in:
parent
2ef33b5338
commit
30787e48f2
7 changed files with 26 additions and 12 deletions
|
@ -448,6 +448,22 @@ func (err ErrAccessTokenEmpty) Error() string {
|
||||||
// \_______ /__| \___ (____ /___| /__/_____ \(____ /__| |__|\____/|___| /
|
// \_______ /__| \___ (____ /___| /__/_____ \(____ /__| |__|\____/|___| /
|
||||||
// \/ /_____/ \/ \/ \/ \/ \/
|
// \/ /_____/ \/ \/ \/ \/ \/
|
||||||
|
|
||||||
|
// ErrOrgNotExist represents a "OrgNotExist" kind of error.
|
||||||
|
type ErrOrgNotExist struct {
|
||||||
|
ID int64
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsErrOrgNotExist checks if an error is a ErrOrgNotExist.
|
||||||
|
func IsErrOrgNotExist(err error) bool {
|
||||||
|
_, ok := err.(ErrOrgNotExist)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err ErrOrgNotExist) Error() string {
|
||||||
|
return fmt.Sprintf("org does not exist [id: %d, name: %s]", err.ID, err.Name)
|
||||||
|
}
|
||||||
|
|
||||||
// ErrLastOrgOwner represents a "LastOrgOwner" kind of error.
|
// ErrLastOrgOwner represents a "LastOrgOwner" kind of error.
|
||||||
type ErrLastOrgOwner struct {
|
type ErrLastOrgOwner struct {
|
||||||
UID int64
|
UID int64
|
||||||
|
|
|
@ -16,8 +16,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrOrgNotExist organization does not exist
|
|
||||||
ErrOrgNotExist = errors.New("Organization does not exist")
|
|
||||||
// ErrTeamNotExist team does not exist
|
// ErrTeamNotExist team does not exist
|
||||||
ErrTeamNotExist = errors.New("Team does not exist")
|
ErrTeamNotExist = errors.New("Team does not exist")
|
||||||
)
|
)
|
||||||
|
@ -180,7 +178,7 @@ func CreateOrganization(org, owner *User) (err error) {
|
||||||
// GetOrgByName returns organization by given name.
|
// GetOrgByName returns organization by given name.
|
||||||
func GetOrgByName(name string) (*User, error) {
|
func GetOrgByName(name string) (*User, error) {
|
||||||
if len(name) == 0 {
|
if len(name) == 0 {
|
||||||
return nil, ErrOrgNotExist
|
return nil, ErrOrgNotExist{0, name}
|
||||||
}
|
}
|
||||||
u := &User{
|
u := &User{
|
||||||
LowerName: strings.ToLower(name),
|
LowerName: strings.ToLower(name),
|
||||||
|
@ -190,7 +188,7 @@ func GetOrgByName(name string) (*User, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if !has {
|
} else if !has {
|
||||||
return nil, ErrOrgNotExist
|
return nil, ErrOrgNotExist{0, name}
|
||||||
}
|
}
|
||||||
return u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -230,7 +230,7 @@ func NewTeam(t *Team) (err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if !has {
|
} else if !has {
|
||||||
return ErrOrgNotExist
|
return ErrOrgNotExist{t.OrgID, ""}
|
||||||
}
|
}
|
||||||
|
|
||||||
t.LowerName = strings.ToLower(t.Name)
|
t.LowerName = strings.ToLower(t.Name)
|
||||||
|
|
|
@ -222,10 +222,10 @@ func TestGetOrgByName(t *testing.T) {
|
||||||
assert.Equal(t, "user3", org.Name)
|
assert.Equal(t, "user3", org.Name)
|
||||||
|
|
||||||
org, err = GetOrgByName("user2") // user2 is an individual
|
org, err = GetOrgByName("user2") // user2 is an individual
|
||||||
assert.Equal(t, ErrOrgNotExist, err)
|
assert.True(t, IsErrOrgNotExist(err))
|
||||||
|
|
||||||
org, err = GetOrgByName("") // corner case
|
org, err = GetOrgByName("") // corner case
|
||||||
assert.Equal(t, ErrOrgNotExist, err)
|
assert.True(t, IsErrOrgNotExist(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCountOrganizations(t *testing.T) {
|
func TestCountOrganizations(t *testing.T) {
|
||||||
|
|
|
@ -208,12 +208,12 @@ func orgAssignment(args ...bool) macaron.Handler {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if assignOrg {
|
if assignOrg {
|
||||||
ctx.Org.Organization, err = models.GetUserByName(ctx.Params(":orgname"))
|
ctx.Org.Organization, err = models.GetOrgByName(ctx.Params(":orgname"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if models.IsErrUserNotExist(err) {
|
if models.IsErrOrgNotExist(err) {
|
||||||
ctx.Status(404)
|
ctx.Status(404)
|
||||||
} else {
|
} else {
|
||||||
ctx.Error(500, "GetUserByName", err)
|
ctx.Error(500, "GetOrgByName", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,7 @@ func CreateFork(ctx *context.APIContext, form api.CreateForkOption) {
|
||||||
} else {
|
} else {
|
||||||
org, err := models.GetOrgByName(*form.Organization)
|
org, err := models.GetOrgByName(*form.Organization)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == models.ErrOrgNotExist {
|
if models.IsErrOrgNotExist(err) {
|
||||||
ctx.Error(422, "", err)
|
ctx.Error(422, "", err)
|
||||||
} else {
|
} else {
|
||||||
ctx.Error(500, "GetOrgByName", err)
|
ctx.Error(500, "GetOrgByName", err)
|
||||||
|
|
|
@ -156,7 +156,7 @@ func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) {
|
||||||
|
|
||||||
org, err := models.GetOrgByName(ctx.Params(":org"))
|
org, err := models.GetOrgByName(ctx.Params(":org"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if models.IsErrUserNotExist(err) {
|
if models.IsErrOrgNotExist(err) {
|
||||||
ctx.Error(422, "", err)
|
ctx.Error(422, "", err)
|
||||||
} else {
|
} else {
|
||||||
ctx.Error(500, "GetOrgByName", err)
|
ctx.Error(500, "GetOrgByName", err)
|
||||||
|
|
Loading…
Reference in a new issue