Welcome to mirror list, hosted at ThFree Co, Russian Federation.

commit.go « git2go « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4325cd930e5cc5301f80031c80593c0deda1871c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package git2go

import (
	"errors"
	"fmt"

	"gitlab.com/gitlab-org/gitaly/v16/internal/structerr"
	"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
)

// UnknownIndexError is an unspecified error that was produced by performing an invalid operation on the index.
type UnknownIndexError string

// Error returns the error message of the unknown index error.
func (err UnknownIndexError) Error() string { return string(err) }

// IndexErrorType specifies which of the known index error types has occurred.
type IndexErrorType uint

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
)

// IndexError is a well-defined error that was produced by performing an invalid operation on the index.
type IndexError struct {
	Path string
	Type IndexErrorType
}

// 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: %q", err.Path)
	default:
		panic(fmt.Sprintf("unhandled IndexErrorType: %v", err.Type))
	}
}

// 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
	}

	return &gitalypb.IndexError{
		Path:      []byte(err.Path),
		ErrorType: errType,
	}
}

// StructuredError returns the structured error.
func (err IndexError) StructuredError() structerr.Error {
	e := errors.New(err.Error())
	switch err.Type {
	case ErrDirectoryExists, ErrFileExists:
		return structerr.NewAlreadyExists("%w", e)
	case ErrDirectoryTraversal, ErrEmptyPath, ErrInvalidPath:
		return structerr.NewInvalidArgument("%w", e)
	case ErrFileNotFound:
		return structerr.NewNotFound("%w", e)
	default:
		return structerr.NewInternal("%w", e)
	}
}

// InvalidArgumentError is returned when an invalid argument is provided.
type InvalidArgumentError string

func (err InvalidArgumentError) Error() string { return string(err) }