error.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. export class IdlError extends Error {
  2. constructor(message: string) {
  3. super(message);
  4. this.name = "IdlError";
  5. }
  6. }
  7. // An error from a user defined program.
  8. export class ProgramError extends Error {
  9. constructor(readonly code: number, readonly msg: string, ...params: any[]) {
  10. super(...params);
  11. }
  12. public static parse(
  13. err: any,
  14. idlErrors: Map<number, string>
  15. ): ProgramError | null {
  16. // TODO: don't rely on the error string. web3.js should preserve the error
  17. // code information instead of giving us an untyped string.
  18. let components = err.toString().split("custom program error: ");
  19. if (components.length !== 2) {
  20. return null;
  21. }
  22. let errorCode: number;
  23. try {
  24. errorCode = parseInt(components[1]);
  25. } catch (parseErr) {
  26. return null;
  27. }
  28. // Parse user error.
  29. let errorMsg = idlErrors.get(errorCode);
  30. if (errorMsg !== undefined) {
  31. return new ProgramError(errorCode, errorMsg, errorCode + ": " + errorMsg);
  32. }
  33. // Parse framework internal error.
  34. errorMsg = LangErrorMessage.get(errorCode);
  35. if (errorMsg !== undefined) {
  36. return new ProgramError(errorCode, errorMsg, errorCode + ": " + errorMsg);
  37. }
  38. // Unable to parse the error. Just return the untranslated error.
  39. return null;
  40. }
  41. public toString(): string {
  42. return this.msg;
  43. }
  44. }
  45. const LangErrorCode = {
  46. // Instructions.
  47. InstructionMissing: 100,
  48. InstructionFallbackNotFound: 101,
  49. InstructionDidNotDeserialize: 102,
  50. InstructionDidNotSerialize: 103,
  51. // IDL instructions.
  52. IdlInstructionStub: 1000,
  53. IdlInstructionInvalidProgram: 1001,
  54. // Constraints.
  55. ConstraintMut: 2000,
  56. ConstraintHasOne: 2001,
  57. ConstraintSigner: 2002,
  58. ConstraintRaw: 2003,
  59. ConstraintOwner: 2004,
  60. ConstraintRentExempt: 2005,
  61. ConstraintSeeds: 2006,
  62. ConstraintExecutable: 2007,
  63. ConstraintState: 2008,
  64. ConstraintAssociated: 2009,
  65. ConstraintAssociatedInit: 2010,
  66. ConstraintClose: 2011,
  67. ConstraintAddress: 2012,
  68. ConstraintZero: 2013,
  69. ConstraintTokenMint: 2014,
  70. ConstraintTokenOwner: 2015,
  71. ConstraintMintMintAuthority: 2016,
  72. ConstraintMintFreezeAuthority: 2017,
  73. ConstraintMintDecimals: 2018,
  74. ConstraintSpace: 2019,
  75. // Accounts.
  76. AccountDiscriminatorAlreadySet: 3000,
  77. AccountDiscriminatorNotFound: 3001,
  78. AccountDiscriminatorMismatch: 3002,
  79. AccountDidNotDeserialize: 3003,
  80. AccountDidNotSerialize: 3004,
  81. AccountNotEnoughKeys: 3005,
  82. AccountNotMutable: 3006,
  83. AccountOwnedByWrongProgram: 3007,
  84. InvalidProgramId: 3008,
  85. InvalidProgramExecutable: 3009,
  86. AccountNotSigner: 3010,
  87. AccountNotSystemOwned: 3011,
  88. AccountNotInitialized: 3012,
  89. AccountNotProgramData: 3013,
  90. AccountNotAssociatedTokenAccount: 3014,
  91. // State.
  92. StateInvalidAddress: 4000,
  93. // Used for APIs that shouldn't be used anymore.
  94. Deprecated: 5000,
  95. };
  96. const LangErrorMessage = new Map([
  97. // Instructions.
  98. [
  99. LangErrorCode.InstructionMissing,
  100. "8 byte instruction identifier not provided",
  101. ],
  102. [
  103. LangErrorCode.InstructionFallbackNotFound,
  104. "Fallback functions are not supported",
  105. ],
  106. [
  107. LangErrorCode.InstructionDidNotDeserialize,
  108. "The program could not deserialize the given instruction",
  109. ],
  110. [
  111. LangErrorCode.InstructionDidNotSerialize,
  112. "The program could not serialize the given instruction",
  113. ],
  114. // Idl instructions.
  115. [
  116. LangErrorCode.IdlInstructionStub,
  117. "The program was compiled without idl instructions",
  118. ],
  119. [
  120. LangErrorCode.IdlInstructionInvalidProgram,
  121. "The transaction was given an invalid program for the IDL instruction",
  122. ],
  123. // Constraints.
  124. [LangErrorCode.ConstraintMut, "A mut constraint was violated"],
  125. [LangErrorCode.ConstraintHasOne, "A has_one constraint was violated"],
  126. [LangErrorCode.ConstraintSigner, "A signer constraint was violated"],
  127. [LangErrorCode.ConstraintRaw, "A raw constraint was violated"],
  128. [LangErrorCode.ConstraintOwner, "An owner constraint was violated"],
  129. [LangErrorCode.ConstraintRentExempt, "A rent exempt constraint was violated"],
  130. [LangErrorCode.ConstraintSeeds, "A seeds constraint was violated"],
  131. [LangErrorCode.ConstraintExecutable, "An executable constraint was violated"],
  132. [LangErrorCode.ConstraintState, "A state constraint was violated"],
  133. [LangErrorCode.ConstraintAssociated, "An associated constraint was violated"],
  134. [
  135. LangErrorCode.ConstraintAssociatedInit,
  136. "An associated init constraint was violated",
  137. ],
  138. [LangErrorCode.ConstraintClose, "A close constraint was violated"],
  139. [LangErrorCode.ConstraintAddress, "An address constraint was violated"],
  140. [LangErrorCode.ConstraintZero, "Expected zero account discriminant"],
  141. [LangErrorCode.ConstraintTokenMint, "A token mint constraint was violated"],
  142. [LangErrorCode.ConstraintTokenOwner, "A token owner constraint was violated"],
  143. [
  144. LangErrorCode.ConstraintMintMintAuthority,
  145. "A mint mint authority constraint was violated",
  146. ],
  147. [
  148. LangErrorCode.ConstraintMintFreezeAuthority,
  149. "A mint freeze authority constraint was violated",
  150. ],
  151. [
  152. LangErrorCode.ConstraintMintDecimals,
  153. "A mint decimals constraint was violated",
  154. ],
  155. [LangErrorCode.ConstraintSpace, "A space constraint was violated"],
  156. // Accounts.
  157. [
  158. LangErrorCode.AccountDiscriminatorAlreadySet,
  159. "The account discriminator was already set on this account",
  160. ],
  161. [
  162. LangErrorCode.AccountDiscriminatorNotFound,
  163. "No 8 byte discriminator was found on the account",
  164. ],
  165. [
  166. LangErrorCode.AccountDiscriminatorMismatch,
  167. "8 byte discriminator did not match what was expected",
  168. ],
  169. [LangErrorCode.AccountDidNotDeserialize, "Failed to deserialize the account"],
  170. [LangErrorCode.AccountDidNotSerialize, "Failed to serialize the account"],
  171. [
  172. LangErrorCode.AccountNotEnoughKeys,
  173. "Not enough account keys given to the instruction",
  174. ],
  175. [LangErrorCode.AccountNotMutable, "The given account is not mutable"],
  176. [
  177. LangErrorCode.AccountOwnedByWrongProgram,
  178. "The given account is owned by a different program than expected",
  179. ],
  180. [LangErrorCode.InvalidProgramId, "Program ID was not as expected"],
  181. [LangErrorCode.InvalidProgramExecutable, "Program account is not executable"],
  182. [LangErrorCode.AccountNotSigner, "The given account did not sign"],
  183. [
  184. LangErrorCode.AccountNotSystemOwned,
  185. "The given account is not owned by the system program",
  186. ],
  187. [
  188. LangErrorCode.AccountNotInitialized,
  189. "The program expected this account to be already initialized",
  190. ],
  191. [
  192. LangErrorCode.AccountNotProgramData,
  193. "The given account is not a program data account",
  194. ],
  195. [
  196. LangErrorCode.AccountNotAssociatedTokenAccount,
  197. "The given account is not the associated token account",
  198. ],
  199. // State.
  200. [
  201. LangErrorCode.StateInvalidAddress,
  202. "The given state account does not have the correct address",
  203. ],
  204. // Misc.
  205. [
  206. LangErrorCode.Deprecated,
  207. "The API being used is deprecated and should no longer be used",
  208. ],
  209. ]);