mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 02:08:36 +00:00
Refactor error system (#33610)
This commit is contained in:
@@ -72,21 +72,21 @@ func Migrate(ctx *context.APIContext) {
|
||||
}
|
||||
if err != nil {
|
||||
if user_model.IsErrUserNotExist(err) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", err)
|
||||
ctx.APIError(http.StatusUnprocessableEntity, err)
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "GetUser", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if ctx.HasAPIError() {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", ctx.GetErrMsg())
|
||||
ctx.APIError(http.StatusUnprocessableEntity, ctx.GetErrMsg())
|
||||
return
|
||||
}
|
||||
|
||||
if !ctx.Doer.IsAdmin {
|
||||
if !repoOwner.IsOrganization() && ctx.Doer.ID != repoOwner.ID {
|
||||
ctx.Error(http.StatusForbidden, "", "Given user is not an organization.")
|
||||
ctx.APIError(http.StatusForbidden, "Given user is not an organization.")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -94,10 +94,10 @@ func Migrate(ctx *context.APIContext) {
|
||||
// Check ownership of organization.
|
||||
isOwner, err := organization.OrgFromUser(repoOwner).IsOwnedBy(ctx, ctx.Doer.ID)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "IsOwnedBy", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return
|
||||
} else if !isOwner {
|
||||
ctx.Error(http.StatusForbidden, "", "Given user is not owner of organization.")
|
||||
ctx.APIError(http.StatusForbidden, "Given user is not owner of organization.")
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -115,12 +115,12 @@ func Migrate(ctx *context.APIContext) {
|
||||
gitServiceType := convert.ToGitServiceType(form.Service)
|
||||
|
||||
if form.Mirror && setting.Mirror.DisableNewPull {
|
||||
ctx.Error(http.StatusForbidden, "MirrorsGlobalDisabled", fmt.Errorf("the site administrator has disabled the creation of new pull mirrors"))
|
||||
ctx.APIError(http.StatusForbidden, fmt.Errorf("the site administrator has disabled the creation of new pull mirrors"))
|
||||
return
|
||||
}
|
||||
|
||||
if setting.Repository.DisableMigrations {
|
||||
ctx.Error(http.StatusForbidden, "MigrationsGlobalDisabled", fmt.Errorf("the site administrator has disabled migrations"))
|
||||
ctx.APIError(http.StatusForbidden, fmt.Errorf("the site administrator has disabled migrations"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ func Migrate(ctx *context.APIContext) {
|
||||
if form.LFS && len(form.LFSEndpoint) > 0 {
|
||||
ep := lfs.DetermineEndpoint("", form.LFSEndpoint)
|
||||
if ep == nil {
|
||||
ctx.Error(http.StatusInternalServerError, "", ctx.Tr("repo.migrate.invalid_lfs_endpoint"))
|
||||
ctx.APIError(http.StatusInternalServerError, ctx.Tr("repo.migrate.invalid_lfs_endpoint"))
|
||||
return
|
||||
}
|
||||
err = migrations.IsMigrateURLAllowed(ep.String(), ctx.Doer)
|
||||
@@ -221,35 +221,35 @@ func Migrate(ctx *context.APIContext) {
|
||||
func handleMigrateError(ctx *context.APIContext, repoOwner *user_model.User, err error) {
|
||||
switch {
|
||||
case repo_model.IsErrRepoAlreadyExist(err):
|
||||
ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.")
|
||||
ctx.APIError(http.StatusConflict, "The repository with the same name already exists.")
|
||||
case repo_model.IsErrRepoFilesAlreadyExist(err):
|
||||
ctx.Error(http.StatusConflict, "", "Files already exist for this repository. Adopt them or delete them.")
|
||||
ctx.APIError(http.StatusConflict, "Files already exist for this repository. Adopt them or delete them.")
|
||||
case migrations.IsRateLimitError(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Remote visit addressed rate limitation.")
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "Remote visit addressed rate limitation.")
|
||||
case migrations.IsTwoFactorAuthError(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Remote visit required two factors authentication.")
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "Remote visit required two factors authentication.")
|
||||
case repo_model.IsErrReachLimitOfRepo(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("You have already reached your limit of %d repositories.", repoOwner.MaxCreationLimit()))
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("You have already reached your limit of %d repositories.", repoOwner.MaxCreationLimit()))
|
||||
case db.IsErrNameReserved(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The username '%s' is reserved.", err.(db.ErrNameReserved).Name))
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The username '%s' is reserved.", err.(db.ErrNameReserved).Name))
|
||||
case db.IsErrNameCharsNotAllowed(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The username '%s' contains invalid characters.", err.(db.ErrNameCharsNotAllowed).Name))
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The username '%s' contains invalid characters.", err.(db.ErrNameCharsNotAllowed).Name))
|
||||
case db.IsErrNamePatternNotAllowed(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The pattern '%s' is not allowed in a username.", err.(db.ErrNamePatternNotAllowed).Pattern))
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The pattern '%s' is not allowed in a username.", err.(db.ErrNamePatternNotAllowed).Pattern))
|
||||
case git.IsErrInvalidCloneAddr(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", err)
|
||||
ctx.APIError(http.StatusUnprocessableEntity, err)
|
||||
case base.IsErrNotSupported(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", err)
|
||||
ctx.APIError(http.StatusUnprocessableEntity, err)
|
||||
default:
|
||||
err = util.SanitizeErrorCredentialURLs(err)
|
||||
if strings.Contains(err.Error(), "Authentication failed") ||
|
||||
strings.Contains(err.Error(), "Bad credentials") ||
|
||||
strings.Contains(err.Error(), "could not read Username") {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Authentication failed: %v.", err))
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("Authentication failed: %v.", err))
|
||||
} else if strings.Contains(err.Error(), "fatal:") {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Migration failed: %v.", err))
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("Migration failed: %v.", err))
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "MigrateRepository", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -259,19 +259,19 @@ func handleRemoteAddrError(ctx *context.APIContext, err error) {
|
||||
addrErr := err.(*git.ErrInvalidCloneAddr)
|
||||
switch {
|
||||
case addrErr.IsURLError:
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", err)
|
||||
ctx.APIError(http.StatusUnprocessableEntity, err)
|
||||
case addrErr.IsPermissionDenied:
|
||||
if addrErr.LocalPath {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "You are not allowed to import local repositories.")
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "You are not allowed to import local repositories.")
|
||||
} else {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "You can not import from disallowed hosts.")
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "You can not import from disallowed hosts.")
|
||||
}
|
||||
case addrErr.IsInvalidPath:
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid local path, it does not exist or not a directory.")
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "Invalid local path, it does not exist or not a directory.")
|
||||
default:
|
||||
ctx.Error(http.StatusInternalServerError, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
|
||||
ctx.APIError(http.StatusInternalServerError, "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
|
||||
}
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "ParseRemoteAddr", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user