Skip to content

Commit

Permalink
fix: trap and throw handling in v3 sync call
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Oct 11, 2024
1 parent fd26b69 commit ea30e47
Show file tree
Hide file tree
Showing 6 changed files with 1,607 additions and 219 deletions.
12 changes: 12 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

## [2.1.2] - 2024-09-30
- fix: revert https://github.com/dfinity/agent-js/pull/923 allow option to set agent replica time
- fix: handle v3 traps correctly, pulling the reject_code and message from the certificate in the error response like v2.
Example trap error message:
```txt
AgentError: Call failed:
Canister: hbrpn-74aaa-aaaaa-qaaxq-cai
Method: Throw (update)
"Request ID": "ae107dfd7c9be168a8ebc122d904900a95e3f15312111d9e0c08f136573c5f13"
"Error code": "IC0406"
"Reject code": "4"
"Reject message": "foo"
```
- feat: the `UpdateCallRejected` error now exposes `reject_code: ReplicaRejectCode`, `reject_message: string`, and `error_code?: string` properties directly on the error object.

## [2.1.1] - 2024-09-13

Expand Down
36 changes: 31 additions & 5 deletions e2e/node/basic/trap.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { describe, it, expect } from 'vitest';
import { ActorMethod, Actor } from '@dfinity/agent';
import { ActorMethod, Actor, HttpAgent } from '@dfinity/agent';
import util from 'util';
import exec from 'child_process';
const execAsync = util.promisify(exec.exec);

const { stdout } = await execAsync('dfx canister id trap');

export const idlFactory = ({ IDL }) => {
return IDL.Service({
Expand All @@ -15,9 +20,30 @@ export interface _SERVICE {

describe('trap', () => {
it('should trap', async () => {
const actor = createActor<_SERVICE>(idlFactory, canisterId);
const result = await actor.Throw();

expect(result).toBeUndefined();
const canisterId = stdout.trim();
const agent = await HttpAgent.create({
host: 'http://localhost:4943',
shouldFetchRootKey: true,
});
const actor = Actor.createActor<_SERVICE>(idlFactory, { canisterId, agent });
try {
await actor.Throw();
} catch (error) {
console.log(error);
expect(error.reject_message).toBe('foo');
}
});
it('should trap', async () => {
const canisterId = stdout.trim();
const agent = await HttpAgent.create({
host: 'http://localhost:4943',
shouldFetchRootKey: true,
});
const actor = Actor.createActor<_SERVICE>(idlFactory, { canisterId, agent });
try {
await actor.test();
} catch (error) {
expect(error.reject_message).toContain('Canister called `ic0.trap` with message: trapping');
}
});
});
Loading

0 comments on commit ea30e47

Please sign in to comment.