2021-09-16 13:42:11 -07:00
|
|
|
export class IdlError extends Error {
|
|
|
|
constructor(message: string) {
|
|
|
|
super(message);
|
|
|
|
this.name = "IdlError";
|
|
|
|
}
|
|
|
|
}
|
2021-01-15 23:05:26 -08:00
|
|
|
|
|
|
|
// An error from a user defined program.
|
|
|
|
export class ProgramError extends Error {
|
|
|
|
constructor(readonly code: number, readonly msg: string, ...params: any[]) {
|
|
|
|
super(...params);
|
|
|
|
}
|
|
|
|
|
2021-06-09 13:02:50 -07:00
|
|
|
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) {
|
2021-10-01 09:55:59 -07:00
|
|
|
return new ProgramError(errorCode, errorMsg, errorCode + ": " + errorMsg);
|
2021-06-09 13:02:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse framework internal error.
|
|
|
|
errorMsg = LangErrorMessage.get(errorCode);
|
|
|
|
if (errorMsg !== undefined) {
|
2021-10-01 09:55:59 -07:00
|
|
|
return new ProgramError(errorCode, errorMsg, errorCode + ": " + errorMsg);
|
2021-06-09 13:02:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unable to parse the error. Just return the untranslated error.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-15 23:05:26 -08:00
|
|
|
public toString(): string {
|
|
|
|
return this.msg;
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 13:02:50 -07:00
|
|
|
|
|
|
|
const LangErrorCode = {
|
|
|
|
// Instructions.
|
|
|
|
InstructionMissing: 100,
|
|
|
|
InstructionFallbackNotFound: 101,
|
|
|
|
InstructionDidNotDeserialize: 102,
|
|
|
|
InstructionDidNotSerialize: 103,
|
|
|
|
|
|
|
|
// IDL instructions.
|
2021-12-08 13:50:08 -08:00
|
|
|
IdlInstructionStub: 1000,
|
|
|
|
IdlInstructionInvalidProgram: 1001,
|
2021-06-09 13:02:50 -07:00
|
|
|
|
|
|
|
// Constraints.
|
2021-12-08 13:50:08 -08:00
|
|
|
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,
|
2021-06-09 13:02:50 -07:00
|
|
|
|
|
|
|
// Accounts.
|
2021-12-08 13:50:08 -08:00
|
|
|
AccountDiscriminatorAlreadySet: 3000,
|
|
|
|
AccountDiscriminatorNotFound: 3001,
|
|
|
|
AccountDiscriminatorMismatch: 3002,
|
|
|
|
AccountDidNotDeserialize: 3003,
|
|
|
|
AccountDidNotSerialize: 3004,
|
|
|
|
AccountNotEnoughKeys: 3005,
|
|
|
|
AccountNotMutable: 3006,
|
2021-12-17 04:34:43 -08:00
|
|
|
AccountOwnedByWrongProgram: 3007,
|
2021-12-08 13:50:08 -08:00
|
|
|
InvalidProgramId: 3008,
|
|
|
|
InvalidProgramExecutable: 3009,
|
|
|
|
AccountNotSigner: 3010,
|
|
|
|
AccountNotSystemOwned: 3011,
|
|
|
|
AccountNotInitialized: 3012,
|
|
|
|
AccountNotProgramData: 3013,
|
2021-06-09 13:02:50 -07:00
|
|
|
// State.
|
2021-12-08 13:50:08 -08:00
|
|
|
StateInvalidAddress: 4000,
|
2021-06-09 13:02:50 -07:00
|
|
|
|
|
|
|
// Used for APIs that shouldn't be used anymore.
|
2021-12-08 13:50:08 -08:00
|
|
|
Deprecated: 5000,
|
2021-06-09 13:02:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
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"],
|
2021-07-03 11:58:23 -07:00
|
|
|
[LangErrorCode.ConstraintHasOne, "A has_one constraint was violated"],
|
2021-06-09 13:02:50 -07:00
|
|
|
[LangErrorCode.ConstraintSigner, "A signer constraint was violated"],
|
2021-08-18 10:35:29 -07:00
|
|
|
[LangErrorCode.ConstraintRaw, "A raw constraint was violated"],
|
2021-06-09 13:02:50 -07:00
|
|
|
[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",
|
|
|
|
],
|
2021-06-10 21:39:50 -07:00
|
|
|
[LangErrorCode.ConstraintClose, "A close constraint was violated"],
|
2021-06-27 13:17:05 -07:00
|
|
|
[LangErrorCode.ConstraintAddress, "An address constraint was violated"],
|
2021-12-08 13:50:08 -08:00
|
|
|
[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"],
|
2021-06-09 13:02:50 -07:00
|
|
|
|
|
|
|
// 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"],
|
|
|
|
[
|
2021-12-17 04:34:43 -08:00
|
|
|
LangErrorCode.AccountOwnedByWrongProgram,
|
|
|
|
"The given account is owned by a different program than expected",
|
2021-06-09 13:02:50 -07:00
|
|
|
],
|
2021-09-11 14:43:12 -07:00
|
|
|
[LangErrorCode.InvalidProgramId, "Program ID was not as expected"],
|
2021-10-30 09:45:06 -07:00
|
|
|
[LangErrorCode.InvalidProgramExecutable, "Program account is not executable"],
|
|
|
|
[LangErrorCode.AccountNotSigner, "The given account did not sign"],
|
2021-09-11 14:43:12 -07:00
|
|
|
[
|
2021-10-30 09:45:06 -07:00
|
|
|
LangErrorCode.AccountNotSystemOwned,
|
|
|
|
"The given account is not owned by the system program",
|
2021-09-11 14:43:12 -07:00
|
|
|
],
|
2021-11-18 07:42:07 -08:00
|
|
|
[
|
|
|
|
LangErrorCode.AccountNotInitialized,
|
|
|
|
"The program expected this account to be already initialized",
|
|
|
|
],
|
2021-12-05 11:14:16 -08:00
|
|
|
[
|
|
|
|
LangErrorCode.AccountNotProgramData,
|
|
|
|
"The given account is not a program data account",
|
|
|
|
],
|
2021-06-09 13:02:50 -07:00
|
|
|
|
|
|
|
// State.
|
|
|
|
[
|
|
|
|
LangErrorCode.StateInvalidAddress,
|
|
|
|
"The given state account does not have the correct address",
|
|
|
|
],
|
|
|
|
|
|
|
|
// Misc.
|
|
|
|
[
|
|
|
|
LangErrorCode.Deprecated,
|
|
|
|
"The API being used is deprecated and should no longer be used",
|
|
|
|
],
|
|
|
|
]);
|