-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
122 lines (100 loc) · 4.65 KB
/
Copy patherrors.go
File metadata and controls
122 lines (100 loc) · 4.65 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
package encx
import (
"errors"
"fmt"
"github.com/hengadev/encx/internal/types"
)
var (
// High-level service errors
ErrKMSUnavailable = errors.New("KMS service unavailable")
ErrSecretStorageUnavailable = errors.New("secret storage unavailable")
ErrKeyRotationRequired = errors.New("key rotation required")
ErrInvalidConfiguration = errors.New("invalid configuration")
ErrAuthenticationFailed = errors.New("authentication failed")
ErrEncryptionFailed = errors.New("encryption failed")
ErrDecryptionFailed = errors.New("decryption failed")
ErrDatabaseUnavailable = errors.New("database unavailable")
// Crypto errors
ErrUninitializedPepper = errors.New("pepper value appears to be uninitialized (all zeros)")
// Field errors
ErrMissingField = errors.New("missing required field")
ErrMissingTargetField = errors.New("missing required target field")
ErrInvalidFieldType = errors.New("invalid field type")
ErrUnsupportedType = errors.New("unsupported type")
// Conversion errors
ErrTypeConversion = errors.New("type conversion failed")
ErrNilPointer = errors.New("nil pointer encountered")
// Operation errors
ErrOperationFailed = errors.New("operation failed")
ErrInvalidFormat = errors.New("invalid format")
// Metadata validation errors
ErrMissingKEKAlias = errors.New("KEK alias is required")
ErrMissingGeneratorVersion = errors.New("generator version is required")
)
func NewUninitalizedPepperError() error {
return ErrUninitializedPepper
}
func NewMissingFieldError(fieldName string, action types.Action) error {
return fmt.Errorf("%w: '%s' is required to %s", ErrMissingTargetField, fieldName, action)
}
func NewMissingTargetFieldError(fieldName string, targetFieldName string, action types.Action) error {
return fmt.Errorf("%w: '%s' is required to %s %s", ErrMissingTargetField, targetFieldName, action, fieldName)
}
func NewInvalidFieldTypeError(fieldName string, expectedType, actualType string, action types.Action) error {
return fmt.Errorf("%w: '%s' must be of type %s to %s, got %s",
ErrInvalidFieldType, fieldName, expectedType, action, actualType)
}
func NewUnsupportedTypeError(fieldName string, typeName string, action types.Action) error {
return fmt.Errorf("%w: field '%s' has unsupported type %s for %s operation",
ErrUnsupportedType, fieldName, typeName, action)
}
func NewTypeConversionError(fieldName string, typeName string, action types.Action) error {
return fmt.Errorf("%w: failed to convert field '%s' to %s for %s operation",
ErrTypeConversion, fieldName, typeName, action)
}
func NewNilPointerError(fieldName string, action types.Action) error {
return fmt.Errorf("%w: field %s is a nil pointer and cannot be processed for %s operation", ErrNilPointer, fieldName, action)
}
func NewOperationFailedError(fieldName string, action types.Action, details string) error {
if details != "" {
return fmt.Errorf("%w: %s operation failed for field '%s': %s",
ErrOperationFailed, action, fieldName, details)
}
return fmt.Errorf("%w: %s operation failed for field '%s'",
ErrOperationFailed, action, fieldName)
}
func NewInvalidFormatError(fieldName string, formatName string, action types.Action) error {
return fmt.Errorf("%w: field '%s' has invalid format for %s operation, expected %s format",
ErrInvalidFormat, fieldName, action, formatName)
}
// IsRetryableError returns true if the error represents a transient failure that might succeed on retry.
func IsRetryableError(err error) bool {
return errors.Is(err, ErrKMSUnavailable) ||
errors.Is(err, ErrSecretStorageUnavailable) ||
errors.Is(err, ErrDatabaseUnavailable)
}
// IsConfigurationError returns true if the error represents a configuration problem.
func IsConfigurationError(err error) bool {
return errors.Is(err, ErrInvalidConfiguration) ||
errors.Is(err, ErrUninitializedPepper) ||
errors.Is(err, ErrMissingField) ||
errors.Is(err, ErrMissingTargetField) ||
errors.Is(err, ErrInvalidFieldType) ||
errors.Is(err, ErrUnsupportedType)
}
// IsAuthError returns true if the error represents an authentication problem.
func IsAuthError(err error) bool {
return errors.Is(err, ErrAuthenticationFailed)
}
// IsOperationError returns true if the error represents a failure during encryption/decryption operations.
func IsOperationError(err error) bool {
return errors.Is(err, ErrEncryptionFailed) ||
errors.Is(err, ErrDecryptionFailed) ||
errors.Is(err, ErrOperationFailed)
}
// IsValidationError returns true if the error represents a data validation problem.
func IsValidationError(err error) bool {
return errors.Is(err, ErrInvalidFormat) ||
errors.Is(err, ErrTypeConversion) ||
errors.Is(err, ErrNilPointer)
}