Browse Source

api/repo: improve migration error handling

pull/3974/merge
Unknwon 8 years ago
parent
commit
9e3c83372f
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 13
      models/error.go
  2. 13
      models/errors/repo.go
  3. 2
      models/repo.go
  4. 3
      models/user.go
  5. 7
      routers/api/v1/repo/repo.go
  6. 2
      routers/repo/repo.go

13
models/error.go

@ -93,19 +93,6 @@ func (err ErrUserHasOrgs) Error() string {
return fmt.Sprintf("user still has membership of organizations [uid: %d]", err.UID)
}
type ErrReachLimitOfRepo struct {
Limit int
}
func IsErrReachLimitOfRepo(err error) bool {
_, ok := err.(ErrReachLimitOfRepo)
return ok
}
func (err ErrReachLimitOfRepo) Error() string {
return fmt.Sprintf("user has reached maximum limit of repositories [limit: %d]", err.Limit)
}
// __ __.__ __ .__
// / \ / \__| | _|__|
// \ \/\/ / | |/ / |

13
models/errors/repo.go

@ -21,6 +21,19 @@ func (err RepoNotExist) Error() string {
return fmt.Sprintf("repository does not exist [id: %d, user_id: %d, name: %s]", err.ID, err.UserID, err.Name)
}
type ReachLimitOfRepo struct {
Limit int
}
func IsReachLimitOfRepo(err error) bool {
_, ok := err.(ReachLimitOfRepo)
return ok
}
func (err ReachLimitOfRepo) Error() string {
return fmt.Sprintf("user has reached maximum limit of repositories [limit: %d]", err.Limit)
}
type InvalidRepoReference struct {
Ref string
}

2
models/repo.go

@ -1019,7 +1019,7 @@ func createRepository(e *xorm.Session, doer, owner *User, repo *Repository) (err
// CreateRepository creates a repository for given user or organization.
func CreateRepository(doer, owner *User, opts CreateRepoOptions) (_ *Repository, err error) {
if !owner.CanCreateRepo() {
return nil, ErrReachLimitOfRepo{owner.MaxRepoCreation}
return nil, errors.ReachLimitOfRepo{owner.RepoCreationNum()}
}
repo := &Repository{

3
models/user.go

@ -610,8 +610,9 @@ func getVerifyUser(code string) (user *User) {
if b, err := hex.DecodeString(hexStr); err == nil {
if user, err = GetUserByName(string(b)); user != nil {
return user
} else if !errors.IsUserNotExist(err) {
log.Error(2, "GetUserByName: %v", err)
}
log.Error(4, "user.getVerifyUser: %v", err)
}
return nil

7
routers/api/v1/repo/repo.go

@ -250,7 +250,7 @@ func Migrate(ctx *context.APIContext, f form.MigrateRepo) {
case addrErr.IsPermissionDenied:
ctx.Error(422, "", "You are not allowed to import local repositories")
case addrErr.IsInvalidPath:
ctx.Error(422, "", "Invalid local path, it does not exist or not a directory.")
ctx.Error(422, "", "Invalid local path, it does not exist or not a directory")
default:
ctx.Error(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
}
@ -273,7 +273,12 @@ func Migrate(ctx *context.APIContext, f form.MigrateRepo) {
log.Error(2, "DeleteRepository: %v", errDelete)
}
}
if errors.IsReachLimitOfRepo(err) {
ctx.Error(422, "", err)
} else {
ctx.Error(500, "MigrateRepository", models.HandleMirrorCredentials(err.Error(), true))
}
return
}

2
routers/repo/repo.go

@ -87,7 +87,7 @@ func Create(ctx *context.Context) {
func handleCreateError(ctx *context.Context, owner *models.User, err error, name, tpl string, form interface{}) {
switch {
case models.IsErrReachLimitOfRepo(err):
case errors.IsReachLimitOfRepo(err):
ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", owner.RepoCreationNum()), tpl, form)
case models.IsErrRepoAlreadyExist(err):
ctx.Data["Err_RepoName"] = true

Loading…
Cancel
Save