-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.go
More file actions
98 lines (80 loc) · 2.51 KB
/
error.go
File metadata and controls
98 lines (80 loc) · 2.51 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
package probe
import (
"fmt"
"strings"
)
// ErrorType represents different categories of errors
type ErrorType string
const (
ErrorTypeValidation ErrorType = "validation"
ErrorTypeExecution ErrorType = "execution"
ErrorTypeConfiguration ErrorType = "configuration"
ErrorTypeNetwork ErrorType = "network"
ErrorTypeFile ErrorType = "file"
ErrorTypeAction ErrorType = "action"
ErrorTypeDependency ErrorType = "dependency"
)
// ProbeError is the base error type with context information
type ProbeError struct {
Type ErrorType
Operation string
Message string
Cause error
Context map[string]any
}
func (e *ProbeError) Error() string {
if e.Cause != nil {
return fmt.Sprintf("%s error in %s: %s (caused by: %v)", e.Type, e.Operation, e.Message, e.Cause)
}
return fmt.Sprintf("%s error in %s: %s", e.Type, e.Operation, e.Message)
}
func (e *ProbeError) Unwrap() error {
return e.Cause
}
func (e *ProbeError) Is(target error) bool {
if t, ok := target.(*ProbeError); ok {
return e.Type == t.Type
}
return false
}
// NewProbeError creates a new ProbeError
func NewProbeError(errorType ErrorType, operation, message string, cause error) *ProbeError {
return &ProbeError{
Type: errorType,
Operation: operation,
Message: message,
Cause: cause,
Context: make(map[string]any),
}
}
// WithContext adds context information to the error
func (e *ProbeError) WithContext(key string, value any) *ProbeError {
e.Context[key] = value
return e
}
// ValidationError for validation-specific errors
type ValidationError struct {
messages []string
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("validation error:\n%s", strings.Join(e.messages, "\n"))
}
func (e *ValidationError) HasError() bool {
return len(e.messages) > 0
}
func (e *ValidationError) AddMessage(s string) {
e.messages = append(e.messages, s)
}
// Convenience functions for creating specific error types
func NewExecutionError(operation, message string, cause error) *ProbeError {
return NewProbeError(ErrorTypeExecution, operation, message, cause)
}
func NewConfigurationError(operation, message string, cause error) *ProbeError {
return NewProbeError(ErrorTypeConfiguration, operation, message, cause)
}
func NewFileError(operation, message string, cause error) *ProbeError {
return NewProbeError(ErrorTypeFile, operation, message, cause)
}
func NewActionError(operation, message string, cause error) *ProbeError {
return NewProbeError(ErrorTypeAction, operation, message, cause)
}