Przeglądaj źródła

chore: remove `internal/database/errors` package (#8096)

Copilot 2 tygodni temu
rodzic
commit
ae59787ff5

+ 0 - 10
internal/database/errors/errors.go

@@ -1,10 +0,0 @@
-package errors
-
-import "errors"
-
-var ErrInternalServerError = errors.New("internal server error")
-
-// New is a wrapper of real errors.New function.
-func New(text string) error {
-	return errors.New(text)
-}

+ 0 - 44
internal/database/errors/repo.go

@@ -1,44 +0,0 @@
-package errors
-
-import (
-	"fmt"
-)
-
-type InvalidRepoReference struct {
-	Ref string
-}
-
-func IsInvalidRepoReference(err error) bool {
-	_, ok := err.(InvalidRepoReference)
-	return ok
-}
-
-func (err InvalidRepoReference) Error() string {
-	return fmt.Sprintf("invalid repository reference [ref: %s]", err.Ref)
-}
-
-type MirrorNotExist struct {
-	RepoID int64
-}
-
-func IsMirrorNotExist(err error) bool {
-	_, ok := err.(MirrorNotExist)
-	return ok
-}
-
-func (err MirrorNotExist) Error() string {
-	return fmt.Sprintf("mirror does not exist [repo_id: %d]", err.RepoID)
-}
-
-type BranchAlreadyExists struct {
-	Name string
-}
-
-func IsBranchAlreadyExists(err error) bool {
-	_, ok := err.(BranchAlreadyExists)
-	return ok
-}
-
-func (err BranchAlreadyExists) Error() string {
-	return fmt.Sprintf("branch already exists [name: %s]", err.Name)
-}

+ 2 - 2
internal/database/issue.go

@@ -2,6 +2,7 @@ package database
 
 import (
 	"context"
+	"errors"
 	"fmt"
 	"strings"
 	"time"
@@ -13,13 +14,12 @@ import (
 	api "github.com/gogs/go-gogs-client"
 
 	"gogs.io/gogs/internal/conf"
-	"gogs.io/gogs/internal/database/errors"
 	"gogs.io/gogs/internal/errutil"
 	"gogs.io/gogs/internal/markup"
 	"gogs.io/gogs/internal/tool"
 )
 
-var ErrMissingIssueNumber = errors.New("No issue number specified")
+var ErrMissingIssueNumber = errors.New("no issue number specified")
 
 // Issue represents an issue or pull request of repository.
 type Issue struct {

+ 16 - 2
internal/database/mirror.go

@@ -15,13 +15,27 @@ import (
 	"github.com/gogs/git-module"
 
 	"gogs.io/gogs/internal/conf"
-	"gogs.io/gogs/internal/database/errors"
 	"gogs.io/gogs/internal/process"
 	"gogs.io/gogs/internal/sync"
 )
 
 var MirrorQueue = sync.NewUniqueQueue(1000)
 
+// MirrorNotExist represents an error when mirror does not exist.
+type MirrorNotExist struct {
+	RepoID int64
+}
+
+// IsMirrorNotExist returns true if the error is MirrorNotExist.
+func IsMirrorNotExist(err error) bool {
+	_, ok := err.(MirrorNotExist)
+	return ok
+}
+
+func (err MirrorNotExist) Error() string {
+	return fmt.Sprintf("mirror does not exist [repo_id: %d]", err.RepoID)
+}
+
 // Mirror represents mirror information of a repository.
 type Mirror struct {
 	ID          int64
@@ -268,7 +282,7 @@ func getMirrorByRepoID(e Engine, repoID int64) (*Mirror, error) {
 	if err != nil {
 		return nil, err
 	} else if !has {
-		return nil, errors.MirrorNotExist{RepoID: repoID}
+		return nil, MirrorNotExist{RepoID: repoID}
 	}
 	return m, nil
 }

+ 1 - 1
internal/database/org_team.go

@@ -2,12 +2,12 @@ package database
 
 import (
 	"context"
+	"errors"
 	"fmt"
 	"strings"
 
 	"xorm.io/xorm"
 
-	"gogs.io/gogs/internal/database/errors"
 	"gogs.io/gogs/internal/errutil"
 )
 

+ 16 - 2
internal/database/repo.go

@@ -29,7 +29,6 @@ import (
 	embedConf "gogs.io/gogs/conf"
 	"gogs.io/gogs/internal/avatar"
 	"gogs.io/gogs/internal/conf"
-	dberrors "gogs.io/gogs/internal/database/errors"
 	"gogs.io/gogs/internal/dbutil"
 	"gogs.io/gogs/internal/errutil"
 	"gogs.io/gogs/internal/markup"
@@ -43,6 +42,21 @@ import (
 // RepoAvatarURLPrefix is used to identify a URL is to access repository avatar.
 const RepoAvatarURLPrefix = "repo-avatars"
 
+// InvalidRepoReference represents an error when repository reference is invalid.
+type InvalidRepoReference struct {
+	Ref string
+}
+
+// IsInvalidRepoReference returns true if the error is InvalidRepoReference.
+func IsInvalidRepoReference(err error) bool {
+	_, ok := err.(InvalidRepoReference)
+	return ok
+}
+
+func (err InvalidRepoReference) Error() string {
+	return fmt.Sprintf("invalid repository reference [ref: %s]", err.Ref)
+}
+
 var repoWorkingPool = sync.NewExclusivePool()
 
 var (
@@ -1751,7 +1765,7 @@ func DeleteRepository(ownerID, repoID int64) error {
 func GetRepositoryByRef(ref string) (*Repository, error) {
 	n := strings.IndexByte(ref, byte('/'))
 	if n < 2 {
-		return nil, dberrors.InvalidRepoReference{Ref: ref}
+		return nil, InvalidRepoReference{Ref: ref}
 	}
 
 	userName, repoName := ref[:n], ref[n+1:]

+ 16 - 2
internal/database/repo_editor.go

@@ -19,7 +19,6 @@ import (
 
 	"gogs.io/gogs/internal/conf"
 	"gogs.io/gogs/internal/cryptoutil"
-	dberrors "gogs.io/gogs/internal/database/errors"
 	"gogs.io/gogs/internal/gitutil"
 	"gogs.io/gogs/internal/osutil"
 	"gogs.io/gogs/internal/pathutil"
@@ -27,6 +26,21 @@ import (
 	"gogs.io/gogs/internal/tool"
 )
 
+// BranchAlreadyExists represents an error when branch already exists.
+type BranchAlreadyExists struct {
+	Name string
+}
+
+// IsBranchAlreadyExists returns true if the error is BranchAlreadyExists.
+func IsBranchAlreadyExists(err error) bool {
+	_, ok := err.(BranchAlreadyExists)
+	return ok
+}
+
+func (err BranchAlreadyExists) Error() string {
+	return fmt.Sprintf("branch already exists [name: %s]", err.Name)
+}
+
 const (
 	EnvAuthUserID          = "GOGS_AUTH_USER_ID"
 	EnvAuthUserName        = "GOGS_AUTH_USER_NAME"
@@ -154,7 +168,7 @@ func (r *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) erro
 	if opts.OldBranch != opts.NewBranch {
 		// Directly return error if new branch already exists in the server
 		if git.RepoHasBranch(repoPath, opts.NewBranch) {
-			return dberrors.BranchAlreadyExists{Name: opts.NewBranch}
+			return BranchAlreadyExists{Name: opts.NewBranch}
 		}
 
 		// Otherwise, delete branch from local copy in case out of sync

+ 13 - 1
internal/database/repo_test.go

@@ -8,6 +8,7 @@ import (
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
 
+	"gogs.io/gogs/internal/conf"
 	"gogs.io/gogs/internal/markup"
 	"gogs.io/gogs/internal/osutil"
 )
@@ -58,6 +59,17 @@ func TestRepository_ComposeMetas(t *testing.T) {
 }
 
 func Test_CreateRepository_PreventDeletion(t *testing.T) {
+	tempRepositoryRoot := filepath.Join(os.TempDir(), "createRepository-tempRepositoryRoot")
+	conf.SetMockRepository(
+		t,
+		conf.RepositoryOpts{
+			Root: tempRepositoryRoot,
+		},
+	)
+	err := os.RemoveAll(tempRepositoryRoot)
+	require.NoError(t, err)
+	defer func() { _ = os.RemoveAll(tempRepositoryRoot) }()
+
 	owner := &User{Name: "testuser"}
 	opts := CreateRepoOptionsLegacy{Name: "safety-test"}
 	repoPath := RepoPath(owner.Name, opts.Name)
@@ -66,7 +78,7 @@ func Test_CreateRepository_PreventDeletion(t *testing.T) {
 	canary := filepath.Join(repoPath, "canary.txt")
 	require.NoError(t, os.WriteFile(canary, []byte("should survive"), 0o644))
 
-	_, err := CreateRepository(owner, owner, opts)
+	_, err = CreateRepository(owner, owner, opts)
 	require.Error(t, err)
 	assert.Contains(t, err.Error(), "repository directory already exists")
 	assert.True(t, osutil.Exist(canary))

+ 6 - 4
internal/route/repo/editor.go

@@ -1,6 +1,7 @@
 package repo
 
 import (
+	"errors"
 	"fmt"
 	"net/http"
 	"path"
@@ -11,7 +12,6 @@ import (
 	"gogs.io/gogs/internal/conf"
 	"gogs.io/gogs/internal/context"
 	"gogs.io/gogs/internal/database"
-	"gogs.io/gogs/internal/database/errors"
 	"gogs.io/gogs/internal/form"
 	"gogs.io/gogs/internal/gitutil"
 	"gogs.io/gogs/internal/pathutil"
@@ -26,6 +26,8 @@ const (
 	tmplEditorUpload      = "repo/editor/upload"
 )
 
+var errInternalServerError = errors.New("internal server error")
+
 // getParentTreeFields returns list of parent tree names and corresponding tree paths
 // based on given tree path.
 func getParentTreeFields(treePath string) (treeNames, treePaths []string) {
@@ -278,7 +280,7 @@ func editFilePost(c *context.Context, f form.EditRepoFile, isNewFile bool) {
 	}); err != nil {
 		log.Error("Failed to update repo file: %v", err)
 		c.FormErr("TreePath")
-		c.RenderWithErr(c.Tr("repo.editor.fail_to_update_file", f.TreePath, errors.ErrInternalServerError), tmplEditorEdit, &f)
+		c.RenderWithErr(c.Tr("repo.editor.fail_to_update_file", f.TreePath, errInternalServerError), tmplEditorEdit, &f)
 		return
 	}
 
@@ -386,7 +388,7 @@ func DeleteFilePost(c *context.Context, f form.DeleteRepoFile) {
 		Message:      message,
 	}); err != nil {
 		log.Error("Failed to delete repo file: %v", err)
-		c.RenderWithErr(c.Tr("repo.editor.fail_to_delete_file", c.Repo.TreePath, errors.ErrInternalServerError), tmplEditorDelete, &f)
+		c.RenderWithErr(c.Tr("repo.editor.fail_to_delete_file", c.Repo.TreePath, errInternalServerError), tmplEditorDelete, &f)
 		return
 	}
 
@@ -508,7 +510,7 @@ func UploadFilePost(c *context.Context, f form.UploadRepoFile) {
 	}); err != nil {
 		log.Error("Failed to upload files: %v", err)
 		c.FormErr("TreePath")
-		c.RenderWithErr(c.Tr("repo.editor.unable_to_upload_files", f.TreePath, errors.ErrInternalServerError), tmplEditorUpload, &f)
+		c.RenderWithErr(c.Tr("repo.editor.unable_to_upload_files", f.TreePath, errInternalServerError), tmplEditorUpload, &f)
 		return
 	}
 

+ 3 - 3
internal/route/repo/issue.go

@@ -1,6 +1,7 @@
 package repo
 
 import (
+	"errors"
 	"fmt"
 	"net/http"
 	"net/url"
@@ -14,7 +15,6 @@ import (
 	"gogs.io/gogs/internal/conf"
 	"gogs.io/gogs/internal/context"
 	"gogs.io/gogs/internal/database"
-	"gogs.io/gogs/internal/database/errors"
 	"gogs.io/gogs/internal/form"
 	"gogs.io/gogs/internal/markup"
 	"gogs.io/gogs/internal/tool"
@@ -33,8 +33,8 @@ const (
 )
 
 var (
-	ErrFileTypeForbidden = errors.New("File type is not allowed")
-	ErrTooManyFiles      = errors.New("Maximum number of files to upload exceeded")
+	ErrFileTypeForbidden = errors.New("file type is not allowed")
+	ErrTooManyFiles      = errors.New("maximum number of files to upload exceeded")
 
 	IssueTemplateCandidates = []string{
 		"ISSUE_TEMPLATE.md",

+ 1 - 1
internal/route/repo/setting.go

@@ -1,6 +1,7 @@
 package repo
 
 import (
+	"errors"
 	"fmt"
 	"io"
 	"strings"
@@ -13,7 +14,6 @@ import (
 	"gogs.io/gogs/internal/conf"
 	"gogs.io/gogs/internal/context"
 	"gogs.io/gogs/internal/database"
-	"gogs.io/gogs/internal/database/errors"
 	"gogs.io/gogs/internal/email"
 	"gogs.io/gogs/internal/form"
 	"gogs.io/gogs/internal/osutil"

+ 1 - 1
internal/route/repo/webhook.go

@@ -1,6 +1,7 @@
 package repo
 
 import (
+	"errors"
 	"fmt"
 	"net/http"
 	"net/url"
@@ -15,7 +16,6 @@ import (
 	"gogs.io/gogs/internal/conf"
 	"gogs.io/gogs/internal/context"
 	"gogs.io/gogs/internal/database"
-	"gogs.io/gogs/internal/database/errors"
 	"gogs.io/gogs/internal/form"
 	"gogs.io/gogs/internal/netutil"
 )