diff options
Diffstat (limited to 'internal/route/api/v1/repo/repo.go')
-rw-r--r-- | internal/route/api/v1/repo/repo.go | 72 |
1 files changed, 35 insertions, 37 deletions
diff --git a/internal/route/api/v1/repo/repo.go b/internal/route/api/v1/repo/repo.go index 7ea9a870..e198dffc 100644 --- a/internal/route/api/v1/repo/repo.go +++ b/internal/route/api/v1/repo/repo.go @@ -5,18 +5,16 @@ package repo import ( - "fmt" "net/http" "path" - log "unknwon.dev/clog/v2" - api "github.com/gogs/go-gogs-client" + "github.com/pkg/errors" + log "unknwon.dev/clog/v2" "gogs.io/gogs/internal/conf" "gogs.io/gogs/internal/context" "gogs.io/gogs/internal/db" - "gogs.io/gogs/internal/db/errors" "gogs.io/gogs/internal/form" "gogs.io/gogs/internal/route/api/v1/convert" ) @@ -81,7 +79,7 @@ func Search(c *context.APIContext) { func listUserRepositories(c *context.APIContext, username string) { user, err := db.GetUserByName(username) if err != nil { - c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err) + c.NotFoundOrError(err, "get user by name") return } @@ -99,12 +97,12 @@ func listUserRepositories(c *context.APIContext, username string) { }) } if err != nil { - c.ServerError("GetUserRepositories", err) + c.Error(err, "get user repositories") return } if err = db.RepositoryList(ownRepos).LoadAttributes(); err != nil { - c.ServerError("LoadAttributes(ownRepos)", err) + c.Error(err, "load attributes") return } @@ -120,7 +118,7 @@ func listUserRepositories(c *context.APIContext, username string) { accessibleRepos, err := user.GetRepositoryAccesses() if err != nil { - c.ServerError("GetRepositoryAccesses", err) + c.Error(err, "get repositories accesses") return } @@ -169,14 +167,14 @@ func CreateUserRepo(c *context.APIContext, owner *db.User, opt api.CreateRepoOpt if db.IsErrRepoAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNamePatternNotAllowed(err) { - c.Error(http.StatusUnprocessableEntity, "", err) + c.ErrorStatus(http.StatusUnprocessableEntity, err) } else { if repo != nil { if err = db.DeleteRepository(c.User.ID, repo.ID); err != nil { - log.Error("DeleteRepository: %v", err) + log.Error("Failed to delete repository: %v", err) } } - c.ServerError("CreateRepository", err) + c.Error(err, "create repository") } return } @@ -187,7 +185,7 @@ func CreateUserRepo(c *context.APIContext, owner *db.User, opt api.CreateRepoOpt func Create(c *context.APIContext, opt api.CreateRepoOption) { // Shouldn't reach this condition, but just in case. if c.User.IsOrganization() { - c.Error(http.StatusUnprocessableEntity, "", "not allowed creating repository for organization") + c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Not allowed to create repository for organization.")) return } CreateUserRepo(c, c.User, opt) @@ -196,12 +194,12 @@ func Create(c *context.APIContext, opt api.CreateRepoOption) { func CreateOrgRepo(c *context.APIContext, opt api.CreateRepoOption) { org, err := db.GetOrgByName(c.Params(":org")) if err != nil { - c.NotFoundOrServerError("GetOrgByName", errors.IsUserNotExist, err) + c.NotFoundOrError(err, "get organization by name") return } if !org.IsOwnedBy(c.User.ID) { - c.Error(http.StatusForbidden, "", "given user is not owner of organization") + c.ErrorStatus(http.StatusForbidden, errors.New("Given user is not owner of organization.")) return } CreateUserRepo(c, org, opt) @@ -214,28 +212,28 @@ func Migrate(c *context.APIContext, f form.MigrateRepo) { if f.Uid != ctxUser.ID { org, err := db.GetUserByID(f.Uid) if err != nil { - if errors.IsUserNotExist(err) { - c.Error(http.StatusUnprocessableEntity, "", err) + if db.IsErrUserNotExist(err) { + c.ErrorStatus(http.StatusUnprocessableEntity, err) } else { - c.Error(http.StatusInternalServerError, "GetUserByID", err) + c.Error(err, "get user by ID") } return } else if !org.IsOrganization() && !c.User.IsAdmin { - c.Error(http.StatusForbidden, "", "given user is not an organization") + c.ErrorStatus(http.StatusForbidden, errors.New("Given user is not an organization.")) return } ctxUser = org } if c.HasError() { - c.Error(http.StatusUnprocessableEntity, "", c.GetErrMsg()) + c.ErrorStatus(http.StatusUnprocessableEntity, errors.New(c.GetErrMsg())) return } if ctxUser.IsOrganization() && !c.User.IsAdmin { // Check ownership of organization. if !ctxUser.IsOwnedBy(c.User.ID) { - c.Error(http.StatusForbidden, "", "Given user is not owner of organization") + c.ErrorStatus(http.StatusForbidden, errors.New("Given user is not owner of organization.")) return } } @@ -246,16 +244,16 @@ func Migrate(c *context.APIContext, f form.MigrateRepo) { addrErr := err.(db.ErrInvalidCloneAddr) switch { case addrErr.IsURLError: - c.Error(http.StatusUnprocessableEntity, "", err) + c.ErrorStatus(http.StatusUnprocessableEntity, err) case addrErr.IsPermissionDenied: - c.Error(http.StatusUnprocessableEntity, "", "you are not allowed to import local repositories") + c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("You are not allowed to import local repositories.")) case addrErr.IsInvalidPath: - c.Error(http.StatusUnprocessableEntity, "", "invalid local path, it does not exist or not a directory") + c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Invalid local path, it does not exist or not a directory.")) default: - c.ServerError("ParseRemoteAddr", fmt.Errorf("unknown error type (ErrInvalidCloneAddr): %v", err)) + c.Error(err, "unexpected error") } } else { - c.ServerError("ParseRemoteAddr", err) + c.Error(err, "parse remote address") } return } @@ -274,10 +272,10 @@ func Migrate(c *context.APIContext, f form.MigrateRepo) { } } - if errors.IsReachLimitOfRepo(err) { - c.Error(http.StatusUnprocessableEntity, "", err) + if db.IsErrReachLimitOfRepo(err) { + c.ErrorStatus(http.StatusUnprocessableEntity, err) } else { - c.ServerError("MigrateRepository", errors.New(db.HandleMirrorCredentials(err.Error(), true))) + c.Error(errors.New(db.HandleMirrorCredentials(err.Error(), true)), "migrate repository") } return } @@ -290,17 +288,17 @@ func Migrate(c *context.APIContext, f form.MigrateRepo) { func parseOwnerAndRepo(c *context.APIContext) (*db.User, *db.Repository) { owner, err := db.GetUserByName(c.Params(":username")) if err != nil { - if errors.IsUserNotExist(err) { - c.Error(http.StatusUnprocessableEntity, "", err) + if db.IsErrUserNotExist(err) { + c.ErrorStatus(http.StatusUnprocessableEntity, err) } else { - c.ServerError("GetUserByName", err) + c.Error(err, "get user by name") } return nil, nil } repo, err := db.GetRepositoryByName(owner.ID, c.Params(":reponame")) if err != nil { - c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err) + c.NotFoundOrError(err, "get repository by name") return nil, nil } @@ -327,12 +325,12 @@ func Delete(c *context.APIContext) { } if owner.IsOrganization() && !owner.IsOwnedBy(c.User.ID) { - c.Error(http.StatusForbidden, "", "given user is not owner of organization") + c.ErrorStatus(http.StatusForbidden, errors.New("Given user is not owner of organization.")) return } if err := db.DeleteRepository(owner.ID, repo.ID); err != nil { - c.ServerError("DeleteRepository", err) + c.Error(err, "delete repository") return } @@ -343,14 +341,14 @@ func Delete(c *context.APIContext) { func ListForks(c *context.APIContext) { forks, err := c.Repo.Repository.GetForks() if err != nil { - c.ServerError("GetForks", err) + c.Error(err, "get forks") return } apiForks := make([]*api.Repository, len(forks)) for i := range forks { if err := forks[i].GetOwner(); err != nil { - c.ServerError("GetOwner", err) + c.Error(err, "get owner") return } apiForks[i] = forks[i].APIFormat(&api.Permission{ @@ -386,7 +384,7 @@ func IssueTracker(c *context.APIContext, form api.EditIssueTrackerOption) { } if err := db.UpdateRepository(repo, false); err != nil { - c.ServerError("UpdateRepository", err) + c.Error(err, "update repository") return } |