-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherrors.go
More file actions
311 lines (255 loc) · 12.3 KB
/
Copy patherrors.go
File metadata and controls
311 lines (255 loc) · 12.3 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package nanogit
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/grafana/nanogit/protocol"
"github.com/grafana/nanogit/protocol/client"
"github.com/grafana/nanogit/protocol/hash"
)
var (
// ErrObjectNotFound is returned when a requested Git object cannot be found in the repository.
// This error should only be used with errors.Is() for comparison, not for type assertions.
ErrObjectNotFound = errors.New("object not found")
// ErrObjectAlreadyExists is returned when attempting to create a Git object that already exists.
// This error should only be used with errors.Is() for comparison, not for type assertions.
ErrObjectAlreadyExists = errors.New("object already exists")
// ErrUnexpectedObjectType is returned when a Git object has a different type than expected.
// This error should only be used with errors.Is() for comparison, not for type assertions.
ErrUnexpectedObjectType = errors.New("unexpected object type")
// ErrNothingToPush is returned when attempting to push changes but no objects have been staged.
// This error should only be used with errors.Is() for comparison, not for type assertions.
ErrNothingToPush = errors.New("nothing to push")
// ErrNothingToCommit is returned when attempting to commit but no changes have been staged.
// This error should only be used with errors.Is() for comparison, not for type assertions.
ErrNothingToCommit = errors.New("nothing to commit")
// ErrUnexpectedObjectCount is returned when the number of objects returned by the server is unexpected.
// This error should only be used with errors.Is() for comparison, not for type assertions.
ErrUnexpectedObjectCount = errors.New("unexpected object count")
// ErrEmptyCommitMessage is returned when attempting to create a commit with an empty message.
// This error should only be used with errors.Is() for comparison, not for type assertions.
ErrEmptyCommitMessage = errors.New("empty commit message")
// ErrEmptyPath is returned when a path is empty.
// This error should only be used with errors.Is() for comparison, not for type assertions.
ErrEmptyPath = errors.New("empty path")
// ErrEmptyRefName is returned when a ref name is empty.
// This error should only be used with errors.Is() for comparison, not for type assertions.
ErrEmptyRefName = errors.New("empty ref name")
// ErrInvalidAuthor is returned when author information is invalid.
// This error should only be used with errors.Is() for comparison, not for type assertions.
ErrInvalidAuthor = errors.New("invalid author information")
// ErrServerUnavailable is returned when the Git server is unavailable (HTTP 5xx status codes).
// This error should only be used with errors.Is() for comparison, not for type assertions.
// It is re-exported from the protocol/client package to avoid import cycles.
ErrServerUnavailable = client.ErrServerUnavailable
// ErrUnauthorized is returned when authentication fails (HTTP 401).
// This error should only be used with errors.Is() for comparison, not for type assertions.
// It is re-exported from the protocol/client package to avoid import cycles.
ErrUnauthorized = client.ErrUnauthorized
// ErrPermissionDenied is returned when the user lacks permission for the operation (HTTP 403).
// This error should only be used with errors.Is() for comparison, not for type assertions.
// It is re-exported from the protocol/client package to avoid import cycles.
ErrPermissionDenied = client.ErrPermissionDenied
// ErrRepositoryNotFound is returned when the repository does not exist (HTTP 404).
// This error should only be used with errors.Is() for comparison, not for type assertions.
// It is re-exported from the protocol/client package to avoid import cycles.
ErrRepositoryNotFound = client.ErrRepositoryNotFound
)
// ObjectNotFoundError provides structured information about a Git object that was not found.
type ObjectNotFoundError struct {
// ObjectID is the hash of the object that was not found.
ObjectID hash.Hash
}
// Error implements the error interface.
func (e *ObjectNotFoundError) Error() string {
return "object " + e.ObjectID.String() + " not found"
}
// Unwrap enables errors.Is() compatibility with ErrObjectNotFound
func (e *ObjectNotFoundError) Unwrap() error {
return ErrObjectNotFound
}
// NewObjectNotFoundError creates a new ObjectNotFoundError with the specified object ID.
func NewObjectNotFoundError(objectID hash.Hash) *ObjectNotFoundError {
return &ObjectNotFoundError{
ObjectID: objectID,
}
}
// ObjectAlreadyExistsError provides structured information about a Git object that already exists.
type ObjectAlreadyExistsError struct {
// ObjectID is the hash of the object that already exists.
ObjectID hash.Hash
}
// Error implements the error interface.
func (e *ObjectAlreadyExistsError) Error() string {
return "object " + e.ObjectID.String() + " already exists"
}
// Unwrap enables errors.Is() compatibility with ErrObjectAlreadyExists
func (e *ObjectAlreadyExistsError) Unwrap() error {
return ErrObjectAlreadyExists
}
// NewObjectAlreadyExistsError creates a new ObjectAlreadyExistsError with the specified object ID.
func NewObjectAlreadyExistsError(objectID hash.Hash) *ObjectAlreadyExistsError {
return &ObjectAlreadyExistsError{
ObjectID: objectID,
}
}
// UnexpectedObjectCountError provides structured information about a Git object with an unexpected count.
type UnexpectedObjectCountError struct {
// ExpectedCount is the number of objects the caller requested.
ExpectedCount int
// ActualCount is the number of objects the server returned.
ActualCount int
// Objects are the objects the server returned.
Objects []*protocol.PackfileObject
}
// Error implements the error interface.
func (e *UnexpectedObjectCountError) Error() string {
objectNames := make([]string, 0, len(e.Objects))
for _, obj := range e.Objects {
objectNames = append(objectNames, fmt.Sprintf("%s/%s", obj.Type.String(), obj.Hash.String()))
}
return "unexpected object count: expected " + strconv.Itoa(e.ExpectedCount) + " but got " + strconv.Itoa(e.ActualCount) + " objects: " + strings.Join(objectNames, ", ")
}
// Unwrap enables errors.Is() compatibility with ErrUnexpectedObjectCount
func (e *UnexpectedObjectCountError) Unwrap() error {
return ErrUnexpectedObjectCount
}
// NewUnexpectedObjectCountError creates a new UnexpectedObjectCountError with the specified details.
func NewUnexpectedObjectCountError(expectedCount int, objects []*protocol.PackfileObject) *UnexpectedObjectCountError {
return &UnexpectedObjectCountError{
ExpectedCount: expectedCount,
ActualCount: len(objects),
Objects: objects,
}
}
// UnexpectedObjectTypeError provides structured information about a Git object with an unexpected type.
type UnexpectedObjectTypeError struct {
// ObjectID is the hash of the mismatched object.
ObjectID hash.Hash
// ExpectedType is the object type the caller requested.
ExpectedType protocol.ObjectType
// ActualType is the object type the server returned.
ActualType protocol.ObjectType
}
// Error implements the error interface.
func (e *UnexpectedObjectTypeError) Error() string {
return "object " + e.ObjectID.String() + " has unexpected type " + e.ActualType.String() + " (expected " + e.ExpectedType.String() + ")"
}
// Unwrap enables errors.Is() compatibility with ErrUnexpectedObjectType
func (e *UnexpectedObjectTypeError) Unwrap() error {
return ErrUnexpectedObjectType
}
// NewUnexpectedObjectTypeError creates a new UnexpectedObjectTypeError with the specified details.
func NewUnexpectedObjectTypeError(objectID hash.Hash, expectedType, actualType protocol.ObjectType) *UnexpectedObjectTypeError {
return &UnexpectedObjectTypeError{
ObjectID: objectID,
ExpectedType: expectedType,
ActualType: actualType,
}
}
// PathNotFoundError provides structured information about a Git path that was not found.
type PathNotFoundError struct {
// Path is the repository-relative path that was not found.
Path string
}
// Error implements the error interface.
func (e *PathNotFoundError) Error() string {
return "path not found: " + e.Path
}
// Unwrap enables errors.Is() compatibility with ErrObjectNotFound
func (e *PathNotFoundError) Unwrap() error {
return ErrObjectNotFound
}
// NewPathNotFoundError creates a new PathNotFoundError with the specified path.
func NewPathNotFoundError(path string) *PathNotFoundError {
return &PathNotFoundError{
Path: path,
}
}
// RefNotFoundError provides structured information about a Git reference that was not found.
type RefNotFoundError struct {
// RefName is the full name of the reference that was not found (e.g. "refs/heads/main").
RefName string
}
// Error implements the error interface.
func (e *RefNotFoundError) Error() string {
return "reference not found: " + e.RefName
}
// Unwrap enables errors.Is() compatibility with ErrObjectNotFound
func (e *RefNotFoundError) Unwrap() error {
return ErrObjectNotFound
}
// NewRefNotFoundError creates a new RefNotFoundError with the specified reference name.
func NewRefNotFoundError(refName string) *RefNotFoundError {
return &RefNotFoundError{
RefName: refName,
}
}
// RefAlreadyExistsError provides structured information about a Git reference that already exists.
type RefAlreadyExistsError struct {
// RefName is the full name of the reference that already exists (e.g. "refs/heads/main").
RefName string
}
// Error implements the error interface.
func (e *RefAlreadyExistsError) Error() string {
return "reference already exists: " + e.RefName
}
// Unwrap enables errors.Is() compatibility with ErrObjectAlreadyExists
func (e *RefAlreadyExistsError) Unwrap() error {
return ErrObjectAlreadyExists
}
// NewRefAlreadyExistsError creates a new RefAlreadyExistsError with the specified reference name.
func NewRefAlreadyExistsError(refName string) *RefAlreadyExistsError {
return &RefAlreadyExistsError{
RefName: refName,
}
}
// AuthorError provides structured information about invalid author information.
type AuthorError struct {
// Field is the author field that failed validation (e.g. "name", "email").
Field string
// Reason describes why the field is invalid.
Reason string
}
// Error implements the error interface.
func (e *AuthorError) Error() string {
return fmt.Sprintf("invalid author %s: %s", e.Field, e.Reason)
}
// Unwrap enables errors.Is() compatibility with ErrInvalidAuthor
func (e *AuthorError) Unwrap() error {
return ErrInvalidAuthor
}
// NewAuthorError creates a new AuthorError with the specified details.
func NewAuthorError(field, reason string) *AuthorError {
return &AuthorError{
Field: field,
Reason: reason,
}
}
// ServerUnavailableError provides structured information about a Git server that is unavailable.
// It is re-exported from the protocol/client package to avoid import cycles.
type ServerUnavailableError = client.ServerUnavailableError
// NewServerUnavailableError creates a new ServerUnavailableError with the specified status code and underlying error.
// It is re-exported from the protocol/client package to avoid import cycles.
var NewServerUnavailableError = client.NewServerUnavailableError
// UnauthorizedError provides structured information about an authentication failure.
type UnauthorizedError = client.UnauthorizedError
// PermissionDeniedError provides structured information about a permission denial.
type PermissionDeniedError = client.PermissionDeniedError
// RepositoryNotFoundError provides structured information about a repository not found error.
type RepositoryNotFoundError = client.RepositoryNotFoundError
// NewUnauthorizedError constructs the typed error returned when the server
// rejects the request with HTTP 401 (authentication failed) for the given
// HTTP method and Git endpoint.
// It is re-exported from the protocol/client package to avoid import cycles.
var NewUnauthorizedError = client.NewUnauthorizedError
// NewPermissionDeniedError constructs the typed error returned when the
// server rejects the request with HTTP 403 (authenticated but not allowed)
// for the given HTTP method and Git endpoint.
// It is re-exported from the protocol/client package to avoid import cycles.
var NewPermissionDeniedError = client.NewPermissionDeniedError
// NewRepositoryNotFoundError constructs the typed error returned when the
// server responds with HTTP 404 because the repository does not exist.
// It is re-exported from the protocol/client package to avoid import cycles.
var NewRepositoryNotFoundError = client.NewRepositoryNotFoundError