-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
144 lines (127 loc) · 3.98 KB
/
Copy patherrors.go
File metadata and controls
144 lines (127 loc) · 3.98 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
package avp
import (
"errors"
"fmt"
)
// Error codes
const (
ErrCodeAuthenticationFailed = "AUTHENTICATION_FAILED"
ErrCodeSessionError = "SESSION_ERROR"
ErrCodeSessionExpired = "SESSION_EXPIRED"
ErrCodeSessionTerminated = "SESSION_TERMINATED"
ErrCodeSessionNotFound = "SESSION_NOT_FOUND"
ErrCodeSecretNotFound = "SECRET_NOT_FOUND"
ErrCodeInvalidName = "INVALID_NAME"
ErrCodeInvalidWorkspace = "INVALID_WORKSPACE"
ErrCodeCapacityExceeded = "CAPACITY_EXCEEDED"
ErrCodeBackendError = "BACKEND_ERROR"
ErrCodeBackendUnavailable = "BACKEND_UNAVAILABLE"
ErrCodeRateLimitExceeded = "RATE_LIMIT_EXCEEDED"
ErrCodeValueTooLarge = "VALUE_TOO_LARGE"
ErrCodeEncryptionError = "ENCRYPTION_ERROR"
ErrCodeIntegrityError = "INTEGRITY_ERROR"
)
// AVPError represents an AVP protocol error.
type AVPError struct {
Code string
Message string
Detail map[string]interface{}
}
func (e *AVPError) Error() string {
return fmt.Sprintf("%s: %s", e.Code, e.Message)
}
// Is implements error comparison.
func (e *AVPError) Is(target error) bool {
t, ok := target.(*AVPError)
if !ok {
return false
}
return e.Code == t.Code
}
// Common errors
var (
ErrAuthenticationFailed = &AVPError{Code: ErrCodeAuthenticationFailed, Message: "Authentication failed"}
ErrSessionExpired = &AVPError{Code: ErrCodeSessionExpired, Message: "Session has expired"}
ErrSessionTerminated = &AVPError{Code: ErrCodeSessionTerminated, Message: "Session was terminated"}
ErrRateLimitExceeded = &AVPError{Code: ErrCodeRateLimitExceeded, Message: "Rate limit exceeded"}
)
// NewSessionNotFoundError creates a new session not found error.
func NewSessionNotFoundError(sessionID string) *AVPError {
return &AVPError{
Code: ErrCodeSessionNotFound,
Message: fmt.Sprintf("Session not found: %s", sessionID),
}
}
// NewSecretNotFoundError creates a new secret not found error.
func NewSecretNotFoundError(name string) *AVPError {
return &AVPError{
Code: ErrCodeSecretNotFound,
Message: fmt.Sprintf("Secret '%s' not found", name),
}
}
// NewInvalidNameError creates a new invalid name error.
func NewInvalidNameError(name string) *AVPError {
return &AVPError{
Code: ErrCodeInvalidName,
Message: fmt.Sprintf("Invalid name: %s", name),
}
}
// NewInvalidWorkspaceError creates a new invalid workspace error.
func NewInvalidWorkspaceError(workspace string) *AVPError {
return &AVPError{
Code: ErrCodeInvalidWorkspace,
Message: fmt.Sprintf("Invalid workspace: %s", workspace),
}
}
// NewCapacityExceededError creates a new capacity exceeded error.
func NewCapacityExceededError(message string) *AVPError {
return &AVPError{
Code: ErrCodeCapacityExceeded,
Message: message,
}
}
// NewBackendError creates a new backend error.
func NewBackendError(message string) *AVPError {
return &AVPError{
Code: ErrCodeBackendError,
Message: message,
}
}
// NewValueTooLargeError creates a new value too large error.
func NewValueTooLargeError(maxSize int) *AVPError {
return &AVPError{
Code: ErrCodeValueTooLarge,
Message: fmt.Sprintf("Value exceeds maximum size of %d bytes", maxSize),
}
}
// NewEncryptionError creates a new encryption error.
func NewEncryptionError(message string) *AVPError {
return &AVPError{
Code: ErrCodeEncryptionError,
Message: message,
}
}
// IsSecretNotFound checks if the error is a secret not found error.
func IsSecretNotFound(err error) bool {
var avpErr *AVPError
if errors.As(err, &avpErr) {
return avpErr.Code == ErrCodeSecretNotFound
}
return false
}
// IsSessionExpired checks if the error is a session expired error.
func IsSessionExpired(err error) bool {
var avpErr *AVPError
if errors.As(err, &avpErr) {
return avpErr.Code == ErrCodeSessionExpired
}
return false
}
// IsSessionNotFound checks if the error is a session not found error.
func IsSessionNotFound(err error) bool {
var avpErr *AVPError
if errors.As(err, &avpErr) {
return avpErr.Code == ErrCodeSessionNotFound
}
return false
}