forked from libp2p/go-libp2p-gorpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
121 lines (103 loc) · 2.96 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package rpc
import "errors"
// responseErr is an enum type for providing error type
// information over the wire between rpc server and client.
type responseErr int
const (
// nonRPCErr is an error that hasn't arisen from the gorpc package.
nonRPCErr responseErr = iota
// serverErr is an error that has arisen on the server side.
serverErr
// clientErr is an error that has arisen on the client side.
clientErr
// authorizationErr is an error that has arisen because client doesn't
// have permissions to make the given rpc request
authorizationErr
)
// serverError indicates that error originated in server
// specific code.
type serverError struct {
msg string
}
func (s *serverError) Error() string {
return s.msg
}
// newServerError wraps an error in the serverError type.
func newServerError(err error) error {
return &serverError{err.Error()}
}
// clientError indicates that error originated in client
// specific code.
type clientError struct {
msg string
}
func (c *clientError) Error() string {
return c.msg
}
// newClientError wraps an error in the clientError type.
func newClientError(err error) error {
return &clientError{err.Error()}
}
// authorizationError indicates that error originated because of client not having
// permissions to make given rpc request
type authorizationError struct {
msg string
}
func (a *authorizationError) Error() string {
return a.msg
}
// newAuthorizationError wraps an error in the authorizationError type.
func newAuthorizationError(err error) error {
return &authorizationError{err.Error()}
}
// responseError converts an responseErr and error message string
// into the appropriate error type.
func responseError(errType responseErr, errMsg string) error {
switch errType {
case serverErr:
return &serverError{errMsg}
case clientErr:
return &clientError{errMsg}
case authorizationErr:
return &authorizationError{errMsg}
default:
return errors.New(errMsg)
}
}
// responseErrorType determines whether an error is of either
// serverError or clientError type and returns the appropriate
// responseErr value.
func responseErrorType(err error) responseErr {
switch err.(type) {
case *serverError:
return serverErr
case *clientError:
return clientErr
case *authorizationError:
return authorizationErr
default:
return nonRPCErr
}
}
// IsRPCError returns whether an error is either a serverError
// or clientError.
func IsRPCError(err error) bool {
switch err.(type) {
case *serverError, *clientError, *authorizationError:
return true
default:
return false
}
}
// IsServerError returns whether an error is serverError.
func IsServerError(err error) bool {
return responseErrorType(err) == serverErr
}
// IsClientError returns whether an error is clientError.
func IsClientError(err error) bool {
return responseErrorType(err) == clientErr
}
// IsAuthorizationError returns whether an error is authorizationError.
func IsAuthorizationError(err error) bool {
return responseErrorType(err) == authorizationErr
}