Skip to content

Commit 24c76d1

Browse files
authored
chore: update canister logging examples with trapped query calls (#929)
1 parent b098916 commit 24c76d1

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

motoko/canister_logs/Makefile

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,33 @@ test: install
3636
| grep 'print via non-replicated query' && echo 'PASS'
3737

3838
# Test trapped call is recorded in logs.
39-
# Ignore failed call output (so the test continues) and check the logs to contain the message.
40-
-dfx canister call CanisterLogs trap 'trap via update'
39+
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
40+
- dfx canister call CanisterLogs trap 'trap via update'
4141
dfx canister logs CanisterLogs \
4242
| grep 'trap via update' && echo 'PASS'
4343

44+
# Test trap via replicated query call.
45+
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
46+
- dfx canister call --update CanisterLogs trap_query 'trap via replicated query'
47+
dfx canister logs CanisterLogs \
48+
| grep 'trap via replicated query' && echo 'PASS'
49+
50+
# Test trap via non-replicated query call should NOT record the message.
51+
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
52+
- dfx canister call --query CanisterLogs trap_query 'trap via non-replicated query'
53+
! dfx canister logs CanisterLogs \
54+
| grep 'trap via non-replicated query' && echo 'PASS'
55+
4456
# Test call to fail with memory out of bounds.
45-
# Ignore failed call output (so the test continues) and check the logs to contain the message.
46-
-dfx canister call CanisterLogs memory_oob
57+
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
58+
- dfx canister call CanisterLogs memory_oob
4759
dfx canister logs CanisterLogs \
4860
| grep 'StableMemory range out of bounds' && echo 'PASS'
4961

50-
# Test timer trap, assume it's been 5 seconds since the start.
51-
sleep 2
62+
# Test timer trap.
63+
# The timer is setup to trap every 5 seconds, so this test has to be called
64+
# at least 5 seconds after the start to record the trap log message.
65+
sleep 5
5266
dfx canister logs CanisterLogs \
5367
| grep 'timer trap' && echo 'PASS'
5468

motoko/canister_logs/src/Main.mo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ actor CanisterLogs {
3333
Debug.trap(text);
3434
};
3535

36+
public query func trap_query(text : Text) : async () {
37+
Debug.print("right before trap_query");
38+
Debug.trap(text);
39+
};
40+
3641
public func memory_oob() : async () {
3742
Debug.print("right before memory out of bounds");
3843
let offset : Nat64 = 10;

rust/canister_logs/Makefile

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,45 @@ test: install
3636
| grep 'print via non-replicated query' && echo 'PASS'
3737

3838
# Test trapped call is recorded in logs.
39-
# Ignore failed call output (so the test continues) and check the logs to contain the message.
40-
-dfx canister call canister_logs trap 'trap via update'
39+
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
40+
- dfx canister call canister_logs trap 'trap via update'
4141
dfx canister logs canister_logs \
4242
| grep 'trap via update' && echo 'PASS'
4343

44+
# Test trap via replicated query call.
45+
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
46+
- dfx canister call --update canister_logs trap_query 'trap via replicated query'
47+
dfx canister logs canister_logs \
48+
| grep 'trap via replicated query' && echo 'PASS'
49+
50+
# Test trap via non-replicated query call should NOT record the message.
51+
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
52+
- dfx canister call --query canister_logs trap_query 'trap via non-replicated query'
53+
! dfx canister logs canister_logs \
54+
| grep 'trap via non-replicated query' && echo 'PASS'
55+
4456
# Test the call with panic is recorded in logs.
45-
# Ignore failed call output (so the test continues) and check the logs to contain the message.
46-
-dfx canister call canister_logs panic 'panic via update'
57+
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
58+
- dfx canister call canister_logs panic 'panic via update'
4759
dfx canister logs canister_logs \
4860
| grep 'panic via update' && echo 'PASS'
4961

5062
# Test call to fail with memory out of bounds.
51-
# Ignore failed call output (so the test continues) and check the logs to contain the message.
52-
-dfx canister call canister_logs memory_oob
63+
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
64+
- dfx canister call canister_logs memory_oob
5365
dfx canister logs canister_logs \
5466
| grep 'stable memory out of bounds' && echo 'PASS'
5567

5668
# Test call to fail with failed unwrap.
57-
# Ignore failed call output (so the test continues) and check the logs to contain the message.
58-
-dfx canister call canister_logs failed_unwrap
69+
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
70+
- dfx canister call canister_logs failed_unwrap
5971
dfx canister logs canister_logs \
6072
| grep 'Result::unwrap()' && echo 'PASS'
6173

62-
# Test timer trap, assume it's been 5 seconds since the start.
74+
# Test timer trap.
75+
# The timer is setup to trap every 5 seconds, so this test has to be called
76+
# at least 5 seconds after the start to record the trap log message.
77+
sleep 5
6378
dfx canister logs canister_logs \
6479
| grep 'timer trap' && echo 'PASS'
6580

rust/canister_logs/canister_logs.did

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ service : {
22
"print" : (text) -> ();
33
"print_query" : (text) -> () query;
44
"trap" : (text) -> ();
5+
"trap_query" : (text) -> () query;
56
"panic" : (text) -> ();
67
"memory_oob" : () -> ();
78
"failed_unwrap" : () -> ();

rust/canister_logs/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ fn trap(message: String) {
3636
ic_cdk::trap(&message);
3737
}
3838

39+
#[query]
40+
fn trap_query(message: String) {
41+
ic_cdk::print("right before trap_query");
42+
ic_cdk::trap(&message);
43+
}
44+
3945
#[update]
4046
fn panic(message: String) {
4147
ic_cdk::print("right before panic");

0 commit comments

Comments
 (0)