-
Notifications
You must be signed in to change notification settings - Fork 92
/
errors.go
65 lines (52 loc) · 1.08 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
package jsonrpc
import (
"encoding/json"
"errors"
"reflect"
)
const eTempWSError = -1111111
type RPCConnectionError struct {
err error
}
func (e *RPCConnectionError) Error() string {
if e.err != nil {
return e.err.Error()
}
return "RPCConnectionError"
}
func (e *RPCConnectionError) Unwrap() error {
if e.err != nil {
return e.err
}
return errors.New("RPCConnectionError")
}
type Errors struct {
byType map[reflect.Type]ErrorCode
byCode map[ErrorCode]reflect.Type
}
type ErrorCode int
const FirstUserCode = 2
func NewErrors() Errors {
return Errors{
byType: map[reflect.Type]ErrorCode{},
byCode: map[ErrorCode]reflect.Type{
-1111111: reflect.TypeOf(&RPCConnectionError{}),
},
}
}
func (e *Errors) Register(c ErrorCode, typ interface{}) {
rt := reflect.TypeOf(typ).Elem()
if !rt.Implements(errorType) {
panic("can't register non-error types")
}
e.byType[rt] = c
e.byCode[c] = rt
}
type marshalable interface {
json.Marshaler
json.Unmarshaler
}
type RPCErrorCodec interface {
FromJSONRPCError(JSONRPCError) error
ToJSONRPCError() (JSONRPCError, error)
}