Browse Source

api/repo: improve migration error handling

Unknwon 7 years ago
parent
commit
9e3c83372f
6 changed files with 24 additions and 18 deletions
  1. 0 13
      models/error.go
  2. 13 0
      models/errors/repo.go
  3. 1 1
      models/repo.go
  4. 2 1
      models/user.go
  5. 7 2
      routers/api/v1/repo/repo.go
  6. 1 1
      routers/repo/repo.go

+ 0 - 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 - 0
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
 }

+ 1 - 1
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{

+ 2 - 1
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 - 2
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)
 			}
 		}
-		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
 	}
 

+ 1 - 1
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