-
Notifications
You must be signed in to change notification settings - Fork 8
/
rpc.go
103 lines (80 loc) · 3.13 KB
/
rpc.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
package rpc
import (
"fmt"
"github.com/cuigh/auxo/errors"
)
const PkgName = "auxo.net.rpc"
// A StatusCode is an unsigned 32-bit error code.
type StatusCode uint32
const (
// StatusOK is returned on success.
StatusOK StatusCode = 0
// StatusUnknown indicates an unknown error. An example of where this error may be returned is
// if a Status value received from another address space belongs to
// an error-space that is not known in this address space. Also
// errors raised by APIs that do not return enough error information
// may be converted to this error.
StatusUnknown StatusCode = 1
// StatusCanceled indicates the operation was canceled (typically by the caller).
StatusCanceled StatusCode = 2
// StatusDeadlineExceeded means operation expired before completion.
// For operations that change the state of the system, this error may be
// returned even if the operation has completed successfully. For
// example, a successful response from a server could have been delayed
// long enough for the deadline to expire.
StatusDeadlineExceeded StatusCode = 3
// StatusNodeUnavailable indicates no node is available for call.
StatusNodeUnavailable StatusCode = 4
// StatusNodeShutdown indicates Node is shut down.
StatusNodeShutdown StatusCode = 5
// StatusCodecNotRegistered indicates codec is not registered.
StatusCodecNotRegistered StatusCode = 6
// StatusServerClosed indicates server is closed.
StatusServerClosed StatusCode = 7
// StatusMethodNotFound indicates calling method is unregistered on server.
StatusMethodNotFound StatusCode = 8
// StatusInvalidArgument indicates client specified an invalid argument.
// Note that this differs from FailedPrecondition. It indicates arguments
// that are problematic regardless of the state of the system
// (e.g., a malformed file name).
StatusInvalidArgument StatusCode = 9
// StatusUnauthorized indicates client is unauthorized.
StatusUnauthorized StatusCode = 10
// StatusLoginFailed indicates client's login is failed.
StatusLoginFailed StatusCode = 11
// StatusNilResult indicates return value is nil.
StatusNilResult StatusCode = 12
)
var (
// ErrNodeUnavailable indicates no node is available for call.
ErrNodeUnavailable = NewError(StatusNodeUnavailable, "rpc: no node is available")
// ErrNodeShutdown indicates Node is shut down.
ErrNodeShutdown = NewError(StatusNodeShutdown, "rpc: node is shut down")
// ErrNilResult indicates the return value is nil.
ErrNilResult = NewError(StatusNilResult, "rpc: result is nil")
// ErrServerClosed indicates Server is closed.
ErrServerClosed = NewError(StatusServerClosed, "rpc: server closed")
)
func NewError(code StatusCode, format string, args ...interface{}) *errors.CodedError {
if len(args) == 0 {
return errors.Coded(int32(code), format)
}
return errors.Coded(int32(code), fmt.Sprintf(format, args...))
}
func StatusOf(err error) int32 {
if err == nil {
return int32(StatusOK)
} else if e, ok := err.(*errors.CodedError); ok {
return e.Code
}
return int32(StatusUnknown)
}
type AsyncError interface {
Wait() error
}
type asyncError struct {
error
}
func (ae asyncError) Wait() error {
return ae.error
}