forked from filecoin-project/go-jsonrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
53 lines (41 loc) · 865 Bytes
/
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
package jsonrpc
import (
"encoding/json"
"reflect"
)
const eTempWSError = -1111111
type RPCConnectionError struct {
err error
}
func (e *RPCConnectionError) Error() string {
return e.err.Error()
}
func (e *RPCConnectionError) Unwrap() error {
return e.err
}
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
}