-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
158 lines (140 loc) · 5.03 KB
/
Copy patherrors.go
File metadata and controls
158 lines (140 loc) · 5.03 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
// Copyright 2026-present the xvec project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package xvec
import "strings"
// ErrorCode classifies errors returned by xvec operations. The values and
// default messages match StatusCode in the C++ public API at commit 58375ff.
type ErrorCode uint32
const (
ErrorCodeOK ErrorCode = 0
ErrorCodeNotFound ErrorCode = 1
ErrorCodeAlreadyExists ErrorCode = 2
ErrorCodeInvalidArgument ErrorCode = 3
ErrorCodePermissionDenied ErrorCode = 4
ErrorCodeFailedPrecondition ErrorCode = 5
ErrorCodeResourceExhausted ErrorCode = 6
ErrorCodeUnavailable ErrorCode = 7
ErrorCodeInternal ErrorCode = 8
ErrorCodeNotSupported ErrorCode = 9
ErrorCodeUnknown ErrorCode = 10
)
var errorCodeNames = map[ErrorCode]string{
ErrorCodeOK: "OK",
ErrorCodeNotFound: "NOT_FOUND",
ErrorCodeAlreadyExists: "ALREADY_EXISTS",
ErrorCodeInvalidArgument: "INVALID_ARGUMENT",
ErrorCodePermissionDenied: "PERMISSION_DENIED",
ErrorCodeFailedPrecondition: "FAILED_PRECONDITION",
ErrorCodeResourceExhausted: "RESOURCE_EXHAUSTED",
ErrorCodeUnavailable: "UNAVAILABLE",
ErrorCodeInternal: "INTERNAL_ERROR",
ErrorCodeNotSupported: "NOT_SUPPORTED",
ErrorCodeUnknown: "UNKNOWN",
}
var errorCodeMessages = map[ErrorCode]string{
ErrorCodeOK: "OK",
ErrorCodeNotFound: "Not found",
ErrorCodeAlreadyExists: "Already exists",
ErrorCodeInvalidArgument: "Invalid argument",
ErrorCodePermissionDenied: "Permission denied",
ErrorCodeFailedPrecondition: "Failed precondition",
ErrorCodeResourceExhausted: "Resource exhausted",
ErrorCodeUnavailable: "Unavailable",
ErrorCodeInternal: "Internal error",
ErrorCodeNotSupported: "Not supported",
ErrorCodeUnknown: "Unknown error",
}
func (c ErrorCode) String() string { return enumName(errorCodeNames, c, "ErrorCode") }
// Valid reports whether c is a value defined by the public API.
func (c ErrorCode) Valid() bool { return enumValid(errorCodeNames, c) }
// DefaultMessage returns the stable default message for c.
func (c ErrorCode) DefaultMessage() string {
if message, ok := errorCodeMessages[c]; ok {
return message
}
return errorCodeMessages[ErrorCodeUnknown]
}
// Error is the structured error returned by xvec operations.
//
// Code is suitable for programmatic decisions. Op and Path identify the
// failed operation and collection path when available. Err retains the
// underlying error for errors.Is and errors.As traversal.
type Error struct {
Code ErrorCode
Op string
Path string
Message string
Err error
}
// Error formats the structured context without losing the underlying cause.
func (e *Error) Error() string {
if e == nil {
return "<nil>"
}
parts := []string{"xvec"}
if e.Op != "" {
parts = append(parts, e.Op)
}
if e.Path != "" {
parts = append(parts, e.Path)
}
detail := e.Message
if detail == "" {
detail = e.Code.DefaultMessage()
}
if e.Err != nil && e.Err.Error() != detail {
detail += ": " + e.Err.Error()
}
parts = append(parts, detail)
return strings.Join(parts, ": ")
}
// Unwrap returns the underlying cause.
func (e *Error) Unwrap() error {
if e == nil {
return nil
}
return e.Err
}
// Is makes errors.Is compare xvec errors by ErrorCode. An underlying cause is
// still considered by the standard library through Unwrap.
func (e *Error) Is(target error) bool {
if e == nil || target == nil {
return false
}
switch target := target.(type) {
case codeSentinel:
return e.Code == target.code
case *Error:
return target != nil && e.Code == target.Code
default:
return false
}
}
type codeSentinel struct {
code ErrorCode
}
func (e codeSentinel) Error() string { return "xvec: " + e.code.DefaultMessage() }
// Stable errors.Is targets for each non-success ErrorCode.
var (
ErrNotFound error = codeSentinel{ErrorCodeNotFound}
ErrAlreadyExists error = codeSentinel{ErrorCodeAlreadyExists}
ErrInvalidArgument error = codeSentinel{ErrorCodeInvalidArgument}
ErrPermissionDenied error = codeSentinel{ErrorCodePermissionDenied}
ErrFailedPrecondition error = codeSentinel{ErrorCodeFailedPrecondition}
ErrResourceExhausted error = codeSentinel{ErrorCodeResourceExhausted}
ErrUnavailable error = codeSentinel{ErrorCodeUnavailable}
ErrInternal error = codeSentinel{ErrorCodeInternal}
ErrNotSupported error = codeSentinel{ErrorCodeNotSupported}
ErrUnknown error = codeSentinel{ErrorCodeUnknown}
)