Browse Source

models: move ErrBranchNotExist to errors package

pull/5126/head
Unknwon 7 years ago
parent
commit
1f7983059a
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 3
      cmd/hook.go
  2. 20
      models/error.go
  3. 13
      models/errors/repo.go
  4. 5
      models/repo_branch.go
  5. 4
      routes/api/v1/repo/branch.go
  6. 4
      routes/repo/setting.go

3
cmd/hook.go

@ -22,6 +22,7 @@ import (
"github.com/gogits/git-module" "github.com/gogits/git-module"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/models/errors"
"github.com/gogits/gogs/pkg/httplib" "github.com/gogits/gogs/pkg/httplib"
"github.com/gogits/gogs/pkg/mailer" "github.com/gogits/gogs/pkg/mailer"
"github.com/gogits/gogs/pkg/setting" "github.com/gogits/gogs/pkg/setting"
@ -94,7 +95,7 @@ func runHookPreReceive(c *cli.Context) error {
repoID := com.StrTo(os.Getenv(http.ENV_REPO_ID)).MustInt64() repoID := com.StrTo(os.Getenv(http.ENV_REPO_ID)).MustInt64()
protectBranch, err := models.GetProtectBranchOfRepoByName(repoID, branchName) protectBranch, err := models.GetProtectBranchOfRepoByName(repoID, branchName)
if err != nil { if err != nil {
if models.IsErrBranchNotExist(err) { if errors.IsErrBranchNotExist(err) {
continue continue
} }
fail("Internal error", "GetProtectBranchOfRepoByName [repo_id: %d, branch: %s]: %v", repoID, branchName, err) fail("Internal error", "GetProtectBranchOfRepoByName [repo_id: %d, branch: %s]: %v", repoID, branchName, err)

20
models/error.go

@ -388,26 +388,6 @@ func (err ErrRepoFileAlreadyExist) Error() string {
return fmt.Sprintf("repository file already exists [file_name: %s]", err.FileName) return fmt.Sprintf("repository file already exists [file_name: %s]", err.FileName)
} }
// __________ .__
// \______ \____________ ____ ____ | |__
// | | _/\_ __ \__ \ / \_/ ___\| | \
// | | \ | | \// __ \| | \ \___| Y \
// |______ / |__| (____ /___| /\___ >___| /
// \/ \/ \/ \/ \/
type ErrBranchNotExist struct {
Name string
}
func IsErrBranchNotExist(err error) bool {
_, ok := err.(ErrBranchNotExist)
return ok
}
func (err ErrBranchNotExist) Error() string {
return fmt.Sprintf("branch does not exist [name: %s]", err.Name)
}
// __________ .__ .__ __________ __ // __________ .__ .__ __________ __
// \______ \__ __| | | |\______ \ ____ ________ __ ____ _______/ |_ // \______ \__ __| | | |\______ \ ____ ________ __ ____ _______/ |_
// | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\ // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\

13
models/errors/repo.go

@ -72,3 +72,16 @@ func IsBranchAlreadyExists(err error) bool {
func (err BranchAlreadyExists) Error() string { func (err BranchAlreadyExists) Error() string {
return fmt.Sprintf("branch already exists [name: %s]", err.Name) return fmt.Sprintf("branch already exists [name: %s]", err.Name)
} }
type ErrBranchNotExist struct {
Name string
}
func IsErrBranchNotExist(err error) bool {
_, ok := err.(ErrBranchNotExist)
return ok
}
func (err ErrBranchNotExist) Error() string {
return fmt.Sprintf("branch does not exist [name: %s]", err.Name)
}

5
models/repo_branch.go

@ -11,6 +11,7 @@ import (
"github.com/Unknwon/com" "github.com/Unknwon/com"
"github.com/gogits/git-module" "github.com/gogits/git-module"
"github.com/gogits/gogs/models/errors"
"github.com/gogits/gogs/pkg/tool" "github.com/gogits/gogs/pkg/tool"
) )
@ -45,7 +46,7 @@ func GetBranchesByPath(path string) ([]*Branch, error) {
func (repo *Repository) GetBranch(br string) (*Branch, error) { func (repo *Repository) GetBranch(br string) (*Branch, error) {
if !git.IsBranchExist(repo.RepoPath(), br) { if !git.IsBranchExist(repo.RepoPath(), br) {
return nil, ErrBranchNotExist{br} return nil, errors.ErrBranchNotExist{br}
} }
return &Branch{ return &Branch{
RepoPath: repo.RepoPath(), RepoPath: repo.RepoPath(),
@ -101,7 +102,7 @@ func GetProtectBranchOfRepoByName(repoID int64, name string) (*ProtectBranch, er
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
return nil, ErrBranchNotExist{name} return nil, errors.ErrBranchNotExist{name}
} }
return protectBranch, nil return protectBranch, nil
} }

4
routes/api/v1/repo/branch.go

@ -7,7 +7,7 @@ package repo
import ( import (
api "github.com/gogits/go-gogs-client" api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models/errors"
"github.com/gogits/gogs/pkg/context" "github.com/gogits/gogs/pkg/context"
"github.com/gogits/gogs/routes/api/v1/convert" "github.com/gogits/gogs/routes/api/v1/convert"
) )
@ -16,7 +16,7 @@ import (
func GetBranch(c *context.APIContext) { func GetBranch(c *context.APIContext) {
branch, err := c.Repo.Repository.GetBranch(c.Params("*")) branch, err := c.Repo.Repository.GetBranch(c.Params("*"))
if err != nil { if err != nil {
if models.IsErrBranchNotExist(err) { if errors.IsErrBranchNotExist(err) {
c.Error(404, "GetBranch", err) c.Error(404, "GetBranch", err)
} else { } else {
c.Error(500, "GetBranch", err) c.Error(500, "GetBranch", err)

4
routes/repo/setting.go

@ -433,7 +433,7 @@ func SettingsProtectedBranch(c *context.Context) {
protectBranch, err := models.GetProtectBranchOfRepoByName(c.Repo.Repository.ID, branch) protectBranch, err := models.GetProtectBranchOfRepoByName(c.Repo.Repository.ID, branch)
if err != nil { if err != nil {
if !models.IsErrBranchNotExist(err) { if !errors.IsErrBranchNotExist(err) {
c.Handle(500, "GetProtectBranchOfRepoByName", err) c.Handle(500, "GetProtectBranchOfRepoByName", err)
return return
} }
@ -475,7 +475,7 @@ func SettingsProtectedBranchPost(c *context.Context, f form.ProtectBranch) {
protectBranch, err := models.GetProtectBranchOfRepoByName(c.Repo.Repository.ID, branch) protectBranch, err := models.GetProtectBranchOfRepoByName(c.Repo.Repository.ID, branch)
if err != nil { if err != nil {
if !models.IsErrBranchNotExist(err) { if !errors.IsErrBranchNotExist(err) {
c.Handle(500, "GetProtectBranchOfRepoByName", err) c.Handle(500, "GetProtectBranchOfRepoByName", err)
return return
} }

Loading…
Cancel
Save