agent-js icon indicating copy to clipboard operation
agent-js copied to clipboard

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

Open AndyGura opened this issue 2 years ago • 1 comments

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: https://github.com/dfinity/agent-js/blob/8254c47694c1286058a8293547bda98a6d19c8e1/packages/agent/src/polling/index.ts#L68-L73

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.

AndyGura avatar Jul 11 '23 11:07 AndyGura

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

krpeacock avatar Aug 14 '23 16:08 krpeacock