error.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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: 120,
  53. IdlInstructionInvalidProgram: 121,
  54. // Constraints.
  55. ConstraintMut: 140,
  56. ConstraintHasOne: 141,
  57. ConstraintSigner: 142,
  58. ConstraintRaw: 143,
  59. ConstraintOwner: 144,
  60. ConstraintRentExempt: 145,
  61. ConstraintSeeds: 146,
  62. ConstraintExecutable: 147,
  63. ConstraintState: 148,
  64. ConstraintAssociated: 149,
  65. ConstraintAssociatedInit: 150,
  66. ConstraintClose: 151,
  67. ConstraintAddress: 152,
  68. // Accounts.
  69. AccountDiscriminatorAlreadySet: 160,
  70. AccountDiscriminatorNotFound: 161,
  71. AccountDiscriminatorMismatch: 162,
  72. AccountDidNotDeserialize: 163,
  73. AccountDidNotSerialize: 164,
  74. AccountNotEnoughKeys: 165,
  75. AccountNotMutable: 166,
  76. AccountNotProgramOwned: 167,
  77. InvalidProgramId: 168,
  78. InvalidProgramExecutable: 169,
  79. AccountNotSigner: 170,
  80. AccountNotSystemOwned: 171,
  81. AccountNotInitialized: 172,
  82. // State.
  83. StateInvalidAddress: 180,
  84. // Used for APIs that shouldn't be used anymore.
  85. Deprecated: 299,
  86. };
  87. const LangErrorMessage = new Map([
  88. // Instructions.
  89. [
  90. LangErrorCode.InstructionMissing,
  91. "8 byte instruction identifier not provided",
  92. ],
  93. [
  94. LangErrorCode.InstructionFallbackNotFound,
  95. "Fallback functions are not supported",
  96. ],
  97. [
  98. LangErrorCode.InstructionDidNotDeserialize,
  99. "The program could not deserialize the given instruction",
  100. ],
  101. [
  102. LangErrorCode.InstructionDidNotSerialize,
  103. "The program could not serialize the given instruction",
  104. ],
  105. // Idl instructions.
  106. [
  107. LangErrorCode.IdlInstructionStub,
  108. "The program was compiled without idl instructions",
  109. ],
  110. [
  111. LangErrorCode.IdlInstructionInvalidProgram,
  112. "The transaction was given an invalid program for the IDL instruction",
  113. ],
  114. // Constraints.
  115. [LangErrorCode.ConstraintMut, "A mut constraint was violated"],
  116. [LangErrorCode.ConstraintHasOne, "A has_one constraint was violated"],
  117. [LangErrorCode.ConstraintSigner, "A signer constraint was violated"],
  118. [LangErrorCode.ConstraintRaw, "A raw constraint was violated"],
  119. [LangErrorCode.ConstraintOwner, "An owner constraint was violated"],
  120. [LangErrorCode.ConstraintRentExempt, "A rent exempt constraint was violated"],
  121. [LangErrorCode.ConstraintSeeds, "A seeds constraint was violated"],
  122. [LangErrorCode.ConstraintExecutable, "An executable constraint was violated"],
  123. [LangErrorCode.ConstraintState, "A state constraint was violated"],
  124. [LangErrorCode.ConstraintAssociated, "An associated constraint was violated"],
  125. [
  126. LangErrorCode.ConstraintAssociatedInit,
  127. "An associated init constraint was violated",
  128. ],
  129. [LangErrorCode.ConstraintClose, "A close constraint was violated"],
  130. [LangErrorCode.ConstraintAddress, "An address constraint was violated"],
  131. // Accounts.
  132. [
  133. LangErrorCode.AccountDiscriminatorAlreadySet,
  134. "The account discriminator was already set on this account",
  135. ],
  136. [
  137. LangErrorCode.AccountDiscriminatorNotFound,
  138. "No 8 byte discriminator was found on the account",
  139. ],
  140. [
  141. LangErrorCode.AccountDiscriminatorMismatch,
  142. "8 byte discriminator did not match what was expected",
  143. ],
  144. [LangErrorCode.AccountDidNotDeserialize, "Failed to deserialize the account"],
  145. [LangErrorCode.AccountDidNotSerialize, "Failed to serialize the account"],
  146. [
  147. LangErrorCode.AccountNotEnoughKeys,
  148. "Not enough account keys given to the instruction",
  149. ],
  150. [LangErrorCode.AccountNotMutable, "The given account is not mutable"],
  151. [
  152. LangErrorCode.AccountNotProgramOwned,
  153. "The given account is not owned by the executing program",
  154. ],
  155. [LangErrorCode.InvalidProgramId, "Program ID was not as expected"],
  156. [LangErrorCode.InvalidProgramExecutable, "Program account is not executable"],
  157. [LangErrorCode.AccountNotSigner, "The given account did not sign"],
  158. [
  159. LangErrorCode.AccountNotSystemOwned,
  160. "The given account is not owned by the system program",
  161. ],
  162. [
  163. LangErrorCode.AccountNotInitialized,
  164. "The program expected this account to be already initialized",
  165. ],
  166. // State.
  167. [
  168. LangErrorCode.StateInvalidAddress,
  169. "The given state account does not have the correct address",
  170. ],
  171. // Misc.
  172. [
  173. LangErrorCode.Deprecated,
  174. "The API being used is deprecated and should no longer be used",
  175. ],
  176. ]);