dfinity / agent-js

A collection of libraries and tools for building software around the Internet Computer, in JavaScript.

Home Page:https://agent-js.icp.xyz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Update call rejection should produce `UpdateCallRejectedError` instead of text error

AndyGura opened this issue · comments

commented

Describe the bug
Though agent-js declares error UpdateCallRejectedError, it cannot be caught in the wild, since initial http response from canister (always?) looks like this:

{
    "ok": true,
    "status": 202,
    "statusText": "Accepted"
}

And therefore if I write throw Error.reject or Debug.trap in canister, it will be processed by polling and produce pure text error here:

throw new Error(
`Call was rejected:\n` +
` Request ID: ${toHex(requestId)}\n` +
` Reject code: ${rejectCode}\n` +
` Reject text: ${rejectMessage}\n`,
);

I believe instance of UpdateCallRejectedError should be thrown instead for more convenient error-handling in top-level application

To Reproduce

  1. Create and deploy motoko canister:
import Error "mo:base/Error";

actor class Test() {
  public shared func err() : async () {
    throw Error.reject("Rejected message");
  };
};
  1. Create agent-js actor for it
  2. Write test code:
try {
    await actor.err();
} catch (err) {
    console.log(err instanceof UpdateCallRejectedError);
}
  1. Observe false in console and check that error contains only plain text message

Expected behavior
UpdateCallRejectedError instance should be caught with all the appropriate fields in it.

I've reviewed this issue and added it to my queue. Thanks for reporting!