forked from coral-xyz/anchor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.ts
236 lines (216 loc) · 7.01 KB
/
error.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
export class IdlError extends Error {
constructor(message: string) {
super(message);
this.name = "IdlError";
}
}
// An error from a user defined program.
export class ProgramError extends Error {
constructor(readonly code: number, readonly msg: string, ...params: any[]) {
super(...params);
}
public static parse(
err: any,
idlErrors: Map<number, string>
): ProgramError | null {
// TODO: don't rely on the error string. web3.js should preserve the error
// code information instead of giving us an untyped string.
let components = err.toString().split("custom program error: ");
if (components.length !== 2) {
return null;
}
let errorCode: number;
try {
errorCode = parseInt(components[1]);
} catch (parseErr) {
return null;
}
// Parse user error.
let errorMsg = idlErrors.get(errorCode);
if (errorMsg !== undefined) {
return new ProgramError(errorCode, errorMsg, errorCode + ": " + errorMsg);
}
// Parse framework internal error.
errorMsg = LangErrorMessage.get(errorCode);
if (errorMsg !== undefined) {
return new ProgramError(errorCode, errorMsg, errorCode + ": " + errorMsg);
}
// Unable to parse the error. Just return the untranslated error.
return null;
}
public toString(): string {
return this.msg;
}
}
const LangErrorCode = {
// Instructions.
InstructionMissing: 100,
InstructionFallbackNotFound: 101,
InstructionDidNotDeserialize: 102,
InstructionDidNotSerialize: 103,
// IDL instructions.
IdlInstructionStub: 1000,
IdlInstructionInvalidProgram: 1001,
// Constraints.
ConstraintMut: 2000,
ConstraintHasOne: 2001,
ConstraintSigner: 2002,
ConstraintRaw: 2003,
ConstraintOwner: 2004,
ConstraintRentExempt: 2005,
ConstraintSeeds: 2006,
ConstraintExecutable: 2007,
ConstraintState: 2008,
ConstraintAssociated: 2009,
ConstraintAssociatedInit: 2010,
ConstraintClose: 2011,
ConstraintAddress: 2012,
ConstraintZero: 2013,
ConstraintTokenMint: 2014,
ConstraintTokenOwner: 2015,
ConstraintMintMintAuthority: 2016,
ConstraintMintFreezeAuthority: 2017,
ConstraintMintDecimals: 2018,
ConstraintSpace: 2019,
// Accounts.
AccountDiscriminatorAlreadySet: 3000,
AccountDiscriminatorNotFound: 3001,
AccountDiscriminatorMismatch: 3002,
AccountDidNotDeserialize: 3003,
AccountDidNotSerialize: 3004,
AccountNotEnoughKeys: 3005,
AccountNotMutable: 3006,
AccountOwnedByWrongProgram: 3007,
InvalidProgramId: 3008,
InvalidProgramExecutable: 3009,
AccountNotSigner: 3010,
AccountNotSystemOwned: 3011,
AccountNotInitialized: 3012,
AccountNotProgramData: 3013,
AccountNotAssociatedTokenAccount: 3014,
// State.
StateInvalidAddress: 4000,
// Miscellaneous
DeclaredProgramIdMismatch: 4100,
// Used for APIs that shouldn't be used anymore.
Deprecated: 5000,
};
const LangErrorMessage = new Map([
// Instructions.
[
LangErrorCode.InstructionMissing,
"8 byte instruction identifier not provided",
],
[
LangErrorCode.InstructionFallbackNotFound,
"Fallback functions are not supported",
],
[
LangErrorCode.InstructionDidNotDeserialize,
"The program could not deserialize the given instruction",
],
[
LangErrorCode.InstructionDidNotSerialize,
"The program could not serialize the given instruction",
],
// Idl instructions.
[
LangErrorCode.IdlInstructionStub,
"The program was compiled without idl instructions",
],
[
LangErrorCode.IdlInstructionInvalidProgram,
"The transaction was given an invalid program for the IDL instruction",
],
// Constraints.
[LangErrorCode.ConstraintMut, "A mut constraint was violated"],
[LangErrorCode.ConstraintHasOne, "A has_one constraint was violated"],
[LangErrorCode.ConstraintSigner, "A signer constraint was violated"],
[LangErrorCode.ConstraintRaw, "A raw constraint was violated"],
[LangErrorCode.ConstraintOwner, "An owner constraint was violated"],
[LangErrorCode.ConstraintRentExempt, "A rent exempt constraint was violated"],
[LangErrorCode.ConstraintSeeds, "A seeds constraint was violated"],
[LangErrorCode.ConstraintExecutable, "An executable constraint was violated"],
[LangErrorCode.ConstraintState, "A state constraint was violated"],
[LangErrorCode.ConstraintAssociated, "An associated constraint was violated"],
[
LangErrorCode.ConstraintAssociatedInit,
"An associated init constraint was violated",
],
[LangErrorCode.ConstraintClose, "A close constraint was violated"],
[LangErrorCode.ConstraintAddress, "An address constraint was violated"],
[LangErrorCode.ConstraintZero, "Expected zero account discriminant"],
[LangErrorCode.ConstraintTokenMint, "A token mint constraint was violated"],
[LangErrorCode.ConstraintTokenOwner, "A token owner constraint was violated"],
[
LangErrorCode.ConstraintMintMintAuthority,
"A mint mint authority constraint was violated",
],
[
LangErrorCode.ConstraintMintFreezeAuthority,
"A mint freeze authority constraint was violated",
],
[
LangErrorCode.ConstraintMintDecimals,
"A mint decimals constraint was violated",
],
[LangErrorCode.ConstraintSpace, "A space constraint was violated"],
// Accounts.
[
LangErrorCode.AccountDiscriminatorAlreadySet,
"The account discriminator was already set on this account",
],
[
LangErrorCode.AccountDiscriminatorNotFound,
"No 8 byte discriminator was found on the account",
],
[
LangErrorCode.AccountDiscriminatorMismatch,
"8 byte discriminator did not match what was expected",
],
[LangErrorCode.AccountDidNotDeserialize, "Failed to deserialize the account"],
[LangErrorCode.AccountDidNotSerialize, "Failed to serialize the account"],
[
LangErrorCode.AccountNotEnoughKeys,
"Not enough account keys given to the instruction",
],
[LangErrorCode.AccountNotMutable, "The given account is not mutable"],
[
LangErrorCode.AccountOwnedByWrongProgram,
"The given account is owned by a different program than expected",
],
[LangErrorCode.InvalidProgramId, "Program ID was not as expected"],
[LangErrorCode.InvalidProgramExecutable, "Program account is not executable"],
[LangErrorCode.AccountNotSigner, "The given account did not sign"],
[
LangErrorCode.AccountNotSystemOwned,
"The given account is not owned by the system program",
],
[
LangErrorCode.AccountNotInitialized,
"The program expected this account to be already initialized",
],
[
LangErrorCode.AccountNotProgramData,
"The given account is not a program data account",
],
[
LangErrorCode.AccountNotAssociatedTokenAccount,
"The given account is not the associated token account",
],
// State.
[
LangErrorCode.StateInvalidAddress,
"The given state account does not have the correct address",
],
// Miscellaneous
[
LangErrorCode.DeclaredProgramIdMismatch,
"The declared program id does not match the actual program id",
],
// Deprecated
[
LangErrorCode.Deprecated,
"The API being used is deprecated and should no longer be used",
],
]);