-
Notifications
You must be signed in to change notification settings - Fork 19
/
errors.go
145 lines (125 loc) · 7.82 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package api
import (
"context"
"errors"
"code.vegaprotocol.io/vega/libs/jsonrpc"
)
const (
// Network error codes are errors that comes from the network itself and its
// nodes.
// ErrorCodeNodeRequestFailed refers to the inability of the program to
// talk to the network nodes.
ErrorCodeNodeRequestFailed jsonrpc.ErrorCode = 1000
// Application error codes are programmatic errors that comes from the API
// itself and its "business" rules. It ranges from 1000 to 1999, included.
// ErrorCodeRequestNotPermitted refers a request made by a third-party application
// that is not permitted to do.
ErrorCodeRequestNotPermitted jsonrpc.ErrorCode = 2000
// Client error codes are errors that results from the client. It ranges
// from 2000 to 2999, included.
// ErrorCodeConnectionHasBeenClosed refers to an interruption of the service triggered
// by the client.
ErrorCodeConnectionHasBeenClosed jsonrpc.ErrorCode = 3000
// ErrorCodeRequestHasBeenRejected refers to an explicit rejection of a request by the
// client.
ErrorCodeRequestHasBeenRejected jsonrpc.ErrorCode = 3001
)
var (
ErrCannotRotateKeysOnIsolatedWallet = errors.New("cannot rotate keys on an isolated wallet")
ErrChainIDIsRequired = errors.New("the chain ID is required")
ErrClientRejectedTheRequest = errors.New("the client rejected the request")
ErrConnectionClosed = errors.New("the client closed the connection")
ErrConnectionTokenIsRequired = errors.New("the connection token is required")
ErrCouldNotConnectToWallet = errors.New("could not connect to the wallet")
ErrCouldNotGetLastBlockInformation = errors.New("could not get information about the last block on the network")
ErrCouldNotRequestPermissions = errors.New("could not request permissions")
ErrCouldNotSendTransaction = errors.New("could not send transaction")
ErrCouldNotSignTransaction = errors.New("could not sign transaction")
ErrCurrentPublicKeyDoesNotExist = errors.New("the current public key does not exist")
ErrCurrentPublicKeyIsRequired = errors.New("the next public key is required")
ErrEnactmentBlockHeightIsRequired = errors.New("the enactment block height is required")
ErrEnactmentBlockHeightMustBeGreaterThanSubmissionOne = errors.New("the enactment block height must be greater than the submission one")
ErrEncodedTransactionIsNotValidBase64String = errors.New("the encoded transaction is not a valid base-64 string")
ErrEncodedTransactionIsRequired = errors.New("the encoded transaction is required")
ErrHostnameIsRequired = errors.New("the hostname is required")
ErrNextAndCurrentPublicKeysCannotBeTheSame = errors.New("the next and current public keys cannot be the same")
ErrNextPublicKeyDoesNotExist = errors.New("the next public key does not exist")
ErrNextPublicKeyIsRequired = errors.New("the next public key is required")
ErrNextPublicKeyIsTainted = errors.New("the next public key is tainted")
ErrNoHealthyNodeAvailable = errors.New("no healthy node available")
ErrParamsDoNotMatch = errors.New("the params do not match expected ones")
ErrParamsRequired = errors.New("the params are required")
ErrPassphraseIsRequired = errors.New("the passphrase is required")
ErrPublicKeyDoesNotExist = errors.New("the public key does not exist")
ErrPublicKeyIsNotAllowedToBeUsed = errors.New("the public key is not allowed to be used")
ErrPublicKeyIsRequired = errors.New("the public key is required")
ErrReadAccessOnPublicKeysRequired = errors.New(`a "read" access on public keys is required`)
ErrRecoveryPhraseIsRequired = errors.New("the recovery phrase is required")
ErrRequestInterrupted = errors.New("the request has been interrupted")
ErrRequestedPermissionsAreRequired = errors.New("the requested permissions are required")
ErrSendingModeCannotBeTypeUnspecified = errors.New(`the sending mode can't be "TYPE_UNSPECIFIED"`)
ErrSendingModeIsRequired = errors.New("the sending mode is required")
ErrSubmissionBlockHeightIsRequired = errors.New("the submission block height is required")
ErrTransactionFailed = errors.New("the transaction failed")
ErrWalletAlreadyExists = errors.New("a wallet with the same name already exists")
ErrWalletDoesNotExist = errors.New("the wallet does not exist")
ErrWalletIsRequired = errors.New("the wallet is required")
ErrWalletVersionIsRequired = errors.New("the wallet version is required")
ErrNetworkDoesNotExist = errors.New("the network does not exist")
ErrNetworkIsRequired = errors.New("the network is required")
ErrNetworkSourceIsRequired = errors.New("a network source is required")
ErrMultipleNetworkSources = errors.New("network sources are mutually exclusive")
ErrNetworkAlreadyExists = errors.New("a network with the same name already exists")
)
func applicationError(code jsonrpc.ErrorCode, err error) *jsonrpc.ErrorDetails {
if code <= -32000 {
panic("client error code should be greater than -32000")
}
return jsonrpc.NewCustomError(code, "Application error", err)
}
func clientError(code jsonrpc.ErrorCode, err error) *jsonrpc.ErrorDetails {
if code <= -32000 {
panic("client error code should be greater than -32000")
}
return jsonrpc.NewCustomError(code, "Client error", err)
}
func networkError(code jsonrpc.ErrorCode, err error) *jsonrpc.ErrorDetails {
if code <= -32000 {
panic("network error code should be greater than -32000")
}
return jsonrpc.NewCustomError(code, "Network error", err)
}
func invalidParams(err error) *jsonrpc.ErrorDetails {
return jsonrpc.NewInvalidParams(err)
}
func requestNotPermittedError(err error) *jsonrpc.ErrorDetails {
return applicationError(ErrorCodeRequestNotPermitted, err)
}
func connectionClosedError(err error) *jsonrpc.ErrorDetails {
return clientError(ErrorCodeConnectionHasBeenClosed, err)
}
func requestInterruptedError(err error) *jsonrpc.ErrorDetails {
return jsonrpc.NewServerError(jsonrpc.ErrorCodeRequestHasBeenInterrupted, err)
}
func clientRejectionError() *jsonrpc.ErrorDetails {
return clientError(ErrorCodeRequestHasBeenRejected, ErrClientRejectedTheRequest)
}
func internalError(err error) *jsonrpc.ErrorDetails {
return jsonrpc.NewInternalError(err)
}
// handleRequestFlowError is a generic function that build the appropriate
// API error response based on the underlying error.
// If none of them matches, the error handling is delegating to the caller.
func handleRequestFlowError(ctx context.Context, traceID string, pipeline Pipeline, err error) *jsonrpc.ErrorDetails {
if errors.Is(err, ErrConnectionClosed) {
// This error means the client closed the connection by stopping the
// client front-end application. As a result, there is no notification
// to be sent to the client.
return connectionClosedError(err)
}
if errors.Is(err, ErrRequestInterrupted) {
pipeline.NotifyError(ctx, traceID, ServerError, err)
return requestInterruptedError(err)
}
return nil
}