diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-11-16 11:40:08 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-11-16 11:40:08 +0300 |
commit | 52c5d3444a671264cda5b5725fb94e626a400a21 (patch) | |
tree | b7691e43e0c25b32fdba2eb344de73da3dbc0205 /internal/git2go | |
parent | e8cd05759767cd226055e46e241bd1050826ffbb (diff) |
Revert "Merge branch 'wc/user-commit-files-structured-errors' into 'master'"
This reverts merge request !4988
Diffstat (limited to 'internal/git2go')
-rw-r--r-- | internal/git2go/commit.go | 105 | ||||
-rw-r--r-- | internal/git2go/commit_test.go | 24 | ||||
-rw-r--r-- | internal/git2go/serialization.go | 7 |
3 files changed, 36 insertions, 100 deletions
diff --git a/internal/git2go/commit.go b/internal/git2go/commit.go index 2bddad956..7197e5a37 100644 --- a/internal/git2go/commit.go +++ b/internal/git2go/commit.go @@ -2,108 +2,43 @@ package git2go import ( "context" - "errors" "fmt" "gitlab.com/gitlab-org/gitaly/v15/internal/git" "gitlab.com/gitlab-org/gitaly/v15/internal/git/repository" - "gitlab.com/gitlab-org/gitaly/v15/internal/helper" - "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" ) -// UnknownIndexError is an unspecified error that was produced by performing an invalid operation on the index. -type UnknownIndexError string +// IndexError is an error that was produced by performing an invalid operation on the index. +type IndexError string -// Error returns the error message of the unknown index error. -func (err UnknownIndexError) Error() string { return string(err) } +// Error returns the error message of the index error. +func (err IndexError) Error() string { return string(err) } -// IndexErrorType specifies which of the known index error types has occurred. -type IndexErrorType uint +// InvalidArgumentError is returned when an invalid argument is provided. +type InvalidArgumentError string -const ( - // ErrDirectoryExists represent a directory exists error. - ErrDirectoryExists IndexErrorType = iota - // ErrDirectoryTraversal represent a directory traversal error. - ErrDirectoryTraversal - // ErrEmptyPath represent an empty path error. - ErrEmptyPath - // ErrFileExists represent a file exists error. - ErrFileExists - // ErrFileNotFound represent a file not found error. - ErrFileNotFound - // ErrInvalidPath represent an invalid path error. - ErrInvalidPath -) +func (err InvalidArgumentError) Error() string { return string(err) } -// IndexError is a well-defined error that was produced by performing an invalid operation on the index. -type IndexError struct { - Path string - Type IndexErrorType -} +// FileNotFoundError is returned when an action attempts to operate on a non-existing file. +type FileNotFoundError string -// Error returns the error message associated with the error type. -func (err IndexError) Error() string { - switch err.Type { - case ErrDirectoryExists: - return "A directory with this name already exists" - case ErrDirectoryTraversal: - return "Path cannot include directory traversal" - case ErrEmptyPath: - return "You must provide a file path" - case ErrFileExists: - return "A file with this name already exists" - case ErrFileNotFound: - return "A file with this name doesn't exist" - case ErrInvalidPath: - return fmt.Sprintf("invalid path: '%s'", err.Path) - default: - panic(fmt.Sprintf("unhandled IndexErrorType: %v", err.Type)) - } +func (err FileNotFoundError) Error() string { + return fmt.Sprintf("file not found: %q", string(err)) } -// Proto returns the Protobuf representation of this error. -func (err IndexError) Proto() *gitalypb.IndexError { - errType := gitalypb.IndexError_ERROR_TYPE_UNSPECIFIED - switch err.Type { - case ErrDirectoryExists: - errType = gitalypb.IndexError_ERROR_TYPE_DIRECTORY_EXISTS - case ErrDirectoryTraversal: - errType = gitalypb.IndexError_ERROR_TYPE_DIRECTORY_TRAVERSAL - case ErrEmptyPath: - errType = gitalypb.IndexError_ERROR_TYPE_EMPTY_PATH - case ErrFileExists: - errType = gitalypb.IndexError_ERROR_TYPE_FILE_EXISTS - case ErrFileNotFound: - errType = gitalypb.IndexError_ERROR_TYPE_FILE_NOT_FOUND - case ErrInvalidPath: - errType = gitalypb.IndexError_ERROR_TYPE_INVALID_PATH - } +// FileExistsError is returned when an action attempts to overwrite an existing file. +type FileExistsError string - return &gitalypb.IndexError{ - Path: []byte(err.Path), - ErrorType: errType, - } +func (err FileExistsError) Error() string { + return fmt.Sprintf("file exists: %q", string(err)) } -// GrpcError returns the error wrapped with a gRPC code. -func (err IndexError) GrpcError() error { - e := errors.New(err.Error()) - switch err.Type { - case ErrDirectoryExists, ErrFileExists: - return helper.ErrAlreadyExists(e) - case ErrDirectoryTraversal, ErrEmptyPath, ErrInvalidPath: - return helper.ErrInvalidArgument(e) - case ErrFileNotFound: - return helper.ErrNotFound(e) - default: - return helper.ErrInternal(e) - } -} +// DirectoryExistsError is returned when an action attempts to overwrite a directory. +type DirectoryExistsError string -// InvalidArgumentError is returned when an invalid argument is provided. -type InvalidArgumentError string - -func (err InvalidArgumentError) Error() string { return string(err) } +func (err DirectoryExistsError) Error() string { + return fmt.Sprintf("directory exists: %q", string(err)) +} // CommitCommand contains the information and the steps to build a commit. type CommitCommand struct { diff --git a/internal/git2go/commit_test.go b/internal/git2go/commit_test.go index d153425a2..711021a10 100644 --- a/internal/git2go/commit_test.go +++ b/internal/git2go/commit_test.go @@ -91,7 +91,7 @@ func TestExecutor_Commit(t *testing.T) { CreateDirectory{Path: "directory"}, CreateDirectory{Path: "directory"}, }, - error: IndexError{Type: ErrDirectoryExists, Path: "directory"}, + error: DirectoryExistsError("directory"), }, }, }, @@ -110,7 +110,7 @@ func TestExecutor_Commit(t *testing.T) { actions: []Action{ CreateDirectory{Path: "directory"}, }, - error: IndexError{Type: ErrDirectoryExists, Path: "directory"}, + error: DirectoryExistsError("directory"), }, }, }, @@ -129,7 +129,7 @@ func TestExecutor_Commit(t *testing.T) { actions: []Action{ CreateDirectory{Path: "file"}, }, - error: IndexError{Type: ErrFileExists, Path: "file"}, + error: FileExistsError("file"), }, }, }, @@ -154,7 +154,7 @@ func TestExecutor_Commit(t *testing.T) { CreateFile{Path: "file", OID: originalFile.String()}, CreateFile{Path: "file", OID: updatedFile.String()}, }, - error: IndexError{Type: ErrFileExists, Path: "file"}, + error: FileExistsError("file"), }, }, }, @@ -214,7 +214,7 @@ func TestExecutor_Commit(t *testing.T) { actions: []Action{ UpdateFile{Path: "non-existing", OID: updatedFile.String()}, }, - error: IndexError{Type: ErrFileNotFound, Path: "non-existing"}, + error: FileNotFoundError("non-existing"), }, }, }, @@ -240,7 +240,7 @@ func TestExecutor_Commit(t *testing.T) { CreateDirectory{Path: "directory"}, MoveFile{Path: "directory", NewPath: "moved-directory"}, }, - error: IndexError{Type: ErrFileNotFound, Path: "directory"}, + error: FileNotFoundError("directory"), }, }, }, @@ -272,7 +272,7 @@ func TestExecutor_Commit(t *testing.T) { actions: []Action{ MoveFile{Path: "non-existing", NewPath: "destination-file"}, }, - error: IndexError{Type: ErrFileNotFound, Path: "non-existing"}, + error: FileNotFoundError("non-existing"), }, }, }, @@ -285,7 +285,7 @@ func TestExecutor_Commit(t *testing.T) { CreateFile{Path: "already-existing", OID: updatedFile.String()}, MoveFile{Path: "source-file", NewPath: "already-existing"}, }, - error: IndexError{Type: ErrFileExists, Path: "already-existing"}, + error: FileExistsError("already-existing"), }, }, }, @@ -334,7 +334,7 @@ func TestExecutor_Commit(t *testing.T) { actions: []Action{ ChangeFileMode{Path: "non-existing"}, }, - error: IndexError{Type: ErrFileNotFound, Path: "non-existing"}, + error: FileNotFoundError("non-existing"), }, }, }, @@ -402,7 +402,7 @@ func TestExecutor_Commit(t *testing.T) { actions: []Action{ MoveFile{Path: "non-existing", NewPath: "destination"}, }, - error: IndexError{Type: ErrFileNotFound, Path: "non-existing"}, + error: FileNotFoundError("non-existing"), }, }, }, @@ -415,7 +415,7 @@ func TestExecutor_Commit(t *testing.T) { CreateFile{Path: "file-2", OID: updatedFile.String()}, MoveFile{Path: "file-1", NewPath: "file-2"}, }, - error: IndexError{Type: ErrFileExists, Path: "file-2"}, + error: FileExistsError("file-2"), }, }, }, @@ -426,7 +426,7 @@ func TestExecutor_Commit(t *testing.T) { actions: []Action{ DeleteFile{Path: "non-existing"}, }, - error: IndexError{Type: ErrFileNotFound, Path: "non-existing"}, + error: FileNotFoundError("non-existing"), }, }, }, diff --git a/internal/git2go/serialization.go b/internal/git2go/serialization.go index becc21537..0f4949c5f 100644 --- a/internal/git2go/serialization.go +++ b/internal/git2go/serialization.go @@ -21,13 +21,14 @@ var registeredTypes = map[reflect.Type]struct{}{ reflect.TypeOf(MoveFile{}): {}, reflect.TypeOf(UpdateFile{}): {}, reflect.TypeOf(wrapError{}): {}, - reflect.TypeOf(IndexError{}): {}, - reflect.TypeOf(UnknownIndexError("")): {}, + reflect.TypeOf(DirectoryExistsError("")): {}, + reflect.TypeOf(FileExistsError("")): {}, + reflect.TypeOf(FileNotFoundError("")): {}, reflect.TypeOf(InvalidArgumentError("")): {}, reflect.TypeOf(HasConflictsError{}): {}, reflect.TypeOf(ConflictingFilesError{}): {}, reflect.TypeOf(EmptyError{}): {}, - reflect.TypeOf(UnknownIndexError("")): {}, + reflect.TypeOf(IndexError("")): {}, reflect.TypeOf(ConflictError{}): {}, reflect.TypeOf(CommitNotFoundError{}): {}, } |