-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtierror.go
More file actions
123 lines (108 loc) · 3.34 KB
/
tierror.go
File metadata and controls
123 lines (108 loc) · 3.34 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
package thingsdb
import (
"github.com/vmihailenco/msgpack/v5"
)
// ErrorCode known by ThingsDB
type ErrorCode int
const (
// UnpackError - invalid qpack data when create a new Error
UnpackError ErrorCode = ErrorCode(-200)
/*
* Error below are a direct mapping to error codes from ThingsDB
* and are in the range from 96-127.
*/
// CancelledError - operation is cancelled before completion
CancelledError ErrorCode = ErrorCode(-64)
// OperationError - operation is not valid in the current context
OperationError ErrorCode = ErrorCode(-63)
// NumArgumentsError - wrong number of arguments
NumArgumentsError ErrorCode = ErrorCode(-62)
// TypeError - object of inappropriate type
TypeError ErrorCode = ErrorCode(-61)
// ValueError - object has the right type but an inappropriate value
ValueError ErrorCode = ErrorCode(-60)
// OverflowError - interger overflow
OverflowError ErrorCode = ErrorCode(-59)
// ZeroDivError - division or module by zero
ZeroDivError ErrorCode = ErrorCode(-58)
// MaxQuotaError - max quota is reached
MaxQuotaError ErrorCode = ErrorCode(-57)
// AuthError - authentication error
AuthError ErrorCode = ErrorCode(-56)
// ForbiddenError - forbidden (access denied)
ForbiddenError ErrorCode = ErrorCode(-55)
// LookupError - requested resource not found
LookupError ErrorCode = ErrorCode(-54)
// BadRequestError - unable to handle request due to invalid data
BadRequestError ErrorCode = ErrorCode(-53)
// SyntaxError - syntax error in query
SyntaxError ErrorCode = ErrorCode(-52)
// NodeError - node is temporary unable to handle the request
NodeError ErrorCode = ErrorCode(-51)
// AssertionError - assertion statement has failed
AssertionError ErrorCode = ErrorCode(-50)
// ResultTooLargeError - result too large
ResultTooLargeError = ErrorCode(-6)
// RequestTimeoutError - request timed out
RequestTimeoutError = ErrorCode(-5)
// RequestCancelError - request is cancelled
RequestCancelError = ErrorCode(-4)
// WriteUVError - cannot write to socket
WriteUVError ErrorCode = ErrorCode(-3)
// MemoryError - memory allocation error
MemoryError ErrorCode = ErrorCode(-2)
// InternalError - internal error
InternalError ErrorCode = ErrorCode(-1)
)
// TiError can be returned by the ThingsDB package.
type TiError struct {
msg string
code ErrorCode
}
// NewError returns a pointer to a new Error.
func NewTiError(msg string, code ErrorCode) *TiError {
return &TiError{
msg: msg,
code: code,
}
}
// NewTiErrorFromByte returns a pointer to a new Error from msgpack byte data.
func NewTiErrorFromByte(b []byte) *TiError {
var result interface{}
err := msgpack.Unmarshal(b, &result)
if err != nil {
return &TiError{
msg: err.Error(),
code: UnpackError,
}
}
errMap, ok := result.(map[string]interface{})
if !ok {
return &TiError{
msg: "expected a map",
code: UnpackError,
}
}
msg, ok := errMap["error_msg"].(string)
if !ok {
return &TiError{
msg: "expected `error_msg` of type `string`",
code: UnpackError,
}
}
errCode, ok := errMap["error_code"].(int8)
if !ok {
return &TiError{
msg: "expected `error_code` of type `int8`",
code: UnpackError,
}
}
return &TiError{
msg: msg,
code: ErrorCode(errCode),
}
}
// Error returns the error msg.
func (e *TiError) Error() string { return e.msg }
// Code returns the error type.
func (e *TiError) Code() ErrorCode { return e.code }