-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
106 lines (94 loc) · 2.69 KB
/
errors.go
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
package nxsugar
import (
"fmt"
nexus "github.com/nayarsystems/nxgo/nxcore"
)
const (
// nxgo
ErrParse = -32700
ErrInvalidRequest = -32600
ErrInternal = -32603
ErrInvalidParams = -32602
ErrMethodNotFound = -32601
ErrTtlExpired = -32011
ErrPermissionDenied = -32010
ErrConnClosed = -32007
ErrLockNotOwned = -32006
ErrUserExists = -32005
ErrInvalidUser = -32004
ErrInvalidPipe = -32003
ErrInvalidTask = -32002
ErrCancel = -32001
ErrTimeout = -32000
ErrNotSupported = -32099
// nxsugar
ErrTestingMethodNotProvided = -20000
ErrPactNotDefined = -20001
)
var ErrStr = map[int]string{
// nxgo
ErrParse: "Parse error",
ErrInvalidRequest: "Invalid request",
ErrMethodNotFound: "Method not found",
ErrInvalidParams: "Invalid params",
ErrInternal: "Internal error",
ErrTimeout: "Timeout",
ErrCancel: "Cancel",
ErrInvalidTask: "Invalid task",
ErrInvalidPipe: "Invalid pipe",
ErrInvalidUser: "Invalid user",
ErrUserExists: "User already exists",
ErrPermissionDenied: "Permission denied",
ErrTtlExpired: "TTL expired",
ErrLockNotOwned: "Lock not owned",
ErrConnClosed: "Connection is closed",
ErrNotSupported: "Not supported",
// nxsugar
ErrTestingMethodNotProvided: "Testing method not provided",
ErrPactNotDefined: "Pact not defined for provided input",
}
type JsonRpcErr struct {
Cod int `json:"code"`
Mess string `json:"message"`
Dat interface{} `json:"data,omitempty"`
}
func (e *JsonRpcErr) Error() string {
return fmt.Sprintf("[%d] %s", e.Cod, e.Mess)
}
func (e *JsonRpcErr) Code() int {
return e.Cod
}
func (e *JsonRpcErr) Data() interface{} {
return e.Dat
}
// NewJsonRpcErr creates new JSON-RPC error.
//
// code is the JSON-RPC error code.
// message is optional in case of well known error code (negative values).
// data is an optional extra info object.
func NewJsonRpcErr(code int, message string, data interface{}) *JsonRpcErr {
if code < 0 {
if message != "" {
message = fmt.Sprintf("%s:[%s]", ErrStr[code], message)
} else {
message = ErrStr[code]
}
}
return &JsonRpcErr{Cod: code, Mess: message, Dat: data}
}
/*
IsNexusErr checks whether an error is from nexus (it matches the type *nxcore.JsonRpcErr).
*/
func IsNexusErr(err error) bool {
_, ok := err.(*nexus.JsonRpcErr)
return ok
}
/*
IsNexusErrCode checks whether an error is from nexus (it matches the type *nxcore.JsonRpcErr) and matches the provided code.
*/
func IsNexusErrCode(err error, code int) bool {
if nexusErr, ok := err.(*nexus.JsonRpcErr); ok {
return nexusErr.Cod == code
}
return false
}