/
errors.ts
147 lines (144 loc) · 6.37 KB
/
errors.ts
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
146
147
import errorCodesPrefix from '../constants/errorCodesPrefix';
import addressUtils from '../utils/addressUtils';
import { Address } from './types';
import { XuNetwork } from '../constants/enums';
const codesPrefix = errorCodesPrefix.P2P;
const errorCodes = {
NODE_ALREADY_CONNECTED: codesPrefix.concat('.1'),
NOT_CONNECTED: codesPrefix.concat('.2'),
UNEXPECTED_NODE_PUB_KEY: codesPrefix.concat('.3'),
ATTEMPTED_CONNECTION_TO_SELF: codesPrefix.concat('.4'),
EXTERNAL_IP_UNRETRIEVABLE: codesPrefix.concat('.5'),
CONNECTION_RETRIES_MAX_PERIOD_EXCEEDED: codesPrefix.concat('.6'),
CONNECTION_RETRIES_REVOKED: codesPrefix.concat('.7'),
COULD_NOT_CONNECT: codesPrefix.concat('.7'),
NODE_UNKNOWN: codesPrefix.concat('.8'),
NODE_ALREADY_BANNED: codesPrefix.concat('.9'),
NODE_NOT_BANNED: codesPrefix.concat('.10'),
NODE_IS_BANNED: codesPrefix.concat('.11'),
ALREADY_CONNECTING: codesPrefix.concat('.12'),
RESPONSE_TIMEOUT: codesPrefix.concat('.13'),
AUTH_FAILURE_INVALID_TARGET: codesPrefix.concat('.14'),
AUTH_FAILURE_INVALID_SIGNATURE: codesPrefix.concat('.15'),
PARSER_INVALID_PACKET: codesPrefix.concat('.16'),
PARSER_UNKNOWN_PACKET_TYPE: codesPrefix.concat('.17'),
PARSER_DATA_INTEGRITY_ERR: codesPrefix.concat('.18'),
PARSER_MAX_BUFFER_SIZE_EXCEEDED: codesPrefix.concat('.19'),
FRAMER_MSG_NOT_ENCRYPTED: codesPrefix.concat('.`20'),
FRAMER_INVALID_NETWORK_MAGIC_VALUE: codesPrefix.concat('.21'),
FRAMER_INCOMPATIBLE_MSG_ORIGIN_NETWORK: codesPrefix.concat('.22'),
FRAMER_INVALID_MSG_LENGTH: codesPrefix.concat('.23'),
POOL_CLOSED: codesPrefix.concat('.24'),
};
const errors = {
NODE_ALREADY_CONNECTED: (nodePubKey: string, address: Address) => ({
message: `node ${nodePubKey} at ${addressUtils.toString(address)} already connected`,
code: errorCodes.NODE_ALREADY_CONNECTED,
}),
NOT_CONNECTED: (nodePubKey: string) => ({
message: `node (${nodePubKey}) is not connected`,
code: errorCodes.NOT_CONNECTED,
}),
UNEXPECTED_NODE_PUB_KEY: (nodePubKey: string, expectedNodePubKey: string, address: string) => ({
message: `node at ${address} sent pub key ${nodePubKey}, expected ${expectedNodePubKey}`,
code: errorCodes.UNEXPECTED_NODE_PUB_KEY,
}),
MALFORMED_VERSION: (address: string, peerVersion: string | undefined) => ({
message: `node at ${address} has version ${peerVersion}, which is malformed, should be in semantic format`,
code: errorCodes.UNEXPECTED_NODE_PUB_KEY,
}),
INCOMPATIBLE_VERSION: (address: string, minCompatibleVersion: string, peerVersion: string | undefined) => ({
message: `node at ${address} has version ${peerVersion}, expected minimum ${minCompatibleVersion}`,
code: errorCodes.UNEXPECTED_NODE_PUB_KEY,
}),
ATTEMPTED_CONNECTION_TO_SELF: {
message: 'cannot attempt connection to self',
code: errorCodes.ATTEMPTED_CONNECTION_TO_SELF,
},
EXTERNAL_IP_UNRETRIEVABLE: (err: Error) => ({
message: `could not retrieve external IP: ${err.message}`,
code: errorCodes.EXTERNAL_IP_UNRETRIEVABLE,
}),
CONNECTION_RETRIES_MAX_PERIOD_EXCEEDED: {
message: 'Connection retry attempts to peer exceeded maximum time allotment',
code: errorCodes.CONNECTION_RETRIES_MAX_PERIOD_EXCEEDED,
},
CONNECTION_RETRIES_REVOKED: {
message: 'Connection retry attempts to peer were revoked',
code: errorCodes.CONNECTION_RETRIES_REVOKED,
},
COULD_NOT_CONNECT: (address: Address, err: Error) => ({
message: `could not connect to peer at ${addressUtils.toString(address)}: ${err.message}`,
code: errorCodes.COULD_NOT_CONNECT,
}),
NODE_UNKNOWN: (nodePubKey: string) => ({
message: `node ${nodePubKey} is unknown`,
code: errorCodes.NODE_UNKNOWN,
}),
NODE_ALREADY_BANNED: (nodePubKey: string) => ({
message: `node ${nodePubKey} has already been banned`,
code: errorCodes.NODE_ALREADY_BANNED,
}),
NODE_NOT_BANNED: (nodePubKey: string) => ({
message: `node ${nodePubKey} has not been banned`,
code: errorCodes.NODE_NOT_BANNED,
}),
NODE_IS_BANNED: (nodePubKey: string) => ({
message: `could not connect to node ${nodePubKey} because it is banned`,
code: errorCodes.NODE_IS_BANNED,
}),
ALREADY_CONNECTING: (nodePubKey: string) => ({
message: `there is already an existing connection attempt to node ${nodePubKey}`,
code: errorCodes.ALREADY_CONNECTING,
}),
RESPONSE_TIMEOUT: (request: string) => ({
message: `response timeout (${request}) `,
code: errorCodes.RESPONSE_TIMEOUT,
}),
AUTH_FAILURE_INVALID_TARGET: (nodePubKey: string, target: string) => ({
message: `could not authenticate peer (${nodePubKey}): invalid target (${target})`,
code: errorCodes.AUTH_FAILURE_INVALID_TARGET,
}),
AUTH_FAILURE_INVALID_SIGNATURE: (nodePubKey: string) => ({
message: `could not authenticate peer (${nodePubKey}): invalid signature`,
code: errorCodes.AUTH_FAILURE_INVALID_SIGNATURE,
}),
PARSER_INVALID_PACKET: (packet: string) => ({
message: `parser: invalid packet: ${packet}`,
code: errorCodes.PARSER_INVALID_PACKET,
}),
PARSER_UNKNOWN_PACKET_TYPE: (packetType: string) => ({
message: `parser: unknown packet type: ${packetType}`,
code: errorCodes.PARSER_UNKNOWN_PACKET_TYPE,
}),
PARSER_DATA_INTEGRITY_ERR: (packet: string) => ({
message: `parser: packet data integrity error: ${packet}`,
code: errorCodes.PARSER_DATA_INTEGRITY_ERR,
}),
PARSER_MAX_BUFFER_SIZE_EXCEEDED: (size: number) => ({
message: `parser: max buffer size exceeded: ${size.toString()}`,
code: errorCodes.PARSER_MAX_BUFFER_SIZE_EXCEEDED,
}),
FRAMER_MSG_NOT_ENCRYPTED: {
message: 'framer: msg is not encrypted',
code: errorCodes.FRAMER_MSG_NOT_ENCRYPTED,
},
FRAMER_INVALID_NETWORK_MAGIC_VALUE: {
message: 'framer: msg has an invalid network magic value (might be encrypted)',
code: errorCodes.FRAMER_INVALID_NETWORK_MAGIC_VALUE,
},
FRAMER_INCOMPATIBLE_MSG_ORIGIN_NETWORK: (expected: XuNetwork, found: XuNetwork) => ({
message: `framer: incompatible msg origin network (expected: ${expected}, found: ${found})`,
code: errorCodes.FRAMER_INCOMPATIBLE_MSG_ORIGIN_NETWORK,
}),
FRAMER_INVALID_MSG_LENGTH: (expected: number, found: number) => ({
message: `framer: invalid msg length (expected: ${expected}, found: ${found})`,
code: errorCodes.FRAMER_INVALID_MSG_LENGTH,
}),
POOL_CLOSED: {
message: 'p2p pool is closed and not accepting new peers',
code: errorCodes.POOL_CLOSED,
},
};
export { errorCodes };
export default errors;