errors.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const assert = require("assert");
  2. const anchor = require('@project-serum/anchor');
  3. const { Account, Transaction, TransactionInstruction } = anchor.web3;
  4. describe("errors", () => {
  5. // Configure the client to use the local cluster.
  6. anchor.setProvider(anchor.Provider.local());
  7. const program = anchor.workspace.Errors;
  8. it("Emits a Hello error", async () => {
  9. try {
  10. const tx = await program.rpc.hello();
  11. assert.ok(false);
  12. } catch (err) {
  13. const errMsg =
  14. "This is an error message clients will automatically display";
  15. assert.equal(err.toString(), errMsg);
  16. assert.equal(err.msg, errMsg);
  17. assert.equal(err.code, 300);
  18. }
  19. });
  20. it("Emits a HelloNoMsg error", async () => {
  21. try {
  22. const tx = await program.rpc.helloNoMsg();
  23. assert.ok(false);
  24. } catch (err) {
  25. const errMsg = "HelloNoMsg";
  26. assert.equal(err.toString(), errMsg);
  27. assert.equal(err.msg, errMsg);
  28. assert.equal(err.code, 300 + 123);
  29. }
  30. });
  31. it("Emits a HelloNext error", async () => {
  32. try {
  33. const tx = await program.rpc.helloNext();
  34. assert.ok(false);
  35. } catch (err) {
  36. const errMsg = "HelloNext";
  37. assert.equal(err.toString(), errMsg);
  38. assert.equal(err.msg, errMsg);
  39. assert.equal(err.code, 300 + 124);
  40. }
  41. });
  42. it("Emits a mut error", async () => {
  43. try {
  44. const tx = await program.rpc.mutError({
  45. accounts: {
  46. myAccount: anchor.web3.SYSVAR_RENT_PUBKEY,
  47. },
  48. });
  49. assert.ok(false);
  50. } catch (err) {
  51. const errMsg = "A mut constraint was violated";
  52. assert.equal(err.toString(), errMsg);
  53. assert.equal(err.msg, errMsg);
  54. assert.equal(err.code, 140);
  55. }
  56. });
  57. it("Emits a has one error", async () => {
  58. try {
  59. const account = new Account();
  60. const tx = await program.rpc.hasOneError({
  61. accounts: {
  62. myAccount: account.publicKey,
  63. owner: anchor.web3.SYSVAR_RENT_PUBKEY,
  64. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  65. },
  66. instructions: [
  67. await program.account.hasOneAccount.createInstruction(account),
  68. ],
  69. signers: [account],
  70. });
  71. assert.ok(false);
  72. } catch (err) {
  73. const errMsg = "A has_one constraint was violated";
  74. assert.equal(err.toString(), errMsg);
  75. assert.equal(err.msg, errMsg);
  76. assert.equal(err.code, 141);
  77. }
  78. });
  79. // This test uses a raw transaction and provider instead of a program
  80. // instance since the client won't allow one to send a transaction
  81. // with an invalid signer account.
  82. it("Emits a signer error", async () => {
  83. try {
  84. const account = new Account();
  85. const tx = new Transaction();
  86. tx.add(
  87. new TransactionInstruction({
  88. keys: [
  89. {
  90. pubkey: anchor.web3.SYSVAR_RENT_PUBKEY,
  91. isWritable: false,
  92. isSigner: false,
  93. },
  94. ],
  95. programId: program.programId,
  96. data: program.coder.instruction.encode("signer_error", {}),
  97. })
  98. );
  99. await program.provider.send(tx);
  100. assert.ok(false);
  101. } catch (err) {
  102. const errMsg =
  103. "Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x8e";
  104. assert.equal(err.toString(), errMsg);
  105. }
  106. });
  107. it("Emits a raw custom error", async () => {
  108. try {
  109. const tx = await program.rpc.rawCustomError({
  110. accounts: {
  111. myAccount: anchor.web3.SYSVAR_RENT_PUBKEY,
  112. },
  113. });
  114. assert.ok(false);
  115. } catch (err) {
  116. const errMsg = "HelloCustom";
  117. assert.equal(err.toString(), errMsg);
  118. assert.equal(err.msg, errMsg);
  119. assert.equal(err.code, 300 + 125);
  120. }
  121. });
  122. it("Emits a account not initialized error", async () => {
  123. try {
  124. const tx = await program.rpc.accountNotInitializedError({
  125. accounts: {
  126. notInitializedAccount: (new anchor.web3.Keypair()).publicKey
  127. },
  128. });
  129. assert.fail("Unexpected success in creating a transaction that should have fail with `AccountNotInitialized` error");
  130. } catch (err) {
  131. const errMsg = "The program expected this account to be already initialized";
  132. assert.equal(err.toString(), errMsg);
  133. }
  134. });
  135. });