api/repo: improve migration error handling

master
Unknwon 2017-04-07 16:00:25 -04:00
parent 809db853fa
commit 9e3c83372f
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
6 changed files with 24 additions and 18 deletions

View File

@ -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)
}
// __ __.__ __ .__
// / \ / \__| | _|__|
// \ \/\/ / | |/ / |

View File

@ -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
}

View File

@ -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{

View File

@ -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

View File

@ -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)
}
}
ctx.Error(500, "MigrateRepository", models.HandleMirrorCredentials(err.Error(), true))
if errors.IsReachLimitOfRepo(err) {
ctx.Error(422, "", err)
} else {
ctx.Error(500, "MigrateRepository", models.HandleMirrorCredentials(err.Error(), true))
}
return
}

View File

@ -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