-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
531 lines (451 loc) · 14 KB
/
Copy patherrors.go
File metadata and controls
531 lines (451 loc) · 14 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Package errors provides custom error types for the starmap system.
// These errors enable better error handling, programmatic error checking,
// and improved debugging throughout the application.
package errors
import (
"errors"
"fmt"
)
// New returns an error that formats as the given text.
// It's an alias for the standard library errors.New for convenience.
var New = errors.New
// Common sentinel errors for the starmap system.
var (
// ErrNotFound indicates that a requested resource was not found.
ErrNotFound = errors.New("not found")
// ErrAlreadyExists indicates that a resource already exists.
ErrAlreadyExists = errors.New("already exists")
// ErrInvalidInput indicates that provided input was invalid.
ErrInvalidInput = errors.New("invalid input")
// ErrAPIKeyRequired indicates that an API key is required but not provided.
ErrAPIKeyRequired = errors.New("API key required")
// ErrAPIKeyInvalid indicates that the provided API key is invalid.
ErrAPIKeyInvalid = errors.New("API key invalid")
// ErrProviderUnavailable indicates that a provider is temporarily unavailable.
ErrProviderUnavailable = errors.New("provider unavailable")
// ErrRateLimited indicates that the API rate limit has been exceeded.
ErrRateLimited = errors.New("rate limited")
// ErrTimeout indicates that an operation timed out.
ErrTimeout = errors.New("operation timed out")
// ErrCanceled indicates that an operation was canceled.
ErrCanceled = errors.New("operation canceled")
// ErrNotImplemented indicates that a feature is not yet implemented.
ErrNotImplemented = errors.New("not implemented")
// ErrReadOnly indicates an attempt to modify a read-only resource.
ErrReadOnly = errors.New("read only")
)
// NotFoundError represents an error when a resource is not found.
type NotFoundError struct {
Resource string
ID string
}
// Error implements the error interface.
func (e *NotFoundError) Error() string {
return fmt.Sprintf("%s with ID %s not found", e.Resource, e.ID)
}
// Is implements errors.Is support.
func (e *NotFoundError) Is(target error) bool {
return target == ErrNotFound
}
// NewNotFoundError creates a new NotFoundError.
func NewNotFoundError(resource, id string) *NotFoundError {
return &NotFoundError{Resource: resource, ID: id}
}
// ValidationError represents a validation failure.
type ValidationError struct {
Field string
Value any
Message string
}
// Error implements the error interface.
func (e *ValidationError) Error() string {
if e.Field != "" {
return fmt.Sprintf("validation failed for field %s: %s", e.Field, e.Message)
}
return fmt.Sprintf("validation failed: %s", e.Message)
}
// Is implements errors.Is support.
func (e *ValidationError) Is(target error) bool {
return target == ErrInvalidInput
}
// NewValidationError creates a new ValidationError.
func NewValidationError(field string, value any, message string) *ValidationError {
return &ValidationError{Field: field, Value: value, Message: message}
}
// APIError represents an error from a provider API.
type APIError struct {
Provider string // Provider ID as string
StatusCode int
Message string
Endpoint string
Err error
}
// Error implements the error interface.
func (e *APIError) Error() string {
if e.StatusCode != 0 {
return fmt.Sprintf("API error from %s (status %d): %s", e.Provider, e.StatusCode, e.Message)
}
return fmt.Sprintf("API error from %s: %s", e.Provider, e.Message)
}
// Unwrap implements errors.Unwrap.
func (e *APIError) Unwrap() error {
return e.Err
}
// Is implements errors.Is support.
func (e *APIError) Is(target error) bool {
if e.StatusCode == 429 {
return target == ErrRateLimited
}
if e.StatusCode >= 500 {
return target == ErrProviderUnavailable
}
return false
}
// NewAPIError creates a new APIError.
func NewAPIError(provider string, statusCode int, message string) *APIError {
return &APIError{
Provider: provider,
StatusCode: statusCode,
Message: message,
}
}
// ConfigError represents a configuration error.
type ConfigError struct {
Component string
Message string
Err error
}
// DependencyError indicates a required external dependency is missing.
type DependencyError struct {
Dependency string
Message string
}
// Error implements the error interface.
func (e *DependencyError) Error() string {
return fmt.Sprintf("dependency %s: %s", e.Dependency, e.Message)
}
// Error implements the error interface.
func (e *ConfigError) Error() string {
if e.Component != "" {
return fmt.Sprintf("configuration error in %s: %s", e.Component, e.Message)
}
return fmt.Sprintf("configuration error: %s", e.Message)
}
// Unwrap implements errors.Unwrap.
func (e *ConfigError) Unwrap() error {
return e.Err
}
// NewConfigError creates a new ConfigError.
func NewConfigError(component, message string, err error) *ConfigError {
return &ConfigError{
Component: component,
Message: message,
Err: err,
}
}
// MergeError represents an error during catalog merge operations.
type MergeError struct {
Source string
Target string
ConflictIDs []string
Err error
}
// Error implements the error interface.
func (e *MergeError) Error() string {
if len(e.ConflictIDs) > 0 {
return fmt.Sprintf("merge conflict between %s and %s for IDs: %v", e.Source, e.Target, e.ConflictIDs)
}
return fmt.Sprintf("merge error between %s and %s: %v", e.Source, e.Target, e.Err)
}
// Unwrap implements errors.Unwrap.
func (e *MergeError) Unwrap() error {
return e.Err
}
// NewMergeError creates a new MergeError.
func NewMergeError(source, target string, conflictIDs []string, err error) *MergeError {
return &MergeError{
Source: source,
Target: target,
ConflictIDs: conflictIDs,
Err: err,
}
}
// SyncError represents an error during sync operations.
type SyncError struct {
Provider string
Models []string
Err error
}
// Error implements the error interface.
func (e *SyncError) Error() string {
if len(e.Models) > 0 {
return fmt.Sprintf("sync error for provider %s (affected models: %v): %v", e.Provider, e.Models, e.Err)
}
return fmt.Sprintf("sync error for provider %s: %v", e.Provider, e.Err)
}
// Unwrap implements errors.Unwrap.
func (e *SyncError) Unwrap() error {
return e.Err
}
// NewSyncError creates a new SyncError.
func NewSyncError(provider string, models []string, err error) *SyncError {
return &SyncError{
Provider: provider,
Models: models,
Err: err,
}
}
// Helper functions for error checking
// IsNotFound checks if an error is a not found error.
func IsNotFound(err error) bool {
return errors.Is(err, ErrNotFound)
}
// IsAlreadyExists checks if an error is an already exists error.
func IsAlreadyExists(err error) bool {
return errors.Is(err, ErrAlreadyExists)
}
// IsValidationError checks if an error is a validation error.
func IsValidationError(err error) bool {
return errors.Is(err, ErrInvalidInput)
}
// IsAPIKeyError checks if an error is related to API keys.
func IsAPIKeyError(err error) bool {
return errors.Is(err, ErrAPIKeyRequired) || errors.Is(err, ErrAPIKeyInvalid)
}
// IsRateLimited checks if an error is a rate limit error.
func IsRateLimited(err error) bool {
return errors.Is(err, ErrRateLimited)
}
// IsTimeout checks if an error is a timeout error.
func IsTimeout(err error) bool {
return errors.Is(err, ErrTimeout)
}
// IsCanceled checks if an error is a cancellation error.
func IsCanceled(err error) bool {
return errors.Is(err, ErrCanceled)
}
// IsProviderUnavailable checks if an error indicates provider unavailability.
func IsProviderUnavailable(err error) bool {
return errors.Is(err, ErrProviderUnavailable)
}
// ParseError represents an error when parsing data formats.
type ParseError struct {
Format string // "json", "yaml", "toml", etc.
File string
Line int
Column int
Message string
Err error
}
// Error implements the error interface.
func (e *ParseError) Error() string {
if e.File != "" && e.Line > 0 {
return fmt.Sprintf("parse error in %s at %s:%d:%d: %s", e.Format, e.File, e.Line, e.Column, e.Message)
}
if e.File != "" {
return fmt.Sprintf("parse error in %s file %s: %s", e.Format, e.File, e.Message)
}
return fmt.Sprintf("%s parse error: %s", e.Format, e.Message)
}
// Unwrap implements errors.Unwrap.
func (e *ParseError) Unwrap() error {
return e.Err
}
// NewParseError creates a new ParseError.
func NewParseError(format, file string, message string, err error) *ParseError {
return &ParseError{
Format: format,
File: file,
Message: message,
Err: err,
}
}
// IOError represents an error during I/O operations.
type IOError struct {
Operation string // "read", "write", "create", "delete", "open", "close"
Path string
Message string
Err error
}
// Error implements the error interface.
func (e *IOError) Error() string {
if e.Path != "" {
return fmt.Sprintf("IO error during %s of %s: %s", e.Operation, e.Path, e.Message)
}
return fmt.Sprintf("IO error during %s: %s", e.Operation, e.Message)
}
// Unwrap implements errors.Unwrap.
func (e *IOError) Unwrap() error {
return e.Err
}
// NewIOError creates a new IOError.
func NewIOError(operation, path string, err error) *IOError {
message := ""
if err != nil {
message = err.Error()
}
return &IOError{
Operation: operation,
Path: path,
Message: message,
Err: err,
}
}
// ResourceError represents an error during resource operations.
type ResourceError struct {
Operation string // "create", "update", "delete", "fetch"
Resource string // "catalog", "provider", "model", "author"
ID string
Message string
Err error
}
// Error implements the error interface.
func (e *ResourceError) Error() string {
if e.ID != "" {
return fmt.Sprintf("failed to %s %s %s: %s", e.Operation, e.Resource, e.ID, e.Message)
}
return fmt.Sprintf("failed to %s %s: %s", e.Operation, e.Resource, e.Message)
}
// Unwrap implements errors.Unwrap.
func (e *ResourceError) Unwrap() error {
return e.Err
}
// NewResourceError creates a new ResourceError.
func NewResourceError(operation, resource, id string, err error) *ResourceError {
message := ""
if err != nil {
message = err.Error()
}
return &ResourceError{
Operation: operation,
Resource: resource,
ID: id,
Message: message,
Err: err,
}
}
// AuthenticationError represents an authentication/authorization error.
type AuthenticationError struct {
Provider string
Method string // "api_key", "oauth", "basic", etc.
Message string
Err error
}
// Error implements the error interface.
func (e *AuthenticationError) Error() string {
if e.Provider != "" {
return fmt.Sprintf("authentication error for %s (%s): %s", e.Provider, e.Method, e.Message)
}
return fmt.Sprintf("authentication error (%s): %s", e.Method, e.Message)
}
// Unwrap implements errors.Unwrap.
func (e *AuthenticationError) Unwrap() error {
return e.Err
}
// Is implements errors.Is support.
func (e *AuthenticationError) Is(target error) bool {
return target == ErrAPIKeyRequired || target == ErrAPIKeyInvalid
}
// NewAuthenticationError creates a new AuthenticationError.
func NewAuthenticationError(provider, method, message string, err error) *AuthenticationError {
return &AuthenticationError{
Provider: provider,
Method: method,
Message: message,
Err: err,
}
}
// TimeoutError represents an operation timeout.
type TimeoutError struct {
Operation string
Duration string
Message string
}
// Error implements the error interface.
func (e *TimeoutError) Error() string {
if e.Duration != "" {
return fmt.Sprintf("operation %s timed out after %s: %s", e.Operation, e.Duration, e.Message)
}
return fmt.Sprintf("operation %s timed out: %s", e.Operation, e.Message)
}
// Is implements errors.Is support.
func (e *TimeoutError) Is(target error) bool {
return target == ErrTimeout
}
// NewTimeoutError creates a new TimeoutError.
func NewTimeoutError(operation, duration, message string) *TimeoutError {
return &TimeoutError{
Operation: operation,
Duration: duration,
Message: message,
}
}
// ProcessError represents an error from an external process or command.
type ProcessError struct {
Operation string // What operation was being performed
Command string // The command that was executed
Output string // Stdout/stderr output from the process
ExitCode int // Exit code if available
Err error // Underlying error
}
// Error implements the error interface.
func (e *ProcessError) Error() string {
if e.Output != "" {
return fmt.Sprintf("process error during %s (command: %s): %v\nOutput: %s", e.Operation, e.Command, e.Err, e.Output)
}
return fmt.Sprintf("process error during %s (command: %s): %v", e.Operation, e.Command, e.Err)
}
// Unwrap implements errors.Unwrap.
func (e *ProcessError) Unwrap() error {
return e.Err
}
// NewProcessError creates a new ProcessError.
func NewProcessError(operation, command, output string, err error) *ProcessError {
return &ProcessError{
Operation: operation,
Command: command,
Output: output,
Err: err,
}
}
// Helper wrapping functions for common patterns
// WrapValidation wraps an error as a ValidationError.
func WrapValidation(field string, err error) error {
if err == nil {
return nil
}
return &ValidationError{Field: field, Message: err.Error()}
}
// WrapIO wraps an error as an IOError.
func WrapIO(operation, path string, err error) error {
if err == nil {
return nil
}
return NewIOError(operation, path, err)
}
// WrapResource wraps an error as a ResourceError.
func WrapResource(operation, resource, id string, err error) error {
if err == nil {
return nil
}
return NewResourceError(operation, resource, id, err)
}
// WrapParse wraps an error as a ParseError.
func WrapParse(format, file string, err error) error {
if err == nil {
return nil
}
return NewParseError(format, file, err.Error(), err)
}
// WrapAPI wraps an error as an APIError.
func WrapAPI(provider string, statusCode int, err error) error {
if err == nil {
return nil
}
return &APIError{
Provider: provider,
StatusCode: statusCode,
Message: err.Error(),
Err: err,
}
}