Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
117 lines (100 loc) · 3.14 KB
/
Copy patherrors.go
File metadata and controls
117 lines (100 loc) · 3.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
//go:build cgo && !purego
package zvec
/*
#include "zvec/c_api.h"
#include <stdlib.h>
*/
import "C"
import (
"errors"
"fmt"
"runtime"
"unsafe"
)
// ErrorCode represents a zvec error code.
type ErrorCode int
const (
OK ErrorCode = 0
NotFound ErrorCode = 1
AlreadyExists ErrorCode = 2
InvalidArgument ErrorCode = 3
PermissionDenied ErrorCode = 4
FailedPrecondition ErrorCode = 5
ResourceExhausted ErrorCode = 6
Unavailable ErrorCode = 7
InternalError ErrorCode = 8
NotSupported ErrorCode = 9
Unknown ErrorCode = 10
)
// String returns the string representation of the error code.
func (c ErrorCode) String() string {
cStr := C.zvec_error_code_to_string(C.zvec_error_code_t(c))
return C.GoString(cStr)
}
// Error represents a zvec error with code and message.
type Error struct {
Code ErrorCode
Message string
}
func (e *Error) Error() string {
return fmt.Sprintf("zvec error [%s]: %s", e.Code, e.Message)
}
// Sentinel errors for common error codes.
var (
ErrNotFound = &Error{Code: NotFound, Message: "resource not found"}
ErrAlreadyExists = &Error{Code: AlreadyExists, Message: "resource already exists"}
ErrInvalidArgument = &Error{Code: InvalidArgument, Message: "invalid argument"}
ErrPermissionDenied = &Error{Code: PermissionDenied, Message: "permission denied"}
ErrFailedPrecondition = &Error{Code: FailedPrecondition, Message: "failed precondition"}
ErrResourceExhausted = &Error{Code: ResourceExhausted, Message: "resource exhausted"}
ErrUnavailable = &Error{Code: Unavailable, Message: "unavailable"}
ErrInternalError = &Error{Code: InternalError, Message: "internal error"}
ErrNotSupported = &Error{Code: NotSupported, Message: "not supported"}
ErrUnknown = &Error{Code: Unknown, Message: "unknown error"}
)
// IsNotFound checks if the error is a not found error.
func IsNotFound(err error) bool {
var zvecErr *Error
return errors.As(err, &zvecErr) && zvecErr.Code == NotFound
}
// IsAlreadyExists checks if the error is an already exists error.
func IsAlreadyExists(err error) bool {
var zvecErr *Error
return errors.As(err, &zvecErr) && zvecErr.Code == AlreadyExists
}
// IsInvalidArgument checks if the error is an invalid argument error.
func IsInvalidArgument(err error) bool {
var zvecErr *Error
return errors.As(err, &zvecErr) && zvecErr.Code == InvalidArgument
}
// toError converts a C error code to a Go error.
// Returns nil if the error code is ZVEC_OK.
func toError(code C.zvec_error_code_t) error {
if code == C.ZVEC_OK {
return nil
}
var cMsg *C.char
C.zvec_get_last_error(&cMsg)
defer func() {
if cMsg != nil {
C.zvec_free(unsafe.Pointer(cMsg))
}
}()
message := "unknown error"
if cMsg != nil {
message = C.GoString(cMsg)
}
return &Error{
Code: ErrorCode(code),
Message: message,
}
}
// lockErrorThread keeps a native call and its subsequent thread-local error
// lookup on the same OS thread.
func lockErrorThread() func() {
runtime.LockOSThread()
return runtime.UnlockOSThread
}
func invalidArgumentError(message string) error {
return &Error{Code: InvalidArgument, Message: message}
}